glide_list

Kanika4
Tera Contributor

I need to write logic to keep a glide_list field sync with the related list on that form. In other words, there is a table Y in the related lists of table X, all the records of table Y should get displayed on table X list field. (Table X and Y have m2m mapping).

Could anyone please help..

1 ACCEPTED SOLUTION

Hi Kanika,

Try below scripts and do testing

sample script below for the insertion

var rec = new GlideRecord('table X');
rec.addQuery('sys_id', current.<fieldX>);
rec.query();
if(rec.next()){

var listArray = rec.<listField>.toString().split(',');

var valueOfY = current.<fieldY>;

listArray.push(valueOfY.toString());

rec.<listField> = listArray.toString();
rec.update();

}

Sample script for deletion

var rec = new GlideRecord('table X');
rec.addQuery('sys_id', current.<fieldX>);
rec.query();
if(rec.next()){

var listArray = rec.<listField>.toString().split(',');

var valueOfY = current.<fieldY>;

var indexOf = listArray.indexOf(valueOfY);

listArray.splice(indexOf, 1);

rec.<listField> = listArray.toString();
rec.update();

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kanika,

you would require to have after insert/update business rule on the m2m table;

BR Condition: value Y changes; query and update the record of table X; if there is addition then get the current value and append it

On similar line you would require after delete Business rule on m2m table to remove the value being deleted

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar  Could you help me with the code for 

BR Condition: value Y changes; query and update the record of table X; if there is addition then get the current value and append it

Hi Kanika,

Try below scripts and do testing

sample script below for the insertion

var rec = new GlideRecord('table X');
rec.addQuery('sys_id', current.<fieldX>);
rec.query();
if(rec.next()){

var listArray = rec.<listField>.toString().split(',');

var valueOfY = current.<fieldY>;

listArray.push(valueOfY.toString());

rec.<listField> = listArray.toString();
rec.update();

}

Sample script for deletion

var rec = new GlideRecord('table X');
rec.addQuery('sys_id', current.<fieldX>);
rec.query();
if(rec.next()){

var listArray = rec.<listField>.toString().split(',');

var valueOfY = current.<fieldY>;

var indexOf = listArray.indexOf(valueOfY);

listArray.splice(indexOf, 1);

rec.<listField> = listArray.toString();
rec.update();

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Kanika,

Any update on this?
Can you mark my answer as correct, 👍 helpful if you were able to achieve the requirement. This enables other members to learn from this thread.

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader