- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2019 09:56 PM
Hello,
I have a problem with script action which reacts to appropriate event, has access and can set up the "current" object, but does not update fields.
I have added some gs.log statements which show the code works partially apart of the current.update() part.
Can anyone please advise?
Many thanks,
Milan
My script action:
setFields();
function setFields() {
gs.info('!!!MILAN CURRENT STATES BEFORE SETTING THEM: ' + current.assigned_to + ' - ' + current.state + ' - ' + current.short_description);
current.state = '9';
current.short_description = 'Test from script action';
current.assigned_to = '';
gs.info('!!!MILAN CURRENT STATES AFTER SETTING THEM: ' + current.assigned_to + ' - ' + current.state + ' - ' + current.short_description);
try {
current.update();
}
catch(err) {
gs.error("UPDATE ERROR: " + err);
}
Log screen shot (not showing any update error so far..):
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-20-2019 11:13 AM
Hello Milan,
Finally I got the issue and I have tested the functionality its working.
Here are my observations to find out the issue
1. Script action is running perfectly on time but due to another before business rule it is aborting the update.
2. To reproduce issue: comment out the line 5 and 6 as below:
var rec = new GlideRecord(current.sys_class_name);
rec.get(current.sys_id);
rec.state = '9';
//rec.setValue('hold_reason','');
//rec.setValue('u_on_hold_reminder','');
rec.short_description = 'Test from script action';
rec.assigned_to = '';
rec.update();
gs.log('!!!MILAN CURRENT STATES AFTER SETTING THEM: ' + current.assigned_to.name + ' - ' + current.state + ' - ' + current.short_description);
keep any incident on hold, save it and then try to changing state to on hold to in progress or another as you want and try to save. You will get below error (2 addInfoMessage):
invalid update()
On hold reminder datetime must be greater than current datetime.
3. So if you can check the business rule named "Story 130 Check On Hold Reminder".You just have added condtion on hold reminder is not empty and logic for validating date.
RESOULTION: You can choose one of the solution. I have modified the script action to just to achieve your need.
1. Add more condition in business rule :"Story 130 Check On Hold Reminder" like incident state is not pending. use changesFrom, changesTo if needed.
2. Why you have created 2 different business rule? you can merge both business rules and if validation is successful then trigger event else abort update with alert.
3. Modify script action as I did already.
Let me know if anything else is needed.
Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade
Abhishek Gardade
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2019 10:07 PM
Hi Milan,
current object you can use to access the fields but not setting the values using update in script action;
current.update() makes sense in business rule for that record since it operates on record;
current.update() makes sense in UI action since it operates for same record
current.update() makes sense in script include function when you call that function from business rule which operates on same record
why not query the record and update it
setFields();
function setFields() {
try {
var gr = new GlideRecord('table');
gr.addQuery(sys_id', current.sys_id);
gr.query();
if(gr.next()){
gs.info('!!!MILAN CURRENT STATES BEFORE SETTING THEM: ' + current.assigned_to + ' - ' + current.state + ' - ' + current.short_description);
gr.state = '9';
gr.short_description = 'Test from script action';
gr.assigned_to = '';
gs.info('!!!MILAN CURRENT STATES AFTER SETTING THEM: ' + current.assigned_to + ' - ' + current.state + ' - ' + current.short_description);
gr.update();
}
}
catch(err) {
gs.error("UPDATE ERROR: " + err);
}
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2019 10:59 PM
I tried your code and it just does not work.
However there is one difference - the gs.info('!!!MILAN CURRENT STATES AFTER SETTING THEM.... code shows the same values, so it is possible the update might work but it just does not set up the values at all, no idea why...
Thanks, Milan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2019 11:05 PM
Hi Milan,
so the above code of mine is not working?
did you replace the table in your GlideRecord with your table?
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2019 11:04 PM
Try below code, also confirm, there is no other mandatory field when you move state to 9.
setFields();
function setFields() {
var rec = new GlideRecord(current.sys_class_name);
rec.get(current.sys_id);
rec.state = '9';
rec.short_description = 'Test from script action';
rec.assigned_to = '';
gs.info('!!!MILAN CURRENT STATES AFTER SETTING THEM: ' + current.assigned_to + ' - ' + current.state + ' - ' + current.short_description);
rec.update();
}
Please mark this response as correct or helpful if it assisted you with your question.