Regarding variable validation

SNOW24
Tera Contributor

Hi all,

I have created one record, in a record Producer I have created one variable requested for, I want to make the requested for read only when requested for user belongs to only one location. Can someone please help me? and i want this requirement to be achieved by using script include. please provide as early as possible.

 

Thanks and Regards

 

1 ACCEPTED SOLUTION

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @SNOW24 ,

You have to use a script include and an onchnage client script as below:

 

Client script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
        return;
   }

    var ga = new GlideAjax('asu_GetLocationData');
    ga.addParam('sysparm_name', 'getlocation');
    ga.addParam('sysparm_user', g_form.getValue("requested_for"));
    ga.getXMLAnswer(updateCampus);
}

function updateCampus(answer) {
    var clearvalue;
    if (answer == 'true'){
g_form.setReadOnly('requested_for',true);
} 
    else {
       g_form.setReadOnly('requested_for',false);
    }
}

 

Script include:

var asu_GetLocationData = Class.create();
asu_GetLocationData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getlocation: function () {
var user = this.getParameter('sysparm_user');
    var loc = new GlideRecord('sys_user');
loc.addQuery('sys_id',user);
loc.addQuery('location','your location sys_id');
    if (loc.next()) {
        return 'true';
      }
    else {    
    return 'false';
}

});

 

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

View solution in original post

3 REPLIES 3

Samaksh Wani
Giga Sage
Giga Sage

Hello @SNOW24 

 

 

var gr = new GlideRecord("sys_user");

var count =0;

gr.query();

while(gr.next()) {

if (gr.location) {

count++;

}

}

if(count==1){
return true;
}

 

 

Use Client Script and check if true is coming from script include,

 

Make it readonly.

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it helpful.

 

Regards,

Samaksh

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @SNOW24 ,

You have to use a script include and an onchnage client script as below:

 

Client script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
        return;
   }

    var ga = new GlideAjax('asu_GetLocationData');
    ga.addParam('sysparm_name', 'getlocation');
    ga.addParam('sysparm_user', g_form.getValue("requested_for"));
    ga.getXMLAnswer(updateCampus);
}

function updateCampus(answer) {
    var clearvalue;
    if (answer == 'true'){
g_form.setReadOnly('requested_for',true);
} 
    else {
       g_form.setReadOnly('requested_for',false);
    }
}

 

Script include:

var asu_GetLocationData = Class.create();
asu_GetLocationData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getlocation: function () {
var user = this.getParameter('sysparm_user');
    var loc = new GlideRecord('sys_user');
loc.addQuery('sys_id',user);
loc.addQuery('location','your location sys_id');
    if (loc.next()) {
        return 'true';
      }
    else {    
    return 'false';
}

});

 

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

Hello @SNOW24 ,

if my answer helped you please mark it as correct.

 

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.