Unique Key violation detected by database error msg

tsoct
Tera Guru
//After business rule
//Insert 
//Update
//Delete

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

    // Add your code here
    var gr = new GlideRecord('sc_task');
    gr.addQuery('request_item', current.request_item);
    gr.addActiveQuery();
    gr.query();
    var grReq = current.request_item.getRefRecord();
    if (current.request_item) {
        if (gr.next()) {
            grReq.state = -10;
            grReq.u_on_hold_reason = 'pending response';
            grReq.update();
             } else {
            gs.eventQueue('createupdate.asAdmin', current); 
        }
    }

})(current, previous);

Hello everyone.

This is the this AFTER business rule that causes the 'Unique Key violation detected by database' problem. I tried removing the.update() and changed it to a before business rule. Both do not work to resolve the error. What can I do to change the script? Thank you very much.

 

 

2 REPLIES 2

Amit Gujarathi
Giga Sage
Giga Sage

HI @tsoct ,
I trust you are doing great.
Please find the corrected code.

(function executeRule(current, previous /*null when async*/ ) {
    // Check if current.request_item is not empty
    if (current.request_item) {
        var gr = new GlideRecord('sc_task');
        gr.addQuery('request_item', current.request_item);
        gr.addActiveQuery();
        gr.query();

        var grReq = current.request_item.getRefRecord();

        if (gr.next()) {
            // Only update if the state or u_on_hold_reason has changed
            if (grReq.state != -10 || grReq.u_on_hold_reason != 'pending response') {
                grReq.state = -10;
                grReq.u_on_hold_reason = 'pending response';
                grReq.update();
            }
        } else {
            gs.eventQueue('createupdate.asAdmin', current); 
        }
    }
})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Thanks but i am still getting the same error 

Unique Key violation detected by database ((conn=2179701) Duplicate entry 'aaf02f59876db110bcefdd383cbb35b5' for key 'PRIMARY')