Related Records - auto-assigning Student ID# to related records Notes Student ID# when creating the related record.

4 posts by 2 authors in: Forums > CMS Builder
Last Post: November 14, 2017   (RSS)

By Dave - November 3, 2017

Hi Zicky, 

If you click "Create" under your related records section and then look in the URL, there should be a field named "yourtableNum".  If you name your field with that name it should get auto-populated.  

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By Mikey - November 3, 2017

That's great Dave!!! Works like a charm!

Hey is it possible to get the record's "Title" field as well using this method? Example "yourtableTitle".

Thanks, Zicky

By Dave - November 14, 2017

Hi Zicky, 

There's no easy way to do that.  You could perhaps write a plugin that that checked for yourtableNum in _REQUEST and then populated the values.  

Actually, I ended up writing it for you.  Try this (relatedRecordsFieldPopulator.php): 

<?php
/*
Plugin Name: Related Records Field Populator
Description: Populate additional fields when adding records with related records
Version: 1.00
CMS Version Required: 3.11
*/

addAction('record_preedit', function($tableName, $recordNum) {
  $parentTable = 'every_field_multi'; // this is the table with a related records field      
  $childTable  = 'accounts';          // this is the table that gets added to
  
  // skip unless adding related record to target table
  $parentNumField  = $parentTable . "Num"; // this visible in the url
  $parentRecordNum = isset($_REQUEST[$parentNumField]) ? $_REQUEST[$parentNumField] : '';
  if ($tableName != $childTable || !$parentRecordNum) { return; } 
  
  // load record
  $parentRecord = mysql_get($parentTable, $_REQUEST[$parentNumField]);
  if (!$parentRecord) { return; } // skip if no parent record

  // pre-populate fields with the same name
  foreach ($parentRecord as $key => $value) {
    if (isset($_REQUEST[$key])) { continue; } // skip if already set
    $_REQUEST[$key] = $value;
  }
  
  // set some custom fields
  if (!isset($_REQUEST['customChildFieldName'])) { // if not already set
    $_REQUEST['fullname'] = $parentRecord['title'];
  }

});

// eof

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com