Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Community Alums
Not applicable

Hi @sath ,

Instead of after BR please use before BR and you don't need to update the value please use below code it is working for me 

 

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var taskType = current.getValue('sys_class_name');
    gs.log("Type = " + taskType);
    var customerValue = '';

    switch (taskType) {
        case 'incident':
            customerValue = current.getValue("caller_id");
			current.setValue('u_customer', customerValue);
            break;
        case 'sc_request':
            customerValue = current.getDisplayValue("requested_for");
			current.setValue('u_customer', customerValue);
            break;
        case 'sc_task':
            customerValue = current.request_item.getDisplayValue("requested_for");
			current.setValue('u_customer', customerValue);
            break;
        case 'sc_req_item':
            customerValue = current.requested_for;
			current.setValue('u_customer', customerValue);
            break;
        case 'rm_story':
            customerValue = current.u_stakeholder;
			current.setValue('u_customer', customerValue);
            break;
        default:
            customerValue = current.opened_by;
			current.setValue('u_customer', customerValue);
    }
	gs.log("Customer Vlaue = " + customerValue);



})(current, previous);

 

Result 

Type is type of incident and it gets the value of incident's caller in Customer 

SarthakKashyap_0-1722095672317.png

 

Please mark mt answer correct and helpful if this works for you

Thanks and Regards

Sarthak