- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2022 05:26 AM
Dear - I have a requirement to update Parent Case priority when a case task is marked closed.
Scenario:
Step 1: A Case has priority 3-Moderate to start with.
Step 2: Case priority changed to 1-Critical and at this time a case task is supposed to be created. // I can achieve this requirement using Business Rule on sn_customerservice_case.
Step 3: now when the case task is marked closed, Parent case's priority should be returned to prior value and case should be updatable again which is 3-Moderate at step1.
I am trying use below client script to capture the old case priority the moment Step 2 is executed.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_scratchpad.priority=oldValue;
alert ("old g_scratchpad.priority " + g_scratchpad.priority);
}
alert above is giving me correct value i.e 3.
At step 3:
When I am trying to fetch this old priority value using Business Rule (sn_customerservice_task) with below script and I just can't get the value.
in fact nothing happens when I am trying to log even g_scratchpad.priority
var parentcase = current.parent.getRefRecord();
gs.addInfoMessage("hello current.parent.getRefRecord()" + parentcase.priority);
gs.addInfoMessage("hello g_scratchpad.priority" + g_scratchpad.priority);
if(parentcase.isValidRecord()) {
parentcase.priority = g_scratchpad.priority;
parentcase.update();
}
Please suggest if possible give me screenshots , and in case there is any other alternate way to achieve share please.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2022 05:40 AM
g_scratchpad is used to pass value from display business rule (server side) to client scripts and not vice versa.
What I understand from your script is that you are trying to pass value from client to server which will not work.
Now coming to your scenario , the previous or old value will fail because your parent task may get updated withing the course when the task is open , so it will pickup the previous value of priority which will be 1 and not 3.
So here is the possible solution:
1. I your step 2, when task is being created, run a before update BR which will copy your previous priority to a custom field( or you can use any other field on case table which is not being used) using below script.
current.u_priority = previous.priority.
2. When your child task is closed, run a BR on task table which will update the priority on parent table using script:
gr.priority = gr.u_priority;
gr.update();
here gr is the object which you will use to glide parent table from child table.
Raghav
MVP 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2022 06:02 AM
Hi @bkhan
There are 2 ways to achieve it.
Solution 1:
Create a new field 'Old Priority [u_old_priority]' (hidden) just for tracking old value of Priority field. In your Step 2, in same Business Rule, populate the value of this previous priority value in this new field.
In Step 3, you can update Priority as below:
var parentcase = current.parent.getRefRecord();
gs.addInfoMessage("hello current.parent.getRefRecord()" + parentcase.priority);
gs.addInfoMessage("hello g_scratchpad.priority" + g_scratchpad.priority);
if(parentcase.isValidRecord()) {
parentcase.priority = parentcase.u_old_priority;
parentcase.update();
}
Solution 2: If you don't want to create new Custom field, you can get old value of Priority from 'sys_audit' table. (Priority field should be added in Audit then old it will track it's old values. By Default it should be already there).
Use below After Update Business Rule script to get Old value of Priority and update it:
var audit = new GlideRecord('sys_audit');
audit.addQuery('tablename','sn_customerservice_case');
audit.addQuery('fieldname','priority');
audit.addQuery('documentkey',current.parent.getUniqueValue()); //Try current.parent.sys_id if it does not works
audit.orderByDec('sys_created_on');
audit.setLimit(1);
audit.query();
if(audit.next())
{
var parentcase = current.parent.getRefRecord();
gs.addInfoMessage("hello current.parent.getRefRecord()" + parentcase.priority);
gs.addInfoMessage("hello g_scratchpad.priority" + g_scratchpad.priority);
if(parentcase.isValidRecord()) {
parentcase.priority =audit.oldValue; //Old Value of Audit will store the old value of Priority field
parentcase.update();
}
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2022 05:38 AM
Hi @bkhan
Firstly this is not the way how g_scratchpad works. It stored the value in Display BR and can be fetched using Client Script and not Visa versa.
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2022 05:40 AM
g_scratchpad is used to pass value from display business rule (server side) to client scripts and not vice versa.
What I understand from your script is that you are trying to pass value from client to server which will not work.
Now coming to your scenario , the previous or old value will fail because your parent task may get updated withing the course when the task is open , so it will pickup the previous value of priority which will be 1 and not 3.
So here is the possible solution:
1. I your step 2, when task is being created, run a before update BR which will copy your previous priority to a custom field( or you can use any other field on case table which is not being used) using below script.
current.u_priority = previous.priority.
2. When your child task is closed, run a BR on task table which will update the priority on parent table using script:
gr.priority = gr.u_priority;
gr.update();
here gr is the object which you will use to glide parent table from child table.
Raghav
MVP 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2022 06:02 AM
Hi @bkhan
There are 2 ways to achieve it.
Solution 1:
Create a new field 'Old Priority [u_old_priority]' (hidden) just for tracking old value of Priority field. In your Step 2, in same Business Rule, populate the value of this previous priority value in this new field.
In Step 3, you can update Priority as below:
var parentcase = current.parent.getRefRecord();
gs.addInfoMessage("hello current.parent.getRefRecord()" + parentcase.priority);
gs.addInfoMessage("hello g_scratchpad.priority" + g_scratchpad.priority);
if(parentcase.isValidRecord()) {
parentcase.priority = parentcase.u_old_priority;
parentcase.update();
}
Solution 2: If you don't want to create new Custom field, you can get old value of Priority from 'sys_audit' table. (Priority field should be added in Audit then old it will track it's old values. By Default it should be already there).
Use below After Update Business Rule script to get Old value of Priority and update it:
var audit = new GlideRecord('sys_audit');
audit.addQuery('tablename','sn_customerservice_case');
audit.addQuery('fieldname','priority');
audit.addQuery('documentkey',current.parent.getUniqueValue()); //Try current.parent.sys_id if it does not works
audit.orderByDec('sys_created_on');
audit.setLimit(1);
audit.query();
if(audit.next())
{
var parentcase = current.parent.getRefRecord();
gs.addInfoMessage("hello current.parent.getRefRecord()" + parentcase.priority);
gs.addInfoMessage("hello g_scratchpad.priority" + g_scratchpad.priority);
if(parentcase.isValidRecord()) {
parentcase.priority =audit.oldValue; //Old Value of Audit will store the old value of Priority field
parentcase.update();
}
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2022 10:03 AM
Thanks Anubhav and Raghav!
Custom field created on Case object and that worked well.