Create a problem record when Incident is Resolved and priority is Critical.

anubhavr
Giga Expert

I have to create a problem record when Incident state is resolved and priority is '1-Critical'. I have tried it using business rule but i am getting an error.

The Error is:- Unique Key violation detected by database (Duplicate entry '778bdfe6db4122005a91d421cf96193c' for key 'PRIMARY')

My business rule scripts is:-

When to run:- State is resolved AND prioirity is 1-critical AND problem_id is empty.

Script :-

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

var prob = new GlideRecord("problem");

prob.short_description = current.short_description;

prob.cmdb_ci = current.cmdb_ci;

prob.priority = current.priority;

//gs.addInfoMessage(prob.work_notes);

//gs.addInfoMessage(answer);

var sysID = prob.insert();

current.problem_id = sysID;

var mySysID = current.update();

gs.addInfoMessage("Problem " + prob.number + " created");

action.setRedirectURL(prob);

action.setReturnURL(current);

})(current, previous);

After testing a problem record is created but still above error is coming. what is this error and how to correct it???

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Anubhav,



Can you turn off this BR and check once. If the issue still exists then there must be current.update() line in BEFORE Business rule on the table.


Please check for the same on your instance once.


http://wiki.servicenow.com/index.php?title=Business_Rules_Best_Practices


View solution in original post

9 REPLIES 9

Chuck Tomasi
Tera Patron

Hi,



Is this happening in an BEFORE business rule or after? I recommend an AFTER since you want to create the problem AFTER the incident has been saved.



My changes are in bold below...



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



var prob = new GlideRecord("problem");


prob.newRecord(); // add this to get new values


prob.short_description = current.short_description;


prob.cmdb_ci = current.cmdb_ci;


prob.priority = current.priority;


//gs.addInfoMessage(prob.work_notes);


//gs.addInfoMessage(answer);



var sysID = prob.insert();


current.problem_id = sysID;


current.setWorkflow(false); // do not trigger business rules again


current.update();



gs.addInfoMessage("Problem " + prob.number + " created");


action.setRedirectURL(prob);


// action.setReturnURL(current);   // two redirects don't make sense. It's like saying go left then go right at the same time!



})(current, previous);



Reference:


Business Rules - ServiceNow Wiki


Business Rules Best Practices - ServiceNow Wiki  


Thank Chuck,



I tried your script but still I am getting same error:



Unique Key violation detected by database (Duplicate entry '.......' for key 'PRIMARY')


Invalid insert.


Hi Chuck,



Sorry for the trouble. Actually my Business Rule was colliding with the similar kind of another business rule. The issue is resolved. My script is also working.



Thank You,


Anubhav.


I'm glad you got your question answered. Thank you for participating in the community.



Please mark the appropriate response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.



If you are viewing this from the community inbox you will not see the correct answer button.   If so, please review How to Mark Answers Correct From Inbox View.



Thank you