Updating state before insert of record not working

Anna_Servicenow
Tera Guru

I have the below script where i am trying to update the state of record from open > duplicate request before insert in BR. I have below script but its not working.

1) should I make it After insert ?

2) Is the script wrong?

 

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

    var caseid= current.case;
    var amount = current.amount;
    var serv_type = current.type_of_service;

    var gr = new GlideRecord('x_inffr_task');

    gr.addQuery('case', caseid);
    gr.addQuery('amount', amount);
    gr.addQuery('type_of_service', serv_type);
    gr.addQuery('sys_created_on', '>', gs.daysAgo(30));
    gr.query();
    gr.next();
    while (gr.next()) {
       

        current.state = '8';
        }
    }
1 ACCEPTED SOLUTION

Anna_Servicenow
Tera Guru

The script worked with before insert

View solution in original post

11 REPLIES 11

Ah okay, so you're not wanting to mark the record that is being inserted with this new state of '8', but the other records that are found by the query?

kkrushkov
Mega Sage

Hello, 
Have you tried using current.update() ?

You shouldn't use current.update in a business rule - there shouldn't be a need if you're doing things correctly.

Pratima Kalamka
Kilo Sage

Hello @Anna_Servicenow 

try on after insert BR

try script:

 

 

 

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

    var caseid = current.case;
    var amount = current.amount;
    var serv_type = current.type_of_service;

    // Query records based on specified conditions
    var gr = new GlideRecord('x_inffr_task');
    gr.addQuery('case', caseid);
    gr.addQuery('amount', amount);
    gr.addQuery('type_of_service', serv_type);
    gr.addQuery('sys_created_on', '>', gs.daysAgo(30));
    gr.query();

    if (gr.hasNext()) {
        // Exclude the first record from being marked as a duplicate
        if (gr.next()) {
            // Update the state of the current record to '8' (Duplicate Request)
        current.setValue('state' , '8');
    }

})(current, previous);

 

 

 

 

If my answer is helpful please mark it as helpful or correct!!

 

Pratima.k

This didnt work