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

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


Sometimes life feels like $h!t when I make those school boy errors!!



Thanks mate, it was very useful!!


Here is the updated code if anyone ends up like me:



var wp = new GlideRecord("u_commmercial_workpackage");


      //wp.initialize();


      wp.u_workpackage_description = 'test workpackage description';


      var wp_sys_id = wp.insert();




      var nr = new GlideRecord('u_newrole');


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


      var refRole_sys_id = nr.insert();




      var relation = new GlideRecord('u_rel_role_workpackages');


      relation.setValue('u_refrole',refRole_sys_id);


      relation.setValue('u_workpackages', wp_sys_id);


      relation.insert();


I created a record producer for the Announcement table and I want to script it to automatically have a specific Service Portal listed in the Service Portal embedded list on the announcement form when submitted. 

Would this method work? I am not sure how to script it.