Issue with the Business rule , how can I fix the code ?

Pooja Khatri
Tera Contributor

I have a requirement where , I need to prevent an API from being able to have the calculated priority be Sev0.

and it will be done if we implement the below logic

 

In a single payload, Impact cannot be 1 - High (value = 1) and Urgency cannot be High (value = 1) at the same time.

If Incident Impact is already set to 1 - High and Urgency is set to Medium High or Normal, do not allow the payload to set Urgency to High

If Incident Urgency is already set to High and Impact is set to something other than 1 - High , do not allow the payload to set Impact to 1 - High

 

I have written the below Business Rule - Before "Insert and Update"

 

(function executeRule(current, previous /*, gs*/) {
// Check if Impact and Urgency cannot be set to High simultaneously
if (current.impact == 1 && current.urgency == 1) {
gs.addErrorMessage("Cannot set Impact and Urgency to High simultaneously.");
return;
}

// Check if Impact is already set to High and Urgency is Medium High or Normal
if (current.impact == 1 && (current.urgency == 2 || current.urgency == 3)) {
gs.addErrorMessage("Cannot set Urgency to High when Impact is already High.");
return;
}

// Check if Urgency is already set to High and Impact is not High
if (current.urgency == 1 && current.impact != 1) {
gs.addErrorMessage("Cannot set Impact to High when Urgency is already High.");
return;
}

})(current, previous);

 

But the script is not working and I am not getting expected output on the API side , How can I fix the script ?

14 REPLIES 14

Hi @kkrushkov - which details should I share from my end to fix the issue ?

Shiva1999
Tera Contributor

Hi @Pooja Khatri ,

 

I have tried above code and it is working.

(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here
    if (current.impact == 1 && current.urgency == 1) {
        gs.addErrorMessage("Cannot set Impact and Urgency to High simultaneously.");
        return;
    }
})(current, previous);

 

Thank you

Hi @Shiva1999 -  I tried with the script , I tried scenario where impact and urgency are set to 1 . 

It should have restrict the api from creating incident (POST) but I was able to create incident through API.

SN_Learn
Kilo Patron
Kilo Patron

Hi @Pooja Khatri ,

 

Please try the below code:

(function executeRule(current, previous /*, gs*/) {
// Check if Impact and Urgency cannot be set to High simultaneously
if (current.impact == 1 && current.urgency == 1) {
current.setAbortAction(true);
gs.addErrorMessage("Cannot set Impact and Urgency to High simultaneously.");
}

// Check if Impact is already set to High and Urgency is Medium High or Normal
if (current.impact == 1 && (current.urgency == 2 || current.urgency == 3)) {
current.setAbortAction(true);
gs.addErrorMessage("Cannot set Urgency to High when Impact is already High.");
}

// Check if Urgency is already set to High and Impact is not High
if (current.urgency == 1 && current.impact != 1) {
current.setAbortAction(true);
gs.addErrorMessage("Cannot set Impact to High when Urgency is already High.");
}

})(current, previous);

 

Please mark this accepted/helpful, if applicable.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Hi @SN_Learn - I tried with the script , I tried scenario where impact and urgency are set to 1 . 

It should have restrict the api from creating incident (POST) but I was able to create incident through API.