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

Peter Bodelier
Giga Sage

The script works fine in my instance.

 

When are you running your BR?


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

I am running BR on Before Insert and Update

That should be fine.

Is your BR triggered at all? If you add a gs.info('debug BR') line at the start of your script, do you see this is the system log? If not, please verify your conditions.


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

I added gs.info and it does run the BR

 

The following error does show up in the log:

Flow Designer: Operation(Deploy Workstation Request.If$9.1b44563b97c431502a7733471153afeb.624b63b5979c39502a7733471153af3e.f9e09916c31332002841b63b12d3aeaa) failed with error: com.snc.process_flow.exception.OpException: Error occured while inserting record: Operation against file 'alm_entitlement_user' was aborted by Business Rule 'Ensure no duplicate entitlements^dfee9e8a97fcbd102a7733471153af3b'. Business Rule Stack:Ensure no duplicate entitlements

 

I think this error is because of current.setAbortAction(true);

 

I also checked the conditions is empty. Is that the cause? I'm not sure what the condition should be. 

Capture.PNG