exception handling in business rules

Rajesh Sheela1
Tera Contributor

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 :

 

RajeshSheela1_0-1672729081781.png

 

 

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

 

 

3 ACCEPTED SOLUTIONS

Abhit
Tera Guru

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

View solution in original post

Mahendra RC
Mega Sage

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

 

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

Abhit
Tera Guru

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

Mahendra RC
Mega Sage

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