logo

NJP

Display Business Rule and g_scratchpad

Import · Sep 15, 2020 · article

Hi,

In this article, I will explain the usage of Display Business Rules and g_scratchpad with 1 small requirement

Requirement: When creating a new problem task from Problem view i want to populate few fields of problem into the problem task such as Assignment group, Assigned to, problem statement.

So For this, we will create a Display BR on the problem_task table. The Display Br executes whenever we view a record on the problem_task.

Display BR

System Definition -> Business Rules ->New

Table: Problem_task

conditions: Parent is not empty (So that this BR runs only when click on new from existing problem)

image

Under Advanced, in the script add this code

(function executeRule(current, previous /*null when async*/ ) {
    var gr = new GlideRecord("problem");
    if (gr.get(current.parent)) {
    g_scratchpad.parent=current.parent;
        g_scratchpad.short_description=gr.short_description;
    g_scratchpad.assigned_to=gr.assigned_to;
    g_scratchpad.assignment_group=gr.assignment_group;
    }

})(current, previous);

Save the BR.

Then create a onLoad client script and there you will have access to g_scratchpad object and populate the values into the problem_task form.

Client Script

function onLoad() {
   //Type appropriate comment here, and begin script below
    if(g_scratchpad.parent) {
        g_form.setValue("short_description",g_scratchpad.short_description);
        g_form.setValue("assignment_group",g_scratchpad.assignment_group);
        g_form.setValue("assigned_to",g_scratchpad.assigned_to);
    }   
}

Let me know if you have any questions in the comments below.

Mark the article as helpful and bookmark if you found it useful.

Regards,Asif

2020 ServiceNow Community MVP

View original source

https://www.servicenow.com/community/developer-articles/display-business-rule-and-g-scratchpad/ta-p/2321189