How to write a script to update related record on same table

Sangeetha8
Tera Contributor

Hi All,

 

I had a requirement to update the contract details of related contracts records when the current manager changes .

I have written a after update business rule. I am querying the parent contract with the current record to get the related contract records.

Business written on -ast_contract

condition- manager changes

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

  var cntno = current.vendor_contract;

    var cnt = new GlideRecord("ast_contract");

    cnt.addEncodedQuery('parent_contract='+cntno);

    //cnt.addQuery("vendor_contract", cntno);

    cnt.query();

 while(cnt.next()) {

        cnt.u_director = cnt.u_manager.u_director;

        cnt.u_vice_president = cnt.u_manager.u_vp;

        cnt.approver = cnt.u_manager;

        cnt.update();

        gs.log("record updated");

    }

    // Add your code here

 

})(current, previous);

 

But this code is not working

Anyone can please help

Thanks

3 REPLIES 3

_Gaurav
Kilo Sage

HI @Sangeetha8 
You don't need to glide on the same table. Just right before BR where the condition is manager changes and you can set the values under Action setion

Hit helpful if this resolves your query.
Thanks!

Hi Need to do the updation on related records example child contracts

Hi @Sangeetha8 adjusted the code accordingly

var cntno = current.vendor_contract;

var cnt = new GlideRecord("ast_contract");

cnt.addEncodedQuery('parent_contract='+cntno);

//cnt.addQuery("vendor_contract", cntno);

cnt.query();

while(cnt.next()) {

cnt.u_director = current.u_manager.u_director; // updating current record value to ast_contract record

cnt.u_vice_president = current.u_manager.u_vp;

cnt.approver = current.u_manager;

cnt.update();

gs.log("record updated");

}

Regards
Harish