If opened_by is the same as request for display message

New user1212
Tera Contributor

Hey,
I'm working on a client script for a catalog item that will check if the request being created was created by the same person for whom access is being granted - if so, a message should be displayed that this is not appropriate for this item.

Unfortunately, I have a problem with the script, because it either completely blocks the possibility of changing the person for whom the request is being created or it doesn't drop out at all. Any ideas?

 

(function() {
var openedBy = g_form.getValue('opened_by');
var requestedFor = g_form.getValue('requested_for');

if (openedBy == requestedFor) {
g_form.showFieldMsg('opened_by', 'End users must not request access for themselves.', 'error');
}
})();
7 REPLIES 7

J Siva
Tera Sage

Hi @New user1212 
May i know where you've written your script?

I've created the On load client script on Requested item table and it's working as expected. PFB

function onLoad() {
    //Type appropriate comment here, and begin script below
    if (g_form.getValue('opened_by') == g_form.getValue('requested_for')) {
        g_form.showFieldMsg('opened_by', 'End users must not request access for themselves.', 'error');
    }

}

JSiva_0-1745925538559.png

Output:

JSiva_1-1745925551251.png

 

Regards,
Siva

 

but this is created for all records from the Requested item table.
I need it to work only for two catalog items and for the message to appear on the Service Portal when we click the Request option in this window.

Newuser1212_0-1745929469416.png

 


I thought that adding this catalog client script in the relateded links of a given catalog item would work.

Newuser1212_1-1745929764025.png

 

@New user1212 I don't think you can access requested_for and opened by from the catalog client script. Because those fields will be populated only after the request submission.

In your case, change the request method as submit and create one new requested for type  variable in the catalog item.

Screenshot_20250429-183553.png

Screenshot_20250429-184346.png

Then create one on submit catalog item to validate the requested for and current logged in user.

Screenshot_20250429-185111.png

 

function onSubmit() {
//Type appropriate comment here, and begin script below
var req = g_form.getValue('requested_for');
var current_user = g_user.userID;
if (req == current_user) {
g_form.addErrorMessage("Invalid user");
return false;
}

}

 

Screenshot_20250429-185225.png

 

Hope this helps.

Regards,

Siva

@New user1212 

So you want validation to run on Order Confirmation modal?

if yes then it's not possible there to run any catalog client script.

what's your actual business requirement?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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