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

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


}  


thank you- that helps!


Glad you got this working.