How to update a record using script in workflow?

daniellethomson
Tera Expert

I have a requirement that has a workflow that runs a script to create a record on a table, much like a record producer does (see script below). Is there a way I can modify this script to look to see if a record already exists then updates that one?

var gr = new GlideRecord('u_cmdb_ci_user');  

  gr.initialize();  

  gr.u_first_name = current.variables.first_name;

  gr.u_last_name = current.variables.last_name;

  gr.u_preferred_name = current.variables.preferred_name;

  gr.u_user_name = current.variables.last_name;

  gr.u_work_email = current.variables.last_name;

  gr.u_seating = current.variables.seating;

  gr.location = current.variables.location;

  gr.u_management_position = current.varaibles.management_position;

  gr.u_work_phone = current.variables.work_phone;

  gr.u_personal_mobile = current.variables.personal_mobile;

  gr.u_status = current.variables.status;

  gr.u_hire_date = current.variables.hire_date;

  gr.u_reports_to = current.variables.reports_to;

  gr.u_management_position = current.variables.management_position;

  gr.u_pay_grade = current.variables.pay_grade;

  gr.u_position = current.variables.position;

  gr.u_business_unit = current.variables.business_unit;

  gr.u_job_function = current.variables.job_function;

  gr.u_home_department = current.variables.home_department;

  gr.company = current.variables.company;

  gr.insert();  

1 ACCEPTED SOLUTION

Your script will look something like this



var gr = new GlideRecord('u_cmdb_ci_user');  


gr.addQuery("email field name", current.variables.<email variable name>);  


gr.query();  


if (gr.next()){  


      gr.u_first_name = current.variables.first_name;  


      gr.u_last_name = current.variables.last_name;  


      ...  


      gr.update();  


} else {  


      gr.initialize();  


      gr.u_first_name = current.variables.first_name;  


      gr.u_last_name = current.variables.last_name;  


      ...  


      gr.insert();  


}  


View solution in original post

7 REPLIES 7

Jim Coyne
Kilo Patron

You certainly can.   Your script could like something like this instead:



var gr = new GlideRecord('u_cmdb_ci_user');


gr.addQuery("field_name1", value1);


gr.addQuery("field_name2", value2);


gr.query();


if (gr.next()){


      gr.u_first_name = current.variables.first_name;


      gr.u_last_name = current.variables.last_name;


      ...


      gr.update();


} else {


      gr.initialize();


      gr.u_first_name = current.variables.first_name;


      gr.u_last_name = current.variables.last_name;


      ...


      gr.insert();


}



You just need to filter on the appropriate data in lines 2 and 3 (or just 2) to search for the record first and if you find one, update it, otherwise create a new record.   Like abhi_r says, filter on the unique identifier.


In this case, I'm not quite sure how to insert a unique identify. This table is for user records and there isn't anything I can guarantee is unique.


Abhinay Erra
Giga Sage

What is the unique key on this table so that we can determine the record already exists.


In this case the unique key would need to be the email address. How would I add that to the script?