Store oldValue in variable and populate it if condition is false

Audumbar kamble
Tera Contributor

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

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
var oldAssignedTo = g_form.getValue('old_assigned_to');
 
if (newValue == 6 || newValue == 9 || newValue == 2) {
        g_form.clearValue('old_assigned_to');
}
else {
        g_form.setValue('old_assigned_to', oldAssignedTo);
    }
}
 
Thank you very much in advance!
#ITSM
4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Audumbar kamble 

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.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Audumbar kamble 

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.

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

Audumbar kamble
Tera Contributor

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'

@Audumbar kamble 

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.

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