when Opened by and logged in user are not same user should not submit the catalog form.

Vinod S Patil
Tera Contributor

Hello Team

My requirement is opened by and logged in user are not same, catalog item should not submit the request and it should show some error.

This has to be done using on submit client script. Please help me.
@Dr Atul G- LNG 
@Anil Lande 
@Ankur Bawiskar 

1 ACCEPTED SOLUTION

Aniket Chavan
Tera Sage
Tera Sage

Hello @Vinod S Patil ,

I didn't understand the term you referred to as 'opened By' because, as @Maik Skoddow mentioned, it will be populated once the item has been submitted.

So If you're referring to the variable named "opened by" in your form and want to ensure it's the same as the logged-in user, you can achieve this through an onSubmit client script. Alternatively, you might consider making the variable read-only and auto-populating it based on the logged-in user.

Here's an example of an onSubmit client script for validation:

 

function onSubmit() {
    var loggedinUser = g_user.userID;
    var openedBy = g_form.getValue('opened_by');

    if (openedBy == loggedinUser) {
        //g_form.showFieldMsg('opened_by', 'The Opened By user is the same as the Logged In user.', 'info');
        return true;
    } else {
        alert("Please make sure that Opened By and Logged In user should be the same.");
        return false;
    }
}

 


Also you can refer the below link as well:

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,
Aniket

View solution in original post

5 REPLIES 5

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Vinod S Patil ,

 

U need to have one on submit client script & a script include in order to achieve this

 

Client Script:

 

function onSubmit() {

    var openedBy = g_form.getValue('opened_by');

 

    // Call GlideAjax to get sys_id of the logged-in user

    var ga = new GlideAjax('GetLoggedInUserID');

    ga.addParam('sysparm_name', 'getLoggedInUserSysID');

    ga.getXMLAnswer(function(response) {

        var loggedInUserSysID = response;

 

        // Compare "Opened By" with the sys_id of the logged-in user

        if (openedBy !== loggedInUserSysID) {

            // Display an error message

            alert('Error: Opened By and Logged In User must be the same.');

 

            // Prevent form submission

            return false;

        }

 

        // Continue with form submission

        return true;

    });

 

    // Ensure synchronous execution

    return false;

}

 

Script Include:(Client callable true)

 

// GetLoggedInUserID.js

var GetLoggedInUserID = Class.create();

 

GetLoggedInUserID.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getLoggedInUserSysID: function() {

        var userID = gs.getUserID();

        return userID;

 

  }

});

 

Thanks,

Danish

 

Maik Skoddow
Tera Patron
Tera Patron

Hi @Vinod S Patil 

your questions don't seem to make sense, as "Opened by" is populated on the requested items only. There is no field "Opened by" on the catalog item. So what exactly are you referring to?

Maik

Anil Lande
Kilo Patron

Hi @Vinod S Patil 

When any user submits a request opened by is set as logged in User.

Not sure why you have this requirement.

If you have different variables like requested for the you can use onSubmit client script and compare that variable with logged in user sys_id. If not same then return false.

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Aniket Chavan
Tera Sage
Tera Sage

Hello @Vinod S Patil ,

I didn't understand the term you referred to as 'opened By' because, as @Maik Skoddow mentioned, it will be populated once the item has been submitted.

So If you're referring to the variable named "opened by" in your form and want to ensure it's the same as the logged-in user, you can achieve this through an onSubmit client script. Alternatively, you might consider making the variable read-only and auto-populating it based on the logged-in user.

Here's an example of an onSubmit client script for validation:

 

function onSubmit() {
    var loggedinUser = g_user.userID;
    var openedBy = g_form.getValue('opened_by');

    if (openedBy == loggedinUser) {
        //g_form.showFieldMsg('opened_by', 'The Opened By user is the same as the Logged In user.', 'info');
        return true;
    } else {
        alert("Please make sure that Opened By and Logged In user should be the same.");
        return false;
    }
}

 


Also you can refer the below link as well:

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,
Aniket