Background Script: Updated parent record from child record

Desmo
Mega Guru

Hello Community,

 

Looking for assistance to correct the background script below:

 

 

var grChi = new GlideRecord("ast_service");//child record
grChi.addQuery("");//query goes here
grChi.query();

while (grChil.next()) {
   var grPar = new GlideRecord("ast_service");//parent record
   grPar.addQuery('number',grPar.parent_contract);
   grPar.query();

   if (grPar.next()){
      grPar.status = grChi.status;//update field on parent record
      grPar.update();
   }

}

 

 

Bonus question: How to convert script into a business rule?

 

Thank you in advance.

1 ACCEPTED SOLUTION

@Desmo I have updated the background script code.

Old Code - added comments 

var grChi = new GlideRecord("ast_service");//child record
grChi.addQuery("");//query goes here
grChi.query();

while (grChil.next()) {   // this need to be updated as grChi.next as you have created grChi as object 
   var grPar = new GlideRecord("ast_service");//parent record
   grPar.addQuery('number',grPar.parent_contract); // grPar.parent_contract you need to replace this with grChi.parent_contract to get the parent sys_id to get parent record as it's referece field use sys_id field
   grPar.query();

   if (grPar.next()){
      grPar.status = grChi.status;//update field on parent record
      grPar.update();
   }

}

Corrected code 

var grChi = new GlideRecord("ast_service");//child record
grChi.addQuery("");//query goes here
grChi.query();

while (grChi.next()) { // updated grChil to grChi
   var grPar = new GlideRecord("ast_service");//parent record
   grPar.addQuery('sys_id',grChi.parent_contract); // updated this line
   grPar.query();

   if (grPar.next()){
      grPar.status = grChi.status;//update field on parent record
      grPar.update();
   }

}

Regards,

RJ

 

View solution in original post

7 REPLIES 7

Jim Coyne
Kilo Patron

Would be helpful if you stated what you are trying to do so we can help out better as well as let us know what issues you are currently having.

 

But to start, you have a typo and a logic error:

  • you have mismatched "grChi" and grChil" GlideRecord objects so "while (grChil.next())" will throw an error
  • you can't use "grPar" in the "addQuery" of the "grPar" GlideRecord object as it would be null.  I'm assuming you mean "grChi"

As for the After Update Business Rule, it could be simplified like so:

(function executeRule(current, previous /*null when async*/ ) {
	//get the parent record
    var parent = current.parent_contract.getRefRecord();
	//make sure it is a valid record
    if (parent.isValidRecord()) {
        parent.status = current.getValue("status");  //update field on parent record
		parent.update();
    }
})(current, previous);

And you would add whatever appropriate condition for when you want it to run:

 

JimCoyne_1-1692495241569.png

 

The first part is important because you do not want it to run if there is no Parent contract and the second part more than likely only when the State changes and not some another field.

 

 

Gabriel_F
Tera Contributor

The provided solution looks great!

I believe adding [glide-record-name].setLimit(1); when you're looking to query one entity improves performance.

Example:

var grPar = new GlideRecord("ast_service");
grPar.addQuery('number', grPar.parent_contract);

// this line should improve performance
grPar.setLimit(1);

grPar.query();

if (grPar.next()) {
    grPar.status = grChi.status;
    grPar.update();
}

 
The extra performance might not be needed here, but it's something neat to share.

We should always be aware of performance implications, no matter how small.  They all add up.

 

That's why my proposed script goes and gets the record with "getRefRecord()" because we know what it is, no need to search for it with a query.