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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Have you considered a more no code solution for this? 
- 2023-02-13 - Article - Auto-populate a variable based on a reference type variable (Utah)

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Wow, This is very interesting! Learning something new everyday.

Mark Manders
Mega Patron

Although I am all for trying to get things to work, especially when you are trying things you just learned, why not use the 'auto populate' tab on the variable? Client scripts are less performant than using this OOB functionality.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark