How to update Field inside the reference field of a current record via Business rule

Roshini
Giga Guru

Am trying to update a field inside the current records reference field.Am using the below code and able to get the logs, but data is not getting updated inside the reference record. Could someone please help on this.

var a2 = new GlideRecord ('ast_contract');
a2.addQuery('parent_contract',current.parent_contract);
a2.orderByDesc('u_import_sync');
a2.query();
if (a2.next())
{
    gs.info("inside if");
    var b = a2.u_import_sync;
 current.parent_contract.u_import_sync.setValue(b);
gs.info("date is " +  current.parent_contract.u_import_sync);
}
gs.info("done");
})(current, previous);
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Roshini 

Unless you have the glideRecord object of the record you cannot set the field value

try this

var a2 = new GlideRecord ('ast_contract');
a2.addQuery('parent_contract',current.parent_contract);
a2.orderByDesc('u_import_sync');
a2.query();
if (a2.next())
{
	gs.info("inside if");
	var b = a2.u_import_sync;
	var ref = current.parent_contract.getRefRecord();
	ref.setValue('u_import_sync',b);
	ref.update();
}
gs.info("done");

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Roshini 

Unless you have the glideRecord object of the record you cannot set the field value

try this

var a2 = new GlideRecord ('ast_contract');
a2.addQuery('parent_contract',current.parent_contract);
a2.orderByDesc('u_import_sync');
a2.query();
if (a2.next())
{
	gs.info("inside if");
	var b = a2.u_import_sync;
	var ref = current.parent_contract.getRefRecord();
	ref.setValue('u_import_sync',b);
	ref.update();
}
gs.info("done");

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Thank you so much for the quick help.It worked !!