script to insert into related list

andy_dufresne
Tera Expert

Hello Gurus,

I thought it was simple but have scratched enough hair that I font have to got barber anytime soon 🙂 !

This is how my form looks [sorry I'm unable to provide the real form screenshot shot]

find_real_file.png

As you can see I have used a 'relationship' table so that I can populate the related-list.   Here is how the relationship is:

find_real_file.png

I wrote this script [I know its not right].   How do I create a script that inserts record into child [workpackages ] and creates insert a parent record and create a link between them?

//insert into workpackage first. This will create a related list record

        var wp = new GlideRecord("u_commmercial_workpackage");  

      //wp.initialize();

      wp.u_workpackage_description = 'test workpackage description';  

      var wp_sys_id = wp.insert();

//This should create the parent record

      var nr = new GlideRecord('u_newrole');  

      nru_mali_number = 'Some Mali Number';     //I'm just putting this so the record doesnt look blank

      var refRole_sys_id = nr.insert();

//This should bind the relationship

      var relation = new GlideRecord('u_rel_role_workpackages');

      relation.setValue(u_refrole,refRole_sys_id);

      relation.setValue(u_workpackages, wp_sys_id);

      relation.insert();

This script throws an error "u_refrole" is not defined. this is one of the fields I defined on the 'u_rel_role_workpackages' table of type reference and referencing u_newrole as above.

So, how do I create a script that inserts these two records [parent and child] and then creates relationship among them.   I'm not too fussed which is created first.

Happy to provide more details if you want.

Thanks for reading!

1 ACCEPTED SOLUTION

manikorada
ServiceNow Employee
ServiceNow Employee

in lines 14 and 15 you need to have the script like this :



relation.setValue('u_refrole',refRole_sys_id);  


relation.setValue('u_workpackages', wp_sys_id);


View solution in original post

6 REPLIES 6

sachin_namjoshi
Kilo Patron
Kilo Patron

Please use below code



  1. //insert into workpackage first. This will create a related list record  
  2.         var wp = new GlideRecord("u_commmercial_workpackage");      
  3.       wp.initialize();  
  4.       wp.u_workpackage_description = 'test workpackage description';      
  5.       var wp_sys_id = wp.insert();    
  6.  
  7. //This should create the parent record  
  8.       var nr = new GlideRecord('u_newrole');      
  9.       nru_mali_number = 'Some Mali Number';     //I'm just putting this so the record doesnt look blank  
  10.       var refRole_sys_id = nr.insert();  
  11.  
  12. //This should bind the relationship  
  13.       var relation = new GlideRecord('u_rel_role_workpackages');  
  14.       relation.setValue('u_refrole',refRole_sys_id);  
  15.       relation.setValue('u_workpackages', wp_sys_id);  
  16.       relation.insert();  

nishailame
ServiceNow Employee
ServiceNow Employee

You are missing quotes around field names.



//insert into workpackage first. This will create a related list record  


        var wp = new GlideRecord("u_commmercial_workpackage");      


      //wp.initialize();  


      wp.u_workpackage_description = 'test workpackage description';      


      var wp_sys_id = wp.insert();    


 


//This should create the parent record  


      var nr = new GlideRecord('u_newrole');      


      nru_mali_number = 'Some Mali Number';     //I'm just putting this so the record doesnt look blank  


      var refRole_sys_id = nr.insert();  


 


//This should bind the relationship  


      var relation = new GlideRecord('u_rel_role_workpackages');  


      relation.setValue('u_refrole',refRole_sys_id);  


      relation.setValue('u_workpackages', wp_sys_id);  


      relation.insert();  






Thanks.


PS: Hit like, Helpful, Correct and Endorse, if it answers your question.