Work notes for multiple records update

vtred
Tera Contributor

Hello,

This is kind of weird, I have two tables

Table1 and Table2

Table2 is under related list of Table1

My BR runs when Table1 is updated and STATE field has value Closed

and this queries the Table2 and gets all the records related to Table1 and updates the WORK NOTES as CLOSED

But here:

when Table2 has 5 records related to Table1 - The WORK NOTES on Table2 is updating for only one record - its not showing the message form other 4 records, what would be the issue here?

BR:

RUNS ON TABLE1

var gr = new GlideRecord('Table2');

  gr.addQuery('Field1', current.sys_id); //relating to Table1

  gr.query();

  while(gr.next())

  {

// Here if i have 5 records - the worknotes is updating for only 1 record

gr.state = Closed;

  gr.work_notes = "The State is changed to Closed because the Table1 is changed to Closed";

  gr.update();

  }

9 REPLIES 9

preddy
Kilo Guru

Hi,



Please check the BR Conditions : state changes to closed or work_notes updated. Code Everything is Good..



var gr = new GlideRecord('Table2');


  gr.addQuery('Field1', current.sys_id); //relating to Table1


  gr.query();


  while(gr.next())


  {


gr.state = Closed; // please correct values


  gr.work_notes = "The State is changed to Closed because the Table1 is changed to Closed";


  gr.update();


  }



I hope this helps.Please mark correct/helpful based on impact


vtred
Tera Contributor

Yea, Checked the field value aswell. The code seems working fine -



if i have 5 records after quieing, its updating the state field value to closed in all the 5 records, only issue is with the Work notes, which is getting updated for only 1 record instead it should update for all 5 records.


I don't see anything wrong in the code, it should work can you please use gr.setValue() function to update the value and see if it works.


vtred
Tera Contributor

Tried this aswell, No Luck guys. May be my Instance has some issues - will check if any other scripts running in background, also will test this in my personal instance.


Tony Cattoi
Mega Guru

I faced a similar issue and it appears as though gr.update() was breaking the loop.   I ended up wrapping the code in functions to stop the loop from breaking each time.       Something like the following:



function update_table2_records(){


var gr = new GlideRecord('Table2');


  gr.addQuery('Field1', current.sys_id); //relating to Table1


  gr.query();


  while(gr.next())


  {


close_items_table2(gr.sys_id);


  }


}



function close_items_table2(record_id){


var new_gr = new GlideRecord('Table2');


new_gr.get(record_id);


new_gr.state = Closed;


new_gr.work_notes = "The State is changed to Closed because the Table1 is changed to Closed";


new_gr.update();


}