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

Rahul RJ
Giga Sage
Giga Sage

Hi @Desmo ,

 

You need to create an after-business rule on the child table and try the below script.

 

   var grPar = new GlideRecord("ast_service");//glide tablerecord
   grPar.addQuery('sys_id',current.parent_contract);//get sysid of parent record
   grPar.query();

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

 

 

Regards,

RJ

 

RJ,

 

Thanks for the business rule. Looking for assistance on the background script as well.

 

Thanks.

@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

 

RJ,

 

Tested and confirmed working.

To quickly discern the manner of the question and provide a usable solution is of great ability--which only some people posses. Thank you.

 

Desmo