Auto populate user details

Shabbir1
Tera Contributor

Hi Team,

 

We have two variables on catalog item 1.Requested for(reference to user table) 2 VIP customer(check box). Requirement is when anyone changes the name in requested for variable if that user is VIP customer we have to show VIP customer check box as true...in user table we have a field VIP customer can anyone help me in the on change client script or any other possible way

 

Regards

Shabbir

1 ACCEPTED SOLUTION

Vrushali  Kolte
Mega Sage

Hello @Shabbir1 ,

 

You can create onChange client script on requested_for variable as below -

 

VrushaliKolte_0-1720499537681.png

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
//please replace the field names as per your use case
    var userDetails = g_form.getReference('requested_for'); 
    if (userDetails.VIPfield == 'true') {
        g_form.setValue('vip_checkbox', true);
    }
    //Type appropriate comment here, and begin script below

}

If my answer solves your issue, please mark it as Accepted ✔️and Helpful 👍 based on the impact.

View solution in original post

5 REPLIES 5

Vrushali  Kolte
Mega Sage

Hello @Shabbir1 ,

 

You can create onChange client script on requested_for variable as below -

 

VrushaliKolte_0-1720499537681.png

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
//please replace the field names as per your use case
    var userDetails = g_form.getReference('requested_for'); 
    if (userDetails.VIPfield == 'true') {
        g_form.setValue('vip_checkbox', true);
    }
    //Type appropriate comment here, and begin script below

}

If my answer solves your issue, please mark it as Accepted ✔️and Helpful 👍 based on the impact.

Thank you so much @Vrushali Kolte  it is working fine 

SN_Learn
Kilo Patron
Kilo Patron

Hi @Shabbir1 ,

 

Please try the below, tested and working in PDI

 

Onchange Catalog Client script on 'requested_by' field:

 

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

    g_form.getReference('requested_by', setVip);

    function setVip(openedFor) {
        if (openedFor.vip == 'true') {
            g_form.setValue('checkbox2', true); //replace checkbox2 with the VIP checkbox backend name field
        } else {
            g_form.setValue('checkbox2', false); //replace checkbox2  with the VIP checkbox backend name field
        }
    }
}

 

SN_Learn_2-1720504883525.png

 

 

Result:

 

VIP User selected:

SN_Learn_0-1720504784506.png

 

 

Not a VIP User:

SN_Learn_1-1720504833533.png

 

 

Mark this as Helpful / Accept the Solution if this helps

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Rupanjani
Giga Guru

Hi @Shabbir1,

 

We can achieve this in two ways:

1. getReference

2. GlideAjax 

 

The best practice is to use GlideAjax instead of getReference, as it can cause the user interface to freeze while the call is being completed, which can result in a poor user experience.

 

onChange Client Script on Requested For:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ga = new GlideAjax('Test'); //script include name
    ga.addParam('sysparm_name', 'getVIPdetails'); //second parameter - function name in the SI
    ga.addParam('sysparm_user', newValue);
    ga.getXML(getData1);

    function getData1(response) {
        var answer1 = response.responseXML.documentElement.getAttribute('answer');
        if (answer1 == 'true') {
            g_form.setValue('<vip_field_name', true);
        } else {
             g_form.setValue('<vip_field_name', false);
        }
    }
    }

 

 

Script Include (make sure the Client callable checkbox is set to true):

 

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

	getVIPdetails: function(){
		try{
			var grUser = new GlideRecord('sys_user');
			var flag = false;
			if(grUser.get(this.getParameter('sysparm_user'))){
				flag = grUser.vip_field; //replace vip_field with vip_field_name
			}
			return flag;
		} catch (ex) {
            gs.info('Error:' + ex.string() + '\nLine:' + ex.lineNumber);
        }
	},

    type: 'Test'
});

 

 


Mark the response correct and helpful if the answer assisted your question.