- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2023 10:58 PM
I have created one business rule which creates incident automatically when case submitted for one subcategory. But i need to handle exception handling in that business rule. Can anyone help me how to achieve this?
Business rule: is
When to Run : After insert
Script is below :
(function executeRule(current, previous /*null when async*/) {
ProductSecurity();
})(current, previous);
function ProductSecurity(){
var psInc=new GlideRecord("incident");
psInc.initialize();
psInc.short_description=current.short_description;
psInc.description=current.description;
psInc.caller_id=current.contact;
psInc.u_asset=current.asset;
psInc.u_account=current.account;
psInc.u_product=current.product;
current.impact='1';
current.urgency='2';
psInc.contact_type=current.contact_type;
psInc.contract=current.contract;
psInc.u_entitlement=current.entitlement;
psInc.u_ci_service_level=current.u_ci_service_level;
psInc.category=current.category;
psInc.subcategory=current.subcategory;
psInc.insert();
gs.addInfoMessage(gs.getMessage('Product Security Incident - ' + psInc.number + ' is created'));
current.incident=psInc.sys_id;
current.update();
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2023 11:03 PM
Hi @Rajesh Sheela1 ,
This article should help you : https://www.servicenow.com/community/developer-articles/before-business-rules-what-happens-if-you-cr...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2023 11:11 PM - edited 01-02-2023 11:12 PM
Hi @Rajesh Sheela1 ,
Wrapping all code in try/catch blocks or move your code to script include and use try/catch blocks
Example:
try{
// Put code here in case something goes wrong.
} catch(e){
//Use any one of the below methods to log the error message.
gs.error(e.message);
gs.addErrorMessage('Exception throw while biting apple:'+e.message);
gs.addInfoMessage('Name of module where error happened:'+ e.message);
}
If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!
Abhit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2023 11:17 PM
Hello @Rajesh Sheela1
If you want exception handling then you can use the try(), catch() block as shown in below script:
Also instead of After Insert BR try once with Async Insert BR. Because it is not recommended to use current.update() in After BR.
In your code since you have created the function ProductSecurity() outside the function executeRule() and you have not passed any parameter while calling the function ProductSecurity() the current and previous object will not be accessible in ProductSecurity() function.
(function executeRule(current, previous /*null when async*/) {
try {
var psInc=new GlideRecord("incident");
psInc.initialize();
psInc.short_description=current.short_description;
psInc.description=current.description;
psInc.caller_id=current.contact;
psInc.u_asset=current.asset;
psInc.u_account=current.account;
psInc.u_product=current.product;
psInc.contact_type=current.contact_type;
psInc.contract=current.contract;
psInc.u_entitlement=current.entitlement;
psInc.u_ci_service_level=current.u_ci_service_level;
psInc.category=current.category;
psInc.subcategory=current.subcategory;
var psIncID = psInc.insert();
if (!psIncID) {
gs.addErrorMessage("Product Security Incident is not created");
} else {
gs.addInfoMessage(gs.getMessage('Product Security Incident - ' + psInc.number + ' is created'));
current.impact='1';
current.urgency='2';
current.incident=psInc.sys_id;
current.update();
}
} catch(e) {
gs.addErrorMessage(e);
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2023 11:03 PM
Hi @Rajesh Sheela1 ,
This article should help you : https://www.servicenow.com/community/developer-articles/before-business-rules-what-happens-if-you-cr...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2023 11:11 PM - edited 01-02-2023 11:12 PM
Hi @Rajesh Sheela1 ,
Wrapping all code in try/catch blocks or move your code to script include and use try/catch blocks
Example:
try{
// Put code here in case something goes wrong.
} catch(e){
//Use any one of the below methods to log the error message.
gs.error(e.message);
gs.addErrorMessage('Exception throw while biting apple:'+e.message);
gs.addInfoMessage('Name of module where error happened:'+ e.message);
}
If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!
Abhit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2023 11:17 PM
Hello @Rajesh Sheela1
If you want exception handling then you can use the try(), catch() block as shown in below script:
Also instead of After Insert BR try once with Async Insert BR. Because it is not recommended to use current.update() in After BR.
In your code since you have created the function ProductSecurity() outside the function executeRule() and you have not passed any parameter while calling the function ProductSecurity() the current and previous object will not be accessible in ProductSecurity() function.
(function executeRule(current, previous /*null when async*/) {
try {
var psInc=new GlideRecord("incident");
psInc.initialize();
psInc.short_description=current.short_description;
psInc.description=current.description;
psInc.caller_id=current.contact;
psInc.u_asset=current.asset;
psInc.u_account=current.account;
psInc.u_product=current.product;
psInc.contact_type=current.contact_type;
psInc.contract=current.contract;
psInc.u_entitlement=current.entitlement;
psInc.u_ci_service_level=current.u_ci_service_level;
psInc.category=current.category;
psInc.subcategory=current.subcategory;
var psIncID = psInc.insert();
if (!psIncID) {
gs.addErrorMessage("Product Security Incident is not created");
} else {
gs.addInfoMessage(gs.getMessage('Product Security Incident - ' + psInc.number + ' is created'));
current.impact='1';
current.urgency='2';
current.incident=psInc.sys_id;
current.update();
}
} catch(e) {
gs.addErrorMessage(e);
}
})(current, previous);