Script include response is null for manager == empty condition overall working as required.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 10:30 AM
//Catalog Client Script
function onLoad() {
var user = g_user.userID;
var manager;
//Call script include
var ga = new GlideAjax('global.CatalogClientUtils'); //Scriptinclude
ga.addParam('sysparm_name', 'CS_SetManagerToRequestedForsManager'); //Method
ga.addParam('userId', user); //Parameters
ga.getXMLAnswer(getResponse);
function getResponse(response) {
alert('response..' + response);
var res = response.split(',');
// var res = JSON.parse(response);
alert('res....' + res);
if (res[1] == false) { //not a VIP lets get manager approval
manager = res[0];
alert('manager..' + manager);
g_form.setValue("manager", manager);
//Located the manager set this person into the manager field on the form.
}
if (manager == ' ') { //manager is empty lets order a catalog item to get that updated.
g_form.hideFieldMsg('manager', 'true');
g_form.showFieldMsg('manager', 'Please have your manager information updated in');
} else {
//manager is a vip skip approval
g_form.setValue("vip", "Yes"); //set the vip variable for check
g_form.addInfoMessage('You have been identified as a VIP. As such no manager approval will occur for this request.');
g_form.setValue('manager', res.manager);
}
}
}
//Script Include-
type: 'CatalogClientUtils'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 10:48 AM
It's always best to initialize variables when you declare them, so try var manager = ''; with the same test case.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 11:56 AM - edited 08-23-2023 11:59 AM
I'd make a couple changes to be safe and make things easier for you:
1. Rename your Script Include
Your "CatalogClientUtils" name is a little too generic, especially since it is in the Global scope. What if ServiceNow created an OOB Script Include with the same name? I'd suggest you prefix all your Script Includes with your company's name or abbreviation. And I'd add "Ajax" to the end for any client-callable ones. It does't really matter, just makes things easier to see what is what.
So my preferred name would be "AcmeCatalogClientUtilsAjax".
2. Rename your method
First, the "CS_SetManagerToRequestedForsManager" name does not follow standard naming conventions. And it actually "gets" information, does not "set" anything.
Should be something like "getManagerToRequestedForsManager"
3. Always return a value
The logic in your Script Include does not always actually return something, so that is why you are getting null sometimes. It only "return"s if it finds the cart and manager.
4. Why are you looking at the Cart record?
Do you have a "Requested for" variable on the Catalog Item? There won't be a record unless someone adds the item to the Cart.
This post may help setting up your Script Include a bit better by using JSON to return multiple bits of info - TNT: Returning Data from GlideAjax Calls.
BTW, these posts might help when posting questions in the future:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 01:18 PM
Here's how I would do it using GlideAjax:
Script Include:
var AcmeCatalogClientUtilsAjax = Class.create();
AcmeCatalogClientUtilsAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
//keep the method generic enough so it could be used in multiple places
//the calling function can do specific things based on the results, such as if the user is a VIP or not
getUserManager: function() {
//initialize some variables
var id = this.getParameter("sysparm_id");
var response = {}; //result will be added to this object
response.userIsVip = ""; //set default values
response.managerId = "";
response.managerName = "";
//get the User record if we received a sys_id with the call
if (id) {
var user = new GlideRecord("sys_user");
//can we find the record?
if (user.get(id)) {
response.userIsVip = (user.getValue("vip") == "1"); //will return "true" or "false"
response.managerId = user.getValue("manager");
response.managerName = user.manager.getDisplayValue();
}
}
return JSON.stringify(response);
},
type: 'AcmeCatalogClientUtilsAjax'
});
And the Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
//ignore when we first load the form
if (isLoading) {
return;
}
//clear the user details if the User field is cleared
if (newValue == "") {
g_form.setValue("manager", "");
g_form.setValue("is_vip", "");
return;
}
//display the User's manager when an actual User record is selected
var ga = new GlideAjax("AcmeCatalogClientUtilsAjax");
ga.addParam("sysparm_name", "getUserManager");
ga.addParam("sysparm_id", newValue);
ga.getXMLAnswer(showUserManager);
// Callback function to process the response returned from the server
function showUserManager(response) {
var result = JSON.parse(response); //transform the JSON string to an object
g_form.setValue("is_vip", result.userIsVip);
g_form.setValue("manager", result.managerId, result.managerName);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 01:52 PM
I just realized you are doing this "onLoad". That's a whole other story. No point loading the form in the browser then going back to the server for info.
Assuming you are just dealing with the currently logged-in user and there is no "Requested for" variable, you can do this right in the Default values of the Manager and VIP variables. We can use this article as a helper: "FpcUser" - A More Useful User Object.
Default value for VIP:
javascript:new global.FpcUser().getUserRecord().vip == "1";
Default value for Manager:
javascript:var user = new global.FpcUser().getUserRecord(); if (!user.vip) {user.manager}