script

BharatiK
Tera Contributor

Hi All,

 

I am writing After Business Rule, and have created Problem ticket when Incident is "on hold". 

In addition I want to update Incident Ticket as well, with Problem created. How we can achieve this?

 

 

Script:

var gr = new GlideRecord ('problem');
    gr.initialize();
    gr.short_description = current.short_description;
    gr.cmdb_ci = current.cmdb_ci;
    gr.insert();
    gs.info ('Problem Ticket' + ' ' +  gr.number);
 
 var inc = new GlideRecord('incident');
    inc.query();
    if (inc.next()) {
        inc.problem_id = gr.sys_id;
        inc.update();
    }
 
 
this script is not working to update incident record.
 
 
 
Thanks
 
 
9 REPLIES 9

jcmings
Mega Sage

If I am understanding correctly, when your Incident state changes to On Hold, you want to create a Problem record, and link that Problem record to the incident record.

 

If that is correct, your script should look something like this:

//Create a problem
var gr = new GlideRecord('problem');
gr.initialize();
gr.short_description = current.short_description;
gr.cmdb_ci = current.cmdb_ci;
var probRec = gr.insert();
gs.info ('Problem Ticket' + ' ' +  gr.number);

//Connect the problem record back to this incident
current.problem_id = probRec;

 

I believe the script you have set up is working, but since you're not using current, and instead a GlideRecord query into Incident, you're updating a random record.

Ankur Bawiskar
Tera Patron
Tera Patron

@BharatiK 

update as this in after update BR

Ensure BR Condition is correctly configured -> State Changes to ON HOLD

Script:

var gr = new GlideRecord('problem');
gr.initialize();
gr.short_description = current.short_description;
gr.cmdb_ci = current.cmdb_ci;
var probRec = gr.insert();
gs.info ('Problem Ticket' + ' ' +  gr.number);

//Connect the problem record back to this incident
current.problem_id = probRec;
current.setWorkflow(false);
current.update();

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar , 

 

Thanks for your inputs. This worked. 

But, isn't it recommended not to use current.update() in business rules?

 

@BharatiK 

Glad to know that it worked

it's not recommended.

you can use before update BR then and current.update() not required

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader