Record Producer Client Script Best Practice?

Brandon P1
Mega Guru

We have a client catalog script in a variable set attached to multiple record produces.  The script works without issue.  What I would like to know is if there is a better way of doing this. When I run diagnostics on anything containing the script I get two issues listed.  So I am seeking advice as I know of no way around this.

The Code:

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

   var userGA = new GlideAjax('CPXCatalogUtils');
	userGA.addParam('sysparm_name','getUserDetails');
	userGA.addParam('sysparm_userid',newValue);
	userGA.getXML(getDetails);
	
	function getDetails(answer) {
		if(answer) {
			var result = answer.responseXML.getElementsByTagName("userdetails");
			var location = result[0].getAttribute("location");
			g_form.setValue('location',location.toString());
		}
	}
   
}

The script include uses dot walking to get the location reference. when we try to get there directly using the user it does not work and research showed that we could only dot walk one layer in and anything deeper would require the use of GlideAjax.

The Diagnostics:

find_real_file.png

Essentially what we are doing is when one of our end users submits an incident through our portal one of the requirements is to note which location they work out of. Instead of forcing the user to add in the info we run this script to do it for them.   

Everything works, but is this the best way to accomplish this?

Thanks!

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Can you replace this with a better solution? Definitely:
- 2021-02-11 Catalog Data Lookup Definition on any table, eliminating Catalog Client Scripting

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

View solution in original post

4 REPLIES 4

Maik Skoddow
Tera Patron
Tera Patron

Hi

if it works for you I would leave it. 

And having a look at your script, I think diagnostics reported a wrong alarm. The method call "getElementsByTagName" was interpreted as DOM manipulation, which in your case is definitely wrong.

And unfortunately I cannot say whether you could replace your script with any other solution, as you did not provide all details.

Kind regards
Maik

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Can you replace this with a better solution? Definitely:
- 2021-02-11 Catalog Data Lookup Definition on any table, eliminating Catalog Client Scripting

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Thanks,  This helped me simplify and reduce code which of course always helps with manageability.   

Mark Roethof
Tera Patron
Tera Patron

Going for scripting anyway? Optimize your code.

For example:
- Use GlideAjax + getXMLAnswer instead of getXML
- On setValue() reference fields/variables, use the value AND displayValue parameter

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn