- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 08:25 AM - edited 07-29-2024 11:35 AM
Hi,
We have to update a field called 'End user' on task form with below:
If task type is Incident, then 'End user' to incident.Caller
If task type is Request, then 'End user' to Request.Requested For
I have created onAfter business rule on insert/update on task table:
(function executeRule(current, previous /*null when async*/ ) {
var taskType = current.sys_class_name;
if (taskType == 'incident') {
var inc = new GlideRecord('incident');
inc.addQuery('number', current.number);
inc.query();
if (inc.next()) {
current.u_customer = inc.caller_id;
}
} else if (taskType == 'sc_request') {
var req = new GlideRecord('sc_request');
req.addQuery('number', current.number);
req.query();
if (req.next()) {
current.u_customer = req.requested_for;
}
} else if (taskType == 'sc_req_item') {
var reqItem = new GlideRecord('sc_req_item');
reqItem.addQuery('number', current.number);
reqItem.query();
if (reqItem.next()) {
current.u_customer = reqItem.requested_for;
}
} else if (taskType == 'sc_task') {
var catTask = new GlideRecord('sc_task');
catTask.addQuery('number', current.number);
catTask.query();
if (catTask.next()) {
current.u_customer = catTask.requested_for;
}
} else if (taskType == 'rm_story') {
var story = new GlideRecord('rm_story');
story.addQuery('number', current.number);
story.query();
if (story.next()) {
current.u_customer = story.u_stakeholder;
}
} else if (taskType == 'rm_enhancement') {
var enhancement = new GlideRecord('rm_enhancement');
enhancement.addQuery('number', current.number);
enhancement.query();
if (enhancement.next()) {
current.u_customer = enhancement.opened_by;
}
} else if (taskType == 'change_request') {
var changeReq = new GlideRecord('change_request');
changeReq.addQuery('number', current.number);
changeReq.query();
if (changeReq.next()) {
current.u_customer = changeReq.opened_by;
}
} else if (taskType == 'change_task') {
var changeTask = new GlideRecord('change_task');
changeTask.addQuery('number', current.number);
changeTask.query();
if (changeTask.next()) {
current.u_customer = changeTask.opened_by;
}
} else if (taskType == 'problem') {
var problem = new GlideRecord('problem');
problem.addQuery('number', current.number);
problem.query();
if (problem.next()) {
current.u_customer = problem.opened_by;
}
} else if (taskType == 'problem_task') {
var problemTask = new GlideRecord('problem_task');
problemTask.addQuery('number', current.number);
problemTask.query();
if (problemTask.next()) {
current.u_customer = problemTask.opened_by;
}
}
current.setWorkflow(false);
current.update();
current.setWorkflow(true);
})(current, previous);
Since I have used current.update(), I have inserted current.setWorkflow(false) to avoid triggering recursive business rules. But this has stopped triggering workflows/flow designers entirely even though I inserted current.setWorkflow(true). Please suggest on workarounds to achieve above requirement without using current.update.
I have tried onBefore BR and removing current.update(), it isn't working either.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 02:35 PM
I don't know what happened to my previous post, but I've tested the following:
(function executeRule(current, previous /*null when async*/) {
var customerValue = '';
var taskType = current.sys_class_name.toString();
gs.addInfoMessage('taskType = ' + taskType);
switch(taskType) {
case 'incident':
customerValue = current.caller_id;
gs.addInfoMessage("caller_id = " + current.caller_id);
break;
case 'sc_request':
customerValue = current.requested_for;
break;
case 'sc_task':
customerValue = current.request_item.requested_for;
gs.addInfoMessage("sc_task = " + currentValue);
break;
case 'sc_req_item':
customerValue = current.requested_for;
break;
// case 'rm_story':
// customerValue = current.u_stakeholder;
// break;
default:
customerValue = current.opened_by;
}
if(customerValue)
gs.addInfoMessage('customerValue = ' + customerValue);
})(current, previous);
get rid of the gs.info() messages, and change last line to current.u_customer = customerValue;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 08:57 AM - edited 07-27-2024 04:08 AM
If you're updating the same record that is triggering the update (which you look to be from skimming the code) then you should be using a Before Business Rule. If you're using a Before Business Rule then current.update() is redundant as current.update() effectively happens anyway when all of the Before Business Rules have ran.
I've not read the code in enough detail to understand why it's not working as a Before rule but this is the correct type to use for this.
I see you're querying the current record again, this is unnecessary as you already have it in the form of current. Someone might beat me to this but the code can be re-written. I'm not sure if you can query a record before it's been committed to the database (as it wouldn't make sense) but it might be failing for this reason if you're attempting to do this for new records
(function executeRule(current, previous /*null when async*/) {
var taskType = current.getValue('sys_class_name');
var customerValue = '';
switch(taskType) {
case 'incident':
customerValue = current.caller_id;
break;
case 'sc_request':
customerValue = current.requested_for;
break;
case 'sc_task':
customerValue = current.request_item.requested_for;
break;
case 'sc_req_item':
customerValue = current.requested_for;
break;
case 'rm_story':
customerValue = current.u_stakeholder;
break;
default:
customerValue = current.opened_by;
}
if(customerValue)
current.setValue('u_customer', customerValue);
})(current, previous);
EDIT: I see why you used GlideRecord here as getValue and dotwalking don't work when trying to get an extended field from the base table but for new inserts the approach of re-querying doesn't seem like it should work as it doesn't exist yet. I'll respond to this later today. There should be ways around this but it would be good to see if it can be done via a single Business Rule
2nd EDIT: Got it working using the ref_ prefix when referencing the field. Also corrected sc_task as requested_for is not a field on that table. I was lazy and used default in the switch statement. If it's not appropriate to use this for classes not listed in your original response (eg: rm_release), then remove this line and the line after and add an additional case for each table and set customerValue to current.opened_by
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 11:19 AM - edited 07-26-2024 11:39 AM
Thank you for the code. I have tried running this, it's going to default case in all the scenarios, it's not going inside the defined case statements.
So, I have changed them to if/else statements and its fetching 'caller id/opened by' only for onAfter business rules, but not for onBefore business rules.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 12:29 PM - edited 07-26-2024 12:31 PM
The code posted, that you tried falls to the 'default' case since 'taskType' is not defined. The above script is missing:
var taskType = current.sys_class_name;
before the switch statement. And set your BR to run before and no update is needed.
see:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 12:58 PM
yes, i have added that to the code already, it isn't working for onBefore BR