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

shloke04
Kilo Patron

Hi,



As suggested above, Please write a After Update Business Rule on your Parent Table to Update the values required on your child Task Table as per the script mentioned below:



For example, Requested Item is my parent Table and Catalog Task is my Child Table for the Script shared below and I am copying the Value of Quantity field from RITM to Description field on the Catalog Task Table as mentioned below:



Script:



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




  // Add your code here


  var gr = new GlideRecord('sc_task');


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


  gr.query();


  if(gr.next())


  {


  gr.description=current.quantity;


  gr.update();


  }




})(current, previous);



find_real_file.png



find_real_file.png



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




Regards,


Shloke


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

Regards,
Shloke

patricklatella
Mega Sage

Thanks for the help everyone, seem to be a couple ideas here...just needing a little clarity.



Shloke, does your solution also require the client script from Shishir's solution?



I've created the "after" business rule on my parent table with your code:   "ocpg_sponsor" is the field on both my parent and my child record.   With it like this it is not working...is there more needed?



find_real_file.png



find_real_file.png


patricklatella
Mega Sage

to clarify what I've done so far.   I've create a business rule on my child table with the following advanced script:   Still not working...any suggestions?




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




  // Add your code here


  var gr = new GlideRecord('x_cur_oc_feedback_oc_feedback');//this is my parent table


  gr.addQuery('ocpg_sponsor',current.sys_id);// ocpg_sponsor is the field on my parent table I want to pull data from


  gr.query();


  if(gr.next())


  {


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


  gr.update();


  }




})(current, previous);


patricklatella
Mega Sage

Hello again Shloke,


is your code:


find_real_file.png



what is 'request_item' in your line 5 referring to?   is that a table name or a field name?   can you clarify?   thanks!


Hi Patrick,



Please find the responses for your queries:



1) Nope, The Business Rule which I shared will work for your requirement. There is no need for any Client Script.


2) Can you confirm whether the Business Rule has been written on the child Table or on the Parent Table?


3) "request_item" is the field on the child Table(sc_task) which is referencing to the Parent Table(sc_req_item) as shown below:



find_real_file.png



So in your Script kindly Replace the Reference field on your child table which is referencing the parent Table and it should work for you.



Regards,


Shloke


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

Regards,
Shloke