Need business rule to only create record if one doesn't already exist

wade88761
Kilo Contributor

I have a before Business Rule setup to create a record in a custom application whenever an incident is submitted. It works fine but I want to add in a part that says it will only create the record if there hasn't already been a record created in the table for the customer. The field in the table that I want it to check against is u_customer. To clarify, if there is already a record for 'u_customer' in the "u_admin_survey" table then don't run the business rule.

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

var survey = new GlideRecord("u_admin_survey");

  survey.initialize();

  survey.u_incident = current.sys_id;

  survey.u_customer = current.u_on_behalf_of;

  survey.u_ticket_type = "Incident";

  survey.u_customer_phone = current.u_on_behalf_of.phone;

  survey.insert();

})(current, previous);

1 ACCEPTED SOLUTION

venkatiyer1
Giga Guru

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


var survey = new GlideRecord("u_admin_survey");


// check whether it exists below will return null if not present


if(!survey.get("u_customer_phone", current.u_on_behalf_of.phone )) {


  survey.initialize();


  survey.u_incident = current.sys_id;


  survey.u_customer = current.u_on_behalf_of;


  survey.u_ticket_type = "Incident";


  survey.u_customer_phone = current.u_on_behalf_of.phone;


  survey.insert();


}  


})(current, previous);


View solution in original post

5 REPLIES 5

venkatiyer1
Giga Guru

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


var survey = new GlideRecord("u_admin_survey");


// check whether it exists below will return null if not present


if(!((survey.get("u_customer_phone", current.u_on_behalf_of.phone ) && survey.state == "New")) {


  survey.initialize();


  survey.u_incident = current.sys_id;


  survey.u_customer = current.u_on_behalf_of;


  survey.u_ticket_type = "Incident";


  survey.u_customer_phone = current.u_on_behalf_of.phone;


  survey.insert();


}



})(current, previous);



The above should work but just ensure the column name is state or else feel free to change it accordingly