how to populate incident number in a custom

raom
Kilo Explorer

Hi Team,

I have one parent table and having another custom form/table as a related list in the parent table. But here I did not find any matching number/id which is common from both the tables and also the custom table(from the related list) is extending from the parent table. I have created a new field in child custom table to populate the parent table record number. Now I would like to populate a record number from parent table into a custom table when I click on New button from the related list.

Please help on this with the possibilities to achieve this. Appreciate your help.

Thanks,

Rao

12 REPLIES 12

Hi Rao,



Please let me know if it worked for you.


Hi Shishir,


Thank you for the information provided. I tried in the same way, it is populating parent number in the custom table when we click on the New button from related list. But when we click on the existing record(previous related records in the related list), it is just populating 'undefined' values for the existing records.



I have used the below script.



BR:


(function onDisplay(current, g_scratchpad) {


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


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



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



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


      if(getParentId != ''){



  g_scratchpad.spnumber = gr.number;


      }



})(current, g_scratchpad);



CS:


function onLoad() {


    //Type appropriate comment here, and begin script below


      if(g_form.getValue('u_testdem') == '') {              


      g_form.setValue('u_testdem', g_scratchpad.spnumber);              


  }


Hi Rao,



May i know what is the function for function onDisplay in your BR, I tried with your function naming in BR and getting undefined error.



Can you please remove (function onDisplay(current, g_scratchpad) {


and un-comment this line // function executeRule(current, previous /*null when async*/) {



Kindly TRY with below code in BR.



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.spnumber = gr.number;


      }


})(current, g_scratchpad);




Also, if you want you can change the CS little but like below code.



function onLoad() {


    //Type appropriate comment here, and begin script below


        if(g_form.isNewRecord()){


                if(g_form.getValue('u_testdem') == '') {            


                g_form.setValue('u_testdem', g_scratchpad.spnumber);            


        }


  }


Gurpreet07
Mega Sage

I Earlier there was no relation between two table then the related list could be a defined related list.


http://wiki.servicenow.com/index.php?title=Creating_Defined_Related_Lists#gsc.tab=0


Now if you have created a parent field over related list table then the system should automatically generate a related list based on new parent child relationship you just created. Configure related list and try to add try to add that new related list on the form. For this new related list , the form opened by clicking on new button should automatically populate the parent field.


shloke04
Kilo Patron

Hi,



Once the Reference from Child to Parent Table has been configured, then you can simply write an OnLoad Client Script on the Chile Table as per the steps mentioned below:



For example we have a Custom Field on the Incident Task Table called "Incident Number" which needs to be populated with Incident Number, we can use the below script written on the Incident Task Table:



Script:



function onLoad() {


    //Type appropriate comment here, and begin script below


    var par = g_form.getReference('parent',setNumber);                                       //Replace "parent" with your Reference field from Child to Parent Table.



}


function setNumber(par)


{


  g_form.setValue('u_incident_number',par.number);                               //Replace "u_incident_number" with your Required field Name.


}



find_real_file.png



Result:



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