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

Shishir Srivast
Mega Sage

I think you are trying to do onChange but client script talls about onLoad and i don't think onLoad supports newValue parameter


patricklatella
Mega Sage

Hi Shishir, thanks for the reply.   Yes I need the field on the child record to populate with the info from the field on the parent record when the child record (which is in a related list under the parent table) is first opened.   This is not possible?


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.



Script Include:


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);          


        }


  }


Kalaiarasan Pus
Giga Sage

Just an advice which will simplify your implementation.



Why don't you add a update business rule on your parent table which will update the child records field? This will be real time data copy without the need of client side ajax. The server side code for this would be much simpler and you will also end up avoiding an extra Ajax call.