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

Kieran Anson
Kilo Patron

Hi,

Are you just wanting to check if a record exists, and if so, modify the current state to '8'?

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

	var relatedTasksGR = new GlideRecord('x_inffr_task');
	relatedTasksGR.addQuery('case' , current.getValue('case'));
	relatedTasksGR.addQuery('type_of_service' , current.getValue('type_of_service'));
	relatedTasksGR.addQuery('amount' , current.getValue('amount'));
	relatedTasksGR.addQuery('sys_created_on' , '>', gs.daysAgo(30));
	relatedTasksGR.setLimit(1);
	relatedTasksGR.query();
	if(relatedTasksGR.hasNext())
		current.setValue('state' , '8')

})(current, previous);

Hi, I am checking for some condition, that would consider the newly opened one as duplicate, So basically the first record will be excluded, rest all newly created record that matches with case, amount and type of existing will b considered as duplicated and all the rest(could be more than 1) will be set to state duplicate on insert

So, to understand this correctly this BR:

  • Runs on insert of the record (lets call it a Case)
  • If a similar case is identified where it has the same amount, and type of service, you want to mark the record being inserted as duplicate (which I assume is 8 in your state model)?

  • Runs on insert of the record (lets call it a Case) >> > yes
  • If a similar case is identified where it has the same amount, and type of service, you want to mark the record being inserted as duplicate (which I assume is 8 in your state model)? >> could be more than1puplicate, all should be marked to stat 8