Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Best practice to validate “Requested for” user (VIP check) before catalog submission – Portal & Nati

nameisnani
Mega Sage
I have a Service Catalog Item where users can request to enable VIP flag for a user.
 

Requirement

Before the catalog form is submitted:

  • Check if the Requested for user is already marked as VIP
  • If yes:
    • Show a clear, user‑friendly message (preferably with the user’s name)
    • Prevent the catalog request from being submitted
  • This validation must work in:
    • Service Portal
    • Native UI

 

  • What is the recommended layer to handle this kind of pre‑submission validation?
  • Is there a preferred pattern to balance performance and data integrity?
  • Any known gotchas when validating catalog submissions in Service Portal?

 

 

PLEASE provide script for this .

 

NOTE for Partculr catalog item 

 

Requestor for field comes from variable set 

 

Requested For

  • Label: Requested for
  • Backend name:
requested_for
4 REPLIES 4

Nishant_Shelar
Mega Guru

Use a Catalog Client Script (onSubmit) + GlideAjax (server check)

  • Works in Service Portal
  • Works in Native UI
  • Prevents submission (UI level)
  • Uses server-side validation 

 

onSubmit Client Script

function onSubmit() {

var user = g_form.getValue('requested_for');

 

var ga = new GlideAjax('CheckVIPUser');
ga.addParam('sysparm_name', 'isUserVIP');
ga.addParam('sysparm_user_id', user);

ga.getXMLWait();

var response = ga.getAnswer();

if (response == 'true') {
g_form.addErrorMessage("This user is already marked as VIP.");
return false;
}

return true;
}

 

Client Callable Script Include

var CheckVIPUser = Class.create();
CheckVIPUser.prototype = Object.extendsObject(AbstractAjaxProcessor, {

isUserVIP: function() {
var userId = this.getParameter('sysparm_user_id');

var gr = new GlideRecord('sys_user');
if (gr.get(userId) && gr.vip == true) {
return 'true';
}

return 'false';
}

});

 

 

Ankur Bawiskar
Tera Patron

@nameisnani 

Better to show that catalog item to only Non VIP users and hide for VIP Users

You can create user criteria with script 

Then Add this in "Available For" related list for that item

var gr = new GlideRecord("sys_user"); gr.addQuery("sys_id", user_id); gr.addQuery("vip=false"); gr.query(); answer = gr.hasNext();

User criteria for Service Portal 

var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", user_id);
gr.addQuery("vip=false");
gr.query();
answer = gr.hasNext();

55.png

💡 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

@nameisnani 

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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

TausifAhmedS
Tera Contributor

Hi @nameisnani ,

 

 

I think my BLOG may be helpful for your requirement onSubmit/ On Submission Validation understanding -> OnSubmit Validation for native ui, workspace and portal.

 

And using GlideRecord() in the client script is not a good practice and also the getXMLWait() won't work in the service portal but works in native ui, you may go through the above article mentioned.

 

Script for your requirement

Client Script:

function onSubmit() {

    // Prevent recursion
    if (g_scratchpad.vipCheckDone)
        return true;

    var requestedFor = g_form.getValue('requested_for');

    if (!requestedFor)
        return true;

    var ga = new GlideAjax('VIPValidation');
    ga.addParam('sysparm_name', 'checkVIP');
    ga.addParam('sysparm_user', requestedFor);

    ga.getXMLAnswer(function(response) {

        // If VIP exists → response will be username
        if (response && response !== 'false') {

            g_form.addErrorMessage(
                'The user "' + response + '" is already marked as VIP. Request cannot be submitted.'
            );

            return;
        }

        // Mark validation complete
        g_scratchpad.vipCheckDone = true;

        // Re-submit form
        g_form.submit(g_form.getActionName());
    });

    return false;
}

 

Server Code:

    checkVIP: function () {

        var userId = this.getParameter('sysparm_user');

        if (!userId)
            return 'false';

        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userId)) {

            // assuming field is 'vip'
            if (userGR.vip == true) {
                return userGR.getDisplayValue(); // return user name
            }
        }

        return 'false';
    }

you can make the required changes to the codes.

 

If my response meets your requirement mark it as helpful👍 and accept it as solution .

Regards,

Tausif.