OnChange - Manager Name script

Brian Sorensen
Tera Guru

Hi
Im getting a Java Script error on this and getting stuck

Here is the scenario

On our Request forms we have the following Variable Set - User Information
the set contains the following

1. Customer Name - customer_name

2. Managers Name - manager

3. Business Reason for Ask

 

When you load the form we have it set do the following

Customer Name - Who ever opened the form

Manager - that persons manager - sets to Read Only -since we do not want people to be able to select their approver

However when you change the customer name, the manager should change as well, this is where I get the error

Here is the script that I am using - I have active off right now

OnChange

BrianSorensen_0-1768228757882.png

Script

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var ga = new GlideAjax('UserUtils');
ga.addParam('sysparm_name', 'getUserDetails');
ga.addParam('sysparm_user_id', g_form.getValue('customer_name'));
ga.getXMLAnswer(function(answer) {
console.log('answer: ' + answer);
if (!answer)
return;
try {
var userObject = JSON.parse(answer);
g_form.setValue('manager', userObject.manager.sys_id, userObject.manager.name);
} catch (err) {
console.log(err);
}
});
}

 

When I change the customer I get Java Error.

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron

Post the entire Script Include.  Errors are usually in the Script Include not found, running correctly, or returning anything.  Also consider an alternate approach of using the Auto Populate feature on the Manager variable, instead of the client and server scripts. 

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron

@Brian Sorensen 

why not use auto-populate feature and no scripting is required

Auto-populate a variable based on a reference type variable (Utah) 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Brian Sorensen 

Thank you for marking my response as helpful.

As per community feature you can mark multiple responses as correct.

💡 If my response helped, please mark it as correct as well so that this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Brad Bowman
Kilo Patron

Post the entire Script Include.  Errors are usually in the Script Include not found, running correctly, or returning anything.  Also consider an alternate approach of using the Auto Populate feature on the Manager variable, instead of the client and server scripts. 

Thanks!
Didn't realize that Autopopulate was working (previously had an issue with it)