The CreatorCon Call for Content is officially open! Get started here.

Ho do you use GlideAjax in Catalog Clint Script?

aryanjain27
Tera Contributor

Hey,

 

I've recently learned about GlideAjax and was using it in the catalog clint script but couldn't make it to work. I have a table 'x_accounts' that has columns u_account_id and u_account_alias. I have a reference variable in the catalog item that is referencing this table via account_alias and depending upon what account is selected I want to account_id to be shown on the catalog item. So I have another read only variable for account_id whose value I am setting in catalog clint script. I used glideAjax to get the account_id from the table but its not working. Is it implemented correctly?

 

Catalog Clint Script:

onChange is applied on the account_alias variable on the catalog item.

 

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

    // Perform a GlideAjax call to fetch the id based on the selected name
    var ga = new GlideAjax('D_Ajax');
	ga.addParam('sysparm_name','getAccountId');
    ga.addParam('sysparm_accountSysId', newValue);
	ga.getXMLAnswer(function(answer){
		var response = JSON.parse(answer);
		g_form.setValue('account_id', response.accountId);
	});
}

 

 

Script-Includes:

 

var D_Ajax = Class.create();
D_Ajax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getAccountId: function() {
        var account_sys_id = this.getParameter("sysparm_accountSysId");
        var gr = new GlideRecord("x_accounts");
        gr.get(account_sys_id)
        accountId = gr.u_account_id;

	    var response = {};
	    response.accountId = 'Test'; // Debugging
        return JSON.stringify(response);
    },
	type: 'D_Ajax'
});

 

 To debug, I set the response static to verify if the issue is with glide record. But even after that I do not see the text 'Test' on the account_id variable in the catalog item.

 

Any help would be appreciated.

 

Thanks!

1 ACCEPTED SOLUTION

Satishkumar B
Giga Sage
Giga Sage

Updated Client Script:

 

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

// Perform a GlideAjax call to fetch the id based on the selected name
var ga = new GlideAjax('D_Ajax');
ga.addParam('sysparm_name', 'getAccountId');
ga.addParam('sysparm_accountSysId', newValue);
ga.getXMLAnswer(function(answer) {
console.log("Answer received: " + answer); // Debugging
var response = JSON.parse(answer);
console.log("Parsed response: " + JSON.stringify(response)); // Debugging
g_form.setValue('account_id', response.accountId);
});
}

Script Include:

Your Script Include looks good, but ensure that it is set to `Client Callable`. Also, confirm that the table and field names are correct.

-- Updated Script Include:


var D_Ajax = Class.create();
D_Ajax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getAccountId: function() {
var account_sys_id = this.getParameter("sysparm_accountSysId");
var gr = new GlideRecord("x_accounts");
if (gr.get(account_sys_id)) {
var accountId = gr.u_account_id;

var response = {};
response.accountId = accountId;
return JSON.stringify(response);
} else {
var response = {};
response.accountId = 'Not found';
return JSON.stringify(response);
}
},
type: 'D_Ajax'
});

Troubleshooting Steps:

1. Verify Script Include is Client Callable:
- Go to your Script Include and ensure the `Client Callable` checkbox is checked.

2. Confirm Correct Variable Names:
- Ensure that the variable names in your catalog item match the names used in the client script.

3. Debugging:
- Add `console.log` statements in your client script to verify the flow and the responses.

4. ServiceNow Logs:
- Check the ServiceNow logs for any errors related to the `D_Ajax` Script Include.

 

#####################################################
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

 

View solution in original post

7 REPLIES 7

Nisha15
Mega Sage
Mega Sage

Hi @aryanjain27 ,

can you try the below codes:
script include

var D_Ajax = Class.create();
D_Ajax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getAccountId: function() {
        var accountSysId = this.getParameter('sysparm_accountSysId');
        var gr = new GlideRecord('x_accounts');
        if (gr.get(accountSysId)) {
            var accountId = gr.getValue('u_account_id');
            return accountId; 
        } else {
            return 'No Account Found';
        }
    },

    type: 'D_Ajax'
});


Client script:

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

    // Perform a GlideAjax call to fetch the id based on the selected name
    var ga = new GlideAjax('D_Ajax');
    ga.addParam('sysparm_name', 'getAccountId');
    ga.addParam('sysparm_accountSysId', newValue);
    ga.getXMLAnswer(function(answer) {
        g_form.setValue('account_id', answer); 
    });
}

Hi,

Its not working still. I replaced answer  to newValue in catalog clint script to verify that variables are set correctly. And I do see the account sys_id changing in the account_id variable when selecting the account. But the account_id is not populating.

 

g_form.setValue('account_id', answer); 
// to
g_form.setValue('account_id', newValue); 

 

 

Satishkumar B
Giga Sage
Giga Sage

Updated Client Script:

 

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

// Perform a GlideAjax call to fetch the id based on the selected name
var ga = new GlideAjax('D_Ajax');
ga.addParam('sysparm_name', 'getAccountId');
ga.addParam('sysparm_accountSysId', newValue);
ga.getXMLAnswer(function(answer) {
console.log("Answer received: " + answer); // Debugging
var response = JSON.parse(answer);
console.log("Parsed response: " + JSON.stringify(response)); // Debugging
g_form.setValue('account_id', response.accountId);
});
}

Script Include:

Your Script Include looks good, but ensure that it is set to `Client Callable`. Also, confirm that the table and field names are correct.

-- Updated Script Include:


var D_Ajax = Class.create();
D_Ajax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getAccountId: function() {
var account_sys_id = this.getParameter("sysparm_accountSysId");
var gr = new GlideRecord("x_accounts");
if (gr.get(account_sys_id)) {
var accountId = gr.u_account_id;

var response = {};
response.accountId = accountId;
return JSON.stringify(response);
} else {
var response = {};
response.accountId = 'Not found';
return JSON.stringify(response);
}
},
type: 'D_Ajax'
});

Troubleshooting Steps:

1. Verify Script Include is Client Callable:
- Go to your Script Include and ensure the `Client Callable` checkbox is checked.

2. Confirm Correct Variable Names:
- Ensure that the variable names in your catalog item match the names used in the client script.

3. Debugging:
- Add `console.log` statements in your client script to verify the flow and the responses.

4. ServiceNow Logs:
- Check the ServiceNow logs for any errors related to the `D_Ajax` Script Include.

 

#####################################################
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

 

Clint Callable, that was it! Silly me, It works now! Thanx a lot!