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

@BharatiK 

Since you mentioned my script worked.

Please mark my response as correct and close the thread so that it benefits future readers.

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

@BharatiK 

Hope you are doing good.

Did my reply answer your question?

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

@BharatiK 

If my response worked, please mark my response as correct and close the thread.

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

KoteswaraVM
Tera Expert

Hi @BharatiK ,

The Script you mentioned is correct but a small script error.

Please refer to the below script:

Script: (BharatiK - 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.addQuery('sys_id', current.sys_id.toString());   //need to specify on what incident record the update action needs to be performed. If not the very first outcome or result of the query will get updated with the value since we used if after inc.query().
    inc.query();
    if (inc.next()) {
        inc.problem_id = gr.sys_id;
        inc.update();
    }


But for your reference, the best practice is not to use current.update() in BRs but if you want to use current.update() in BRs then please refer to the below script

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);

  current.problem_id = gr.sys_id.toString();
current.setWorkflow(false); //this will help to stop all the Business Rules on the record to avoid the performance issues.
current.update();
current.setWorkflow(true); //hence the update is completed, setWorkflow(true) will help to enable all the Business Rules on the record.


I hope this information will help you and happy learning.




Thanks,
Koteswara Vara Prasad M

Runjay Patel
Giga Sage

Hi @BharatiK ,

 

I would suggest you to use flow, and if you are okay with business rule the after update BR would be right choice.

If you use before BR then then you will create loop whole, lets say in a scenario update failed by other BR on that case problem will be created but it wont be mapped to incident.

 

After BR code you case use like below.

var gr = new GlideRecord('problem');
gr.initialize();
gr.short_description = current.short_description;
gr.cmdb_ci = current.cmdb_ci;
var probRec = gr.insert();

if (probRec) {
       // Connect the problem record back to this incident
    current.setWorkflow(false); // Prevent triggering unnecessary workflows
    current.setValue('problem_id', probRec);
    current.update();
} else {
    gs.log('Failed to create Problem record.');
}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------