I want to create a new record using a workflow

lexy
Kilo Expert

Using a workflow, I want to create a new record on a table. However, this is creating multiple records. In addition,

I would also like to create a new record related record in the related table of proc_po_item.

var gr = new GlideRecord('proc_po');

gr.query();

gr.initialize();                            

gr.vendor_account = current.variables.u_first_name; //variables.u_first_name is the field from the variable this should be

gr.number = current.variables.u_last_name;

gr.insert();

17 REPLIES 17

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Lexy,



Script should be



var gr = new GlideRecord('proc_po');


gr.initialize();                          


gr.vendor_account = current.variables.u_first_name; //variables.u_first_name is the field from the variable this should be


gr.number = current.variables.u_last_name;


gr.insert();



Can you please give more info on "I would also like to create a new record related record in the related table of proc_po_item.".


Thanks a lot Pradeep. it worked.



Regarding the proc_co_item table, I am trying to create an equivalent sets of related records to the ones just created on the proc_po table.


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Thanks for the update Lexy. Sorry, I'm still confused. Do you want to copy the same records on proc_co_item table? Please confirm.


Screenshots will be helpful.


Thanks a lot Pradeep,



Parent Table: proc_po (fields: vendor, price, description, requested_by)


Child Table: proc_po_item (fields: vendor, order dates, price, model)


Common reference field between Parent and Child tables: "vendor", "price",


Mission:


  1. To create a Parent record from the Workflow = Almost complete
  2. The Parent Record will automatically create a child record using some details from the parent record and others from the catalog item.  



Script to Create a new record from the workflow:


  1. New Record in Parent Table (proc_po)

//


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


gr.initialize();


gr.vendor_account = current.variables.u_first_name;


gr.description = current.variables.u_description;


gr.setValue('vendor', current.variables.u_vendor); //vendor


gr.total_cost = current.price; //price


gr.setValue('ship_to', current.variables.u_ship_to);


gr.setValue('requested_by', current.requested_for); //THIS IS NOT WORKING (set Requested_by in the variable to requested_for on the proc_po table)


gr.insert();


//




Code for the Child record (proc_po_item table)


//Trying to use a BR to auto-create the related list record



Tried a BR to create …


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


                              // Add your code here


                              var gri = new GlideRecord('proc_po_item');


                              gri.addQuery('vendor', current.sys_id);//reference field on related list table


                              gri.query();


                              if (!gri.next()) {


                                                              gri.newRecord();


                                                              gri.vendor = current.sys_id;  


                                                              gri.total_cost = current.price; //price


                                                              gri.insert();


                              }


})(current, previous);




find_real_file.png