- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
Wednesday
Hello All:
Despite it all, after 15 years, i'm revisiting Script Includes. LOL.
I don't do them much, so here is a dumb question.
I'm attempting to restrict users from requesting duplicate service accounts.
In my form, I'm also dictating the naming convention.
So when they enter a new name, i want to query my Service Account table to:
1. Validate its not already there
2. It hasn't been Deactivated
So, On my catalog variable serv_acc_name
On change, I'm calling my script include to pull back any record with that name
So the answer i get is populated
Serv Acct ={"po_dcom_ticket":{"display_value":"RITM0000013","sys_id":"d4c1edc5db8783000d95561bdc9619aa"},"po_name":"xx-SVC-T-xxxx","po_type":"svc"}
but when i attempt to get get the po_dcom_ticket value it returns object object.
Help.
Client Script
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   //Type appropriate comment here, and begin script below
   var svcName = newValue;
    //findAccount(svcName);
	var ga1 = new GlideAjax('getServAccInfo');
	ga1.addParam('sysparm_name', 'getServAccInfo');
	ga1.addParam('sysparm_acc_type', 'svc');
	ga1.addParam('sysparm_acc_name', svcName);
	ga1.getXML(updateFields);
}
function updateFields(response){
	var answer = response.responseXML.documentElement.getAttribute("answer");
	var aObj = JSON.parse(answer);
	var deComm = aObj.po_name;
	alert("the decom ticket is "+deComm);
}
Script Include
var getServAccInfo = Class.create();
getServAccInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getServAccInfo : function() {
		
		var accType = this.getParameter('sysparm_acc_type');
		var accName = this.getParameter('sysparm_acc_name');
		var u = new GlideRecord('u_service_accounts');
		u.addQuery('u_nam',accName);
		u.query();
		var uObj = {};
		if (u.next()) {
			uObj = {
					"po_name" : u.getValue('u_nam'),
					"po_type" : u.getValue('u_category'),
					"po_dept" : {
						"sys_id" : u.getValue('u_decom_ticket'),
						"display_value" : u.u_decom_ticket.getDisplayValue()
					}
			};
		}
		
		var answer = new JSON().encode(uObj);
		gs.log('Serv Acct =' + answer);
		
		return answer;
		},
		
    type: 'getServAccInfo'
});
					
				
			
			
				
			
			
				Solved! Go to Solution.
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
Thursday - last edited Thursday
Thanks all.
I finally got to my answer.
Unfortunately, I could not get the RITM # to display from the SI.
So I simplified the SI to only return the sys_id of the RITM Decom Ticket.
Then used a GlideRecord in my client script to query task.
To the best solution but it works.
No matter which option I used from your comments, getting the RITM Number to Display in my CLient script always returned Object_object.
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
Wednesday
This probably isn't causing an issue, but it's a bad idea to name your Script Include and a function in it exactly the same. Come up with something new to distinguish the two. I would also change the end of your Script Include to use JSON.stringify instead of encode. You've logged the value, but it doesn't show that it's a string. A format like the following also removes the ambiguity between the Script Include and Client Script both having an 'answer' script variable - so it's clear when you refer to the answer being logged which script you are talking about.
gs.log('Serv Acct =' + JSON.stringify(uObj));
var return JSON.stringify(uObj);
If it's still not working for you, try alerting on answer in the Client Script to see what is actually passed back to the client.
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
Wednesday - last edited Wednesday
I think you might have pasted the incorrect scripts? There is no po_dcom_ticket property in the object returned from your script include.
I changed the field values and table to arbitrary values and I can't notice any issues in what is returned. I think your issue is you are trying to use the window alert to print an object but it will be converted to a string and just print [object Object]. You can try alert(JSON.stringify(obj)) or console.log(obj) 
var getServAccInfo = Class.create();
getServAccInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getServAccInfo: function () {
		var accType = this.getParameter('sysparm_acc_type');
		var accName = this.getParameter('sysparm_acc_name');
		var u = new GlideRecord('incident');
		u.addQuery('short_description', accName);
		u.query();
		var uObj = {};
		if (u.next()) {
			uObj = {
				"po_name": u.getValue('short_description'),
				"po_type": u.getValue('category'),
				"po_dept": {
					"sys_id": u.getValue('caller_id'),
					"display_value": u.caller_id.getDisplayValue()
				}
			};
		}
		return new JSON().encode(uObj);
	},
	type: 'getServAccInfo'
});
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
Thursday - last edited Thursday
Thanks all.
I finally got to my answer.
Unfortunately, I could not get the RITM # to display from the SI.
So I simplified the SI to only return the sys_id of the RITM Decom Ticket.
Then used a GlideRecord in my client script to query task.
To the best solution but it works.
No matter which option I used from your comments, getting the RITM Number to Display in my CLient script always returned Object_object.
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
Thursday - last edited Thursday
The best solution is the one that works, unless it uses not recommended and/or inefficient code. Using GlideRecord in a Client Script has always been a bad idea, not supported, and may cause problems down the road. It will probably be fine in this case if it's not an intense query/load, but it's totally unnecessary to make another server call since you are already making one trip to the server with the Glide Ajax. If RITM number is what you want, then return it from the Script Include with or instead of the sys_id. You can use
u.u_decom_ticket.number
in the Script Include instead of getDisplayValue(), but your log showed you were returning the RITM #, even though this log could not have possibly come from this version of the Script Include. Regardless, the issue with reading it in the Client Script is that you put it inside an object within the object, so either take out that nesting in the Script Include, or use
var deComm = aObj.po_dept.display_value;
in the Client Script to match the structure created in the posted version of the Script Include, or
var deComm = aObj.po_dcom_ticket.display_value;
to match the version of the Script Include that generated the posted log.
