Unable to display error message with gs.addErrorMessage() or gs.addErrorMessage(gs.getMessage())

Jiajie Li
Tera Contributor

Hello experts,

 

I am trying to prevent duplicate software entitlements. If a duplicate is found, I want the request to abort and an error to show up. However, the error is not showing up. I tried both below:

gs.addErrorMessage(gs.getMessage('Duplicate software entitlement found'));//display error message
gs.addErrorMessage("Duplicate software entitlement found");//display error message

 

Below is the full business rule script:

 

(function executeRule(current, previous /*null when async*/) {
    // Check for existing entitlement with the same software and licensee
    var gr = new GlideRecord('alm_entitlement'); //create a new GlideRecord to query "alm_entitlement" table
    gr.addQuery('assigned_to', current.assigned_to); //query the assigned_to field
    gr.addQuery('licensed_by', current.licensed_by); //query the licensed_by field
    gr.query();

    if (gr.hasNext()) { //checks if there is at least one record in the result set of the GlideRecord query. 
    //If there is a matching record, the hasNext() returns true, and the code inside the if statement is run.
        gs.addErrorMessage(gs.getMessage('Duplicate software entitlement found'));//display error message
        gs.addErrorMessage("Duplicate software entitlement found");//display error message
        current.setAbortAction(true); //abort process
    }
})(current, previous);
1 ACCEPTED SOLUTION

I would suggest to update the RITM with additional comments to update the user.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

10 REPLIES 10

Empty condition is fine, then it runs at every update/insert.

 

Are you expecting this to be displayed when Flow Designer is updating/creating the records?
That is not going to work since flows run in their own context, not in your session.
The message should display when you update/insert it manually though.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

ah I was expecting the error to display when I select Update or Close on an SCTASK. Is there any way to get an error to show when selecting those options? I'd like the user to be notified that the process was aborted and not leave them confused. 

I would suggest to update the RITM with additional comments to update the user.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Thanks! I'll do that. 

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @Jiajie Li ,

Please make sure you are running your business rule on before. So that it will abort the submission of record before the record is inserted.

 

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.