populating a field on a record with info from another table

patricklatella
Mega Sage

Hello all,

even though I've gotten this to work before, still struggling when I setup a new auto populate situation.   I've got a custom field on an extended task table (the parent in this scenario) with name "ocpg_sponsor"...which is entered manually.   I then have another custom field, also called "ocpg_sponsor" on another (separate) extended task table (child table in this scenario).

I need the ocpg_sponsor field in the child table to auto populate with the info from the ocpg_sponsor field in the parent record.   Anyone see my code error?   thanks!

my client script is:

function onLoad(control, oldValue, newValue, isLoading) {

      if (newValue == ''){
  g_form.setValue('ocpg_sponsor','');//name of field on form you want to auto populate
}

var ga = new GlideAjax('u_feedback_task_Ajax');//name of script include
      ga.addParam('sysparm_name', 'getOCPGsponsor');//name of function on script include
ga.addParam('sysparm_ocpg', g_form.getValue('ocpg_sponsor'));//name of field on form triggering call
      ga.getXML(OCPGsponsorLookup); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)
}

// Callback function to process the response returned from the server
function OCPGsponsorLookup(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('ocpg_sponsor',answer);//name of field on form you want to auto populate

}

and my script include is;

var u_feedback_task_Ajax = Class.create();

u_feedback_task_Ajax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {


  getOCPGsponsor: function(){
 
  var retVal; // Return value
  var ocpg     = this.getParameter('sysparm_ocpg');
 
  var OCPGsponsorRec   = new GlideRecord('x_cur_oc_feedback_tasks');//table where desired variable lives
  OCPGsponsorRec.addQuery('ocpg_sponsor',ocpg);
  OCPGsponsorRec.query();
  // Query user records
  if(OCPGsponsorRec.next())
    {
   
    retVal = OCPGsponsorRec.ocpg_sponsor;//name of field with info you want to grab
  }
 
  return retVal;
 
},
});

1 ACCEPTED SOLUTION

patricklatella
Mega Sage

I actually got it from help from Pradeep.



used a BEFORE business rule on the child table with the following script



current.ocpg_sponsor = current.parent.ocpg_sponsor.getDisplayValue();



done!   thanks so much everyone!


View solution in original post

26 REPLIES 26

Hi Kalai,



Will that work if there is no child record and user has to click on New button from the related list to create a new record? I am new learner in SNOW.


If there are no records then you could just move the business rule to the child table and change it to before insert instead. And if you are required to see the field before insertion on form, then the rule could be converted to a display rule.



So, there are lots of ways that you could avoid that extra Ajax call.


Sure, just to avoid confusion i am re-posting the same content with small correction, please have the below code in BR NOT IN Script Include



you can achieve in this way, please have your DISPLAY Business Rule on child table and get the data captured in g_scratchpad



Kindly TRY with below code.



***BUSINESS RULE:***



function executeRule(current, previous /*null when async*/) {


    //This function will be automatically called when this rule is processed.


  var gr = new GlideRecord('parent table');


  var getParentId = gr.get(parent.sys_id);


      if(getParentId != '')


        {


                  g_scratchpad.ocpg_sponsor= gr.ocpg_sponsor;


      }


})(current, g_scratchpad);




Also have the onLoad Client Script on your child table.



Client Script:


function onLoad() {


    //Type appropriate comment here, and begin script below


        if(g_form.isNewRecord()){


                if(g_form.getValue('child table field name') == '') {        


                g_form.setValue('<child table field name>', g_scratchpad.ocpg_sponsor);        


        }


  }


Hi Kalaiarasan,


can you elaborate on your solution?   So you're saying I can create an update business rule on my parent table that will update the desired field on my child task records?   Can you provide an example of what the script would look like?   thanks!


Hi Kalaiarasan,


the problem I'm seeing with using a BR in this case is that if you make a change to the parent field in this relationship, it does not automatically change the child field on the child record.   It does update on the child record if you make an update to the child record, but only that way.   So if you pull up a child record after the parent field has been changed, the child record still shows the previous value of the parent field.   For this reason I'm thinking an onLoad client script in this use case is actually better.   Is my thinking correct?