After business rule help

sath
Tera Expert

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.

1 ACCEPTED SOLUTION

Bert_c1
Kilo Patron

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;

View solution in original post

10 REPLIES 10

Try using 'customerValue = current.<field_name>;' on each line.  for taskType use:

 

var taskType = current.sys_class_name.toString();

need the string value of sys_class_name.

 

Seems many here don't test the code they post.

Apologies, my code was tested in background script and I often have to snip out part of my code when it comes to posting a solution as I don't have the same start point (I have to add the lines to define current and then remove them before posting and I removed one too many lines), the missing line was identified by Bert_c1.

 

This actually caused an additional complication here as I was using a 'task' GlideRecord in background script and not a GlideRecord of the table of the record being inserted/updated. This meant that direct dot-walking, which would work in a Business Rule on the task table did not work and thus I needed to use current.ref_[tablename].fieldname. This same method doesn't work in the Business Rule as the GlideRecord is the same type as the record being inserted, not the 'task' table even though the rule is on the 'task' table. My original solution would actually have worked in the end.

 

I have corrected the original solution and tested it using a Business Rule on sc_req_item, sc_task, task and incident. I will move over to testing things like for like as it prevents copy & paste issues and highlights issues like the above which are not obvious until the code fails. I've been somewhat reluctant to do this as I end up having to create fields and scripts to validate things and this may cause me issues down the line.

Hi @Nicholas_Gann,

 

Your restructure of the code is good. Unfortunately, we have to make some assumptions based on what was posted. That there is a custom field named 'u_customer' defined on task that is a reference to the sys_user table. (Since the code posted is using child table reference fields.)

 

Since I didn't define the two custom fields: task.u_customer or rm_story.u_stakeholder, I just used addInfoMessage to debug the logic. And commented out the references to those fields so I could test.

 

Anyway, the OP now has an example of how to script what seems to be the goal in a BR, using your code optimization and my corrections. Th OP can add more cases to the switch statement for tables our examples omitted.

Bert_c1
Kilo Patron

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;

Thank you Bert, this worked!