The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Encounter Business Rule Script Processing Issue.

Meera_P
Tera Expert

Hello,

I'm currently learning about Business Rule Scripts, but I've encountered an issue. While the business rule runs on sc_req_item table without errors, it fails to correctly populate the 'requested for' name in the 'sc_task'.

Can someone please help review the code and let me know where I did incorrectly.  Thank you

 

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

    var gr = new GlideRecord('sc_task');
    gr.initialize();
    gr.request_item = current.sys_id;

    var senderName = getUserByEmail('abraham.lincoln@example.com');
    if (senderName) {
        gr.request_item.requested_for = senderName;
        gr.description = "Requester: " + senderName;
    } else {
        return null;
        //gs.info("Unable to find user for email: abraham.lincoln@example.com");

    }
    gr.insert();

})(current, previous);

// Function to get name from sys_user table based on email
function getUserByEmail(userEmail) {
    var userGR = new GlideRecord('sys_user');
    if (userGR.get('email', userEmail)) {
        // return userGR.name.toString(); 
        return userGR.getUniqueValue();
    } else {
        return null;
    }
}

 

 

7 REPLIES 7

Tony Chatfield1
Kilo Patron

Hi, if you take close look a look at the sc_task table, it does not have a requested_for field, and the field shown on the sc_task form is the requested_for of the related sc_req_item record, which means that your core issue is the field you are trying to set does not exist.

Hello @Tony Chatfield1 

I wasn't aware of that. Thank you for bringing it to my attention. I have a situation where one RITM generates multiple tasks, each of which should have a different requester name. Do you have any suggestions on how I can manage this?

Hi, you could consider adding an additional sys_user reference field to sc_task so that you can deliver this requirement.

@Tony Chatfield1 

Thanks for the suggestion. I've never added a new field to an existing form before, so I want to clarify the steps to make sure I don't make any mistakes.

1.  I should go to the form design and add the new field from there. Is that the correct approach?"

Meera_P_1-1716359074424.png

2. I don't want the new field to appear on every SC task. Is there a way to hide it based on certain criteria?

3. Will the new field be overwritten after the next ServiceNow upgrade?