How to Map Previous value in flow designer with code or no code

rmaroti
Tera Contributor

Hi Everyone,

-I am creating an assets task in Flow Designer where the trigger condition is set to on field  Assigned to "changes" on the Configuration Item (cmdb_ci) table. I want to map the previous value of the Assigned to field from the Configuration Item (cmdb_ci) table.

-For this, I initially tried using a no-code solution, but I believe it's not possible. Therefore, I have created the script below:

 
var currentSysId = fd_data.trigger.current.sys_id; // Get the current sys_id of the record that triggered the flow var
 
auditRecord = new GlideRecord('sys_audit'); // Create a GlideRecord object for the audit table
 
auditRecord.addQuery('documentkey', currentSysId); // Filter by the sys_id of the record
 
auditRecord.addQuery('fieldname', 'assigned_to'); // Filter for changes to the "assigned_to" field
 
auditRecord.orderByDesc('sys_created_on'); // Order by the most recent change

auditRecord.setLimit(1); // Only need the most recent audit record for the change
 
auditRecord.query();
 
var previousAssignedTo = null;
 
if (auditRecord.next())
{
previousAssignedTo = auditRecord.old_value; // The previous value will be in the 'old_value' field of the audit record
}
return previousAssignedTo; // Return the previous value of the 'Assigned to' field

 

However, I'm not getting the previous value of Assigned to; it is returning null. Can anyone help me with the correct code to retrieve the previous value?

1 ACCEPTED SOLUTION

GopikaP
Mega Sage

Hi @rmaroti , you can achieve this without code, you can loop the changed fields array object- 

FlowSC1.png

and then we can check if the changed field is 'assigned_to' -

FlowSC2.png

then you can set the field to the previous value - 

FlowSC3.png

View solution in original post

3 REPLIES 3

GopikaP
Mega Sage

Hi @rmaroti , you can achieve this without code, you can loop the changed fields array object- 

FlowSC1.png

and then we can check if the changed field is 'assigned_to' -

FlowSC2.png

then you can set the field to the previous value - 

FlowSC3.png

Ankur Bawiskar
Tera Patron
Tera Patron

@rmaroti 

Flow designer provides you list of Changed Fields with previous and current value

Simply use that Array of Objects and iterate

AnkurBawiskar_0-1741774475120.png

 

better explanation here

Using the data within the "Changed Fields" Pill

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

@rmaroti 

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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