How can I get a stored value back
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2024 12:30 PM
Incident:
Value of the incident : Caller as A and impact field as 2
if i change the caller to B then impact should change to 3 but if I change it to caller C it should show impact as 2 again. I am losing my old value for some reason
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2024 05:02 AM - edited 10-25-2024 05:07 AM
This script will run every time Caller is changed, so in your scenario when Caller is changed the second time to 'C', the value of impact_old = 3. You need to incorporate logic for changing the impact to a certain value based on Caller, or else do this instead with a Business Rule before Insert or Update when Caller changes (when the record is saved) so that impact only changes once. Using this method you won't see Impact change on the form until after the form is reloaded when it is saved.
The other thing you could do is use a display Business Rule to capture the value of impact when the form loads and store it in a g_scratchpad variable.
g_scratchpad.impact = current.impact;
Your onChange Client Script would then access the g_scratchpad variable instead of getValue for impact.
var impact_old = g_scratchpad.impact;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2024 05:11 AM - edited 10-25-2024 05:13 AM
Hi,
oldValue doesn't change when you make different selections on the firm. Old value is the value when the record was loaded form DB , and it will not change unless record is saved back in the DB.
in your case why do you need the else, you change the impact only when caller is A, else you don't touch it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2024 05:22 AM
Hi @NaumanC ,
In ServiceNow, the oldValue in an onChange client script remains constant until you save the record or reload the page. Refer to the example below to understand the concepts of oldValue and newValue in an onChange client script.
Explanation of oldValue and newValue
Initial State:
- When the form loads, the field has an initial value:
- oldValue = 'a'
- When the form loads, the field has an initial value:
First Change:
- When the user changes the value from 'a' to 'b':
- oldValue remains 'a' (the value before this change).
- newValue is now 'b' (the new value after the change).
- When the user changes the value from 'a' to 'b':
Second Change:
- If the user changes the value again from 'b' to 'c':
- Now, oldValue is still 'a' (the value before the first change).
- newValue becomes 'c' (the new value after this change).
- If the user changes the value again from 'b' to 'c':
Summary of Value Changes:
- After Form Load:
- oldValue: 'a'
- After First Change (to 'b'):
- oldValue: 'a', newValue: 'b'
- After Second Change (to 'c'):
- oldValue: 'a', newValue: 'c'
Conclusion:
- The oldValue parameter retains the value from the last change that triggered the onChange event, not the current state of the field.
- Each time the field changes, the oldValue does not update to reflect the most recent change; instead, it holds the previous value from the last event.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you found my response **helpful**, I’d appreciate it if you could take a moment to select **"Accept as Solution"** and **"Helpful"** Your support not only benefits me but also enriches the community.
Thank you!
Moin Kazi
www.linkedin.com/in/moinuddinkazi
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~