validate requested for and current logged in user to allow request submission

shyam34
Tera Contributor

Hello All,

I have requirement to abort Request submission when we see "Requested for" and "current logged IN user" are not same , we need to do this because there is an variable to acknowledge terms & conditions and this has to be done by requested for only so we need to avoid on behalf of requests 

Can you please guide/help with Script to achieve this !!

13 REPLIES 13

Saurav11
Kilo Patron
Kilo Patron

Hello. 

 

You can write the below onsubmit client script on the catalog item with the below code

 

If(g_user.userID()==g_form.getValue('requested_for')

{

return true;

}

else

{

return false;

}

 

Please mark my answer as correct based on impact

Hello @shyam34 

 

I am assuming you have the order now button n the service portal. Also I hope the requested_for field is a reference to user table.

 

Please do the below:-

 

Create a script include of name GSS_GetCategory1 and should be client callable:-

 

Saurav11_0-1668611848351.png

 

use the below code in script include:-

 

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

    getCategories: function() {
        var requestedfor = this.getParameter('sysparm_internal_service_name');
        var loggedin = gs.getUserID();
        if (requestedfor == loggedin) {
            return true;
        } else {
            return false;
        }
    },

    type: 'GSS_GetCategory1'
});

 

Now in the client script use the below code. Incase if the variable name is something else apart from requested_for just change that name and apart from that everything remains the same

 

function onSubmit() {
if (g_scratchpad._ajaxChecked) {
    // We have run our Ajax Checks, so we can continue on
    // and let our form submission continue
    g_scratchpad._ajaxChecked = null;
    return true;
  }
  // use this line below if you want to store the specific action name
  g_scratchpad._action = g_form.getActionName();
  g_scratchpad._ajaxChecked = false;
  var ga = new GlideAjax('GSS_GetCategory1');
  ga.addParam('sysparm_name', 'getCategories');
  ga.addParam('sysparm_internal_service_name', g_form.getValue('requested_for'));
  ga.getXMLAnswer(function(answer) {
    // I made this a simple check of a true/false result
    // but you can change this to check whatever suits your business case
    // and base it on what gets returned from your script include
    if (answer == "false") {
      // we didn't pass our checks, so alert the user and quit
      alert("didn't work");
      return;
    }
    // it worked! now we can resubmit the form with the 
    // property in place to allow us to continue
    // so once we resubmit, it will re-run this function but will return true
    // at line 5 of this script
    g_scratchpad._ajaxChecked = true;
    if (typeof g_form.orderNow != 'undefined') {
      // this is a catalog item
      g_form.orderNow();
    } else {
      // this will resubmit the form using the saved 
      // ui action that was originally clicked
      g_form.submit(g_scratchpad._action);
    }
  });
  // always return false if we get to this point
  return false;
 }

 

 

Please mark my answer as correct based on Impact.

Muhammad Khan
Mega Sage
Mega Sage

I would say, make that Requested For variable as read only and set a default value of currently logged in user.

 

As far as the script is related you can utilize onSubmit() Catalog Client Script to achieve this requirement. Script logic would look something like below.

 

if(g_form.getValue('<backend_name_of_your_requested_for_variable>') != g_user.userID){
        alert('Requested For can only be logged in user!');
        return false;
}

 

 

Aman Kumar S
Kilo Patron

Hi,

You can use the GlideUser(g_user) class for taking this up!

Create an onSubmit catalog client script.

Script as:

 

If(g_user.userID == g_form.getValue('requested_for')){

    return true;

} else{

    return false;

}

 

Best Regards
Aman Kumar