How to add message in API Call

Sruthi_2511
Tera Contributor

Here is the requirement to add error message for "Change Registration" Modal.  (which is used by API users) alone

Example:

Close code: Successful

State: Cancel (4 )----error message should be shown in API users alone : "Change Registration Modal cannot be cancelled when the close code is "Successful",it must be "Unsuccessful" 

7 REPLIES 7

Me Being Mustaq
Tera Guru

Hi @Sruthi_2511 

 

To add a custom error message specifically for API users during the "Change Registration" modal state transition validation, you need to modify the existing Business Rule ("Change Model: Check State Transition") on the change_request table to:

  • Detect if the operation is triggered by an API user (e.g., via gs.getUser() or checking the API context).

  • Check for the specific condition: Close code = "Successful" and State = "Cancel (4)".

  • If the condition matches and the user is an API user, abort the operation and throw the custom error message:
    "Change Registration Modal cannot be cancelled when the close code is 'Successful', it must be 'Unsuccessful'."

  1. Modify the Business Rule Script

In the Change Model: Check State Transition Business Rule script (Before Update or Before Insert on change_request),

add the following logic:

(function executeRule(current, previous /*null when async*/) {
// Check if the Close Code is 'Successful' and State is 'Cancel (4)'
if (current.close_code == 'Successful' && current.state == 4) { // Assuming state '4' means Cancel

// Check if the operation is triggered by API (you can customize this check as needed)
var user = gs.getUser();
var isAPIUser = user.getRoles().indexOf('api_user_role') !== -1; // Replace 'api_user_role' with your actual API user role

if (isAPIUser) {
// Throw custom error for API users
gs.addErrorMessage("Change Registration Modal cannot be cancelled when the close code is 'Successful', it must be 'Unsuccessful'");
current.setAbortAction(true);
return;
}

// Optionally, for non-API users, you can let the existing generic error remain or customize differently
}
})(current, previous);

 

  1. Identify API Users

  • Define which users count as API users by role or other criteria.

  • Replace 'api_user_role' with the actual role assigned to API users.

  • If your API users don't have a distinct role, consider checking the context of the call with gs.getSession().getClientData() or similar techniques.

  1. Test

  • Trigger the cancellation from an API user context with the close code "Successful".

  • Confirm the custom error message appears instead of the generic one.

  • Ensure non-API users still see the original or expected behavior.

By enhancing the current Business Rule script to detect API users and the specific state/close code condition, you can present a tailored error message during Change Registration modal operation aborting, improving clarity for API users without affecting others.

 

If it is helpful, please hit the thumbs button and accept the correct solution by referring to this solution in the future it will be helpful to them.

 

 

Thanks & Regards,

Mohammed Mustaq Shaik

Ankur Bawiskar
Tera Patron
Tera Patron

@Sruthi_2511 

the error message you are getting is coming from platform level since some business rule stopped the insert/update, it won't give you option to pass your custom error message

It will just tell which BR stopped/aborted the update.

if 3rd party team is using OOTB Table API then you can't customize the error messages.

If you want to send custom error messages then please use Scripted REST API which gives you full control on API response.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Sruthi_2511 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

The requirement is that..there are many integration running for this modal!

I have created scripted rest api as well..still the fields are not getting validated..how should i debug?