Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to update next record field value based on previous record field

Deepak92
Tera Contributor

Hello Folks,

for the same ticket id if  record B has sub status as closed redirect i want to set  record A field value.

Any help would be highly appreciated 

Deepak92_1-1676566051961.jpeg

 

 

 

14 REPLIES 14

Hi @Deepak92 , please try below script in your on after business rule.

 

var tacketId = current.ticket_id; //Give appropriate Ticke Id field name here

var gr = new GlideRecord('incident'); // pass your approprite table name
gr.addQuery('ticket_id', tacketId); // This query to find another same tickid record.
gr.addQuery('number', "!=", current.number); // This query to find another record.
gr.query();
while (gr.next()) {
gr.biznow_sub_status_field = 'Closed - Redirect'; //pass your approriate field name for 'biznow_sub_status_field' and set your logical value of
gr.update();
}

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
Thank you

Swamy

Hi @Deepak92 

 

you can write after insert BR and in the script check if there is already a record with the same ticket ID and status is not empty then update the current record with existing record sub status.

Hello @priyasunku ,

How do i check whether previous records having status (clsoed_Redirect ) ?? and then i need to set current record field. 

i don't need to update previous record. 

Hi @Deepak92 , Thank you for the clearing the requirement.
I will suggest you to create one After Insert BR on respective table.
e.g. I have consider Incident table here.

Sonu_Parab_0-1676647686495.png

Sonu_Parab_1-1676647821697.png

 


And use below Script

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

    // Add your code here
    var ticketId = current.u_ticket_id;

    var gr = new GlideRecord('incident');
    gr.addQuery('u_ticket_id', ticketId);
    gr.addEncodedQuery("u_biznow_sub_status=closed _redirect");
    gr.query();
    if (gr.next()) {
        //gs.addInfoMessage("Record found with same ticket Id and status is closed _redirect");
        current.u_biznow_sub_status = 'closed _redirect';
        current.update();
    }

    //Note: 
	//1] Here  'u_ticket_id' is a field name of 'Ticket Id' field
    //2] 'u_biznow_sub_status'  is a field name of 'BizNow Sub Status' field and 'closed _redirect' is choice of this field.
    //3] Here 1st query to check same ticket id exists or not and 2nd one is to check its status is closed _redirect.
	//Kindly change the Table Name and fields name accordingly.



})(current, previous);


Let us know if you are facing any issue
Thank you

Hello @Sonu Parab ,

Thanks for the revert.

current.update is not recommended  on after BR . correct ?  it may run again and again and create performance issues.

what if i declare  current value in variable and then update them. will it also going to create performance issue like which current.update used to create  .just want to cross check with you