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

patricklatella
Mega Sage

ok I think I'm close, but not working just yet, I replaced "request_item" with "parent", and the child ocpg_sponsor field populated with this



b43b72864f2602004100029d0210c71e



find_real_file.png


Hi,



Saw your script again which you shared above in the Post. Can you please write the Business Rule on your Parent Table and update your Script as below:



Script:



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




  // Add your code here


  var gr = new GlideRecord('x_cur_oc_feedback_oc_feedback');                             //This should be the Child Table


  gr.addQuery('parent',current.sys_id);


  gr.query();


  if(gr.next())


  {


  gr.ocpg_sponsor=current.ocpg_sponsor;                                                       // ocpg_sponsor is the field on my child table I want to autopopulate


  gr.update();


  }




})(current, previous);




In the above Script gr.field_name should be the Child Table field where you want to copy the Value to.


And current.field_name should be the Field from where you want to copy the Value from.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

patricklatella
Mega Sage

Thanks Shloke, but here's what I've got, not working...I have this as an "after Update" BR on my parent table.




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



// Add your code here



var gr = new GlideRecord('x_cur_oc_feedback_tasks');                             //This should be the Child Table



gr.addQuery('parent',current.sys_id);



gr.query();



if(gr.next())


 


{


 


  gr.ocpg_sponsor=current.ocpg_sponsor;                                                       // ocpg_sponsor is the field on my child table I want to autopopulate


 


  gr.update();


 


}







})(current, previous);


Strange. Is this your Personal Instance? If yes can you inbox me your credentials. I Will have a look into it.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Can you please try with below code on your child table business rule.



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


// Add your code here


var gr = new GlideRecord('x_cur_oc_feedback_oc_feedback');


gr.get(parent.sys_id);


current.ocpg_sponsor = gr.ocpg_sponsor;


current.update();


}


})(current, previous);