After Business Rule Not Updating Records

Brando Rdz
Tera Contributor

I need an after business rule to trigger on the sc_req_item table to glide over to another table and update a value on that table.

I've gone through the code a few times, tried a few different solutions but nothing seems to be successful in getting the action to function as expected.

 

BR Script:

 

 (function executeRule(current, previous /*null when async*/) { 
        
var gr = new GlideRecord('sc_req_item');
	gr.addQuery('cat_item', current.u_item );
	gr.query();
        
while (gr.next()) {

current.u_item = gr.cat_item;
gr.update();

 }
  })(current, previous);

 

4 REPLIES 4

James Chun
Kilo Patron

Hi @Brando Rdz,

 

Not sure what you are trying to achieve but in your code, the following lines should be:

current.u_item = gr.cat_item;
gr.update();

BE INSTEAD

gr.cat_item = current.u_item;
gr.update();

 

Thanks

Andrew_TND
Mega Sage
Mega Sage

Hi @Brando Rdz 

 

If I’m reading this right if you’re running a BR on the sc_req_item table why are you using “new GlideRecord() on the same table as it won’t go anywhere. This needs to be updated first.” 

in terms of your script I’d add “getdisplayVaue() after the below to read as current.u_item.getDisplayValue();

gr.addQuery('cat_item', current.u_item );

Please mark as helpful and if it’s resolved the issue, CORRECT!  

Maddysunil
Kilo Sage

@Brando Rdz 

Not sure about your use case but you can update correct field name in below code it should work

 

(function executeRule(current, previous /*null when async*/) {
    gs.info("testItem "+current.u_item);
    if (current.u_item) {
        var gr = new GlideRecord('sc_req_item');
        gr.addQuery('cat_item', current.u_item);
        gr.query();

        while (gr.next()) {
            // Update the desired field on the sc_req_item record
            gr.u_desired_field = 'New Value'; // Replace with the actual field and value you want to update
            gr.update();
        }
    }
})(current, previous);

 

 

Kindly mark helpful/accepted if it helps you.

Thanks

Aman Kumar S
Kilo Patron

Hi @Brando Rdz 

 

What your code says, you are trying to pull value from a field on a table and trying to populate that field(catalog item) and gliding the Requested item table and populating the same value again.

Can you give specifics, from which table/field you are trying to copy and against which table/field. Also, what is the criteria to populate the record.

Best Regards
Aman Kumar