Store oldValue in variable and populate it if condition is false
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 02:56 AM
Hi Team,
We have a catalog item which is used to update asset records in ServiceNow. 'Asset data' is a variable set which contains below variables (Assigned to, Owned by, State, Sub-state, Location).
The requirement is if state is selected (In Stock, In transit or On order), the Assigned to field value should be cleared and if not keep the Assigned to value as it is on the form.
I have created the onChange catalog client script on the variable set for 'State' field, It is clearing the value but not showing the old assigned to field value if we select another state.
#catalog client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 03:06 AM
that's expected. try this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
// Define the states that require clearing the "Assigned to" field
var clearStates = ['6', '9', '2'];
// Check if the new state is one of the states that require clearing the "Assigned to" field
if (clearStates.includes(newValue)) {
g_form.clearValue('assigned_to');
} else {
// If the state is not one of the clear states, retain the old value
var oldAssignedTo = g_form.getValue('assigned_to');
g_form.setValue('assigned_to', oldAssignedTo);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 03:09 AM
check this link for solution
Get 'dynamic old value' on client script
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 04:08 AM
Hi @Ankur Bawiskar ,
Thanks for you reply.
I tried, it is clearing Assigned to value but showing Assigned to field old value if state is not one of them(6,9,2).
I want as below -
For example:
First change: Field State, from 'In Use' to 'In Stock' --> Clear Assigned to From 'XYZ' to <empty>
Second change (without saving form): Field State, from 'In Stock' to 'In Use' --> Populate Assigned to value from <empty> to 'XYZ'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 04:17 AM
check the link I shared and it should work as per your expectation
sharing again here
Get 'dynamic old value' on client script
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader