
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-30-2020 09:01 PM
Alright bear with me.
I have the first draft of a script include
var ContactUtils = Class.create();
ContactUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
/**
* Splits a fullname into first/last components
*/
splitFullname: function(name){
var fullname = name.trim();
var size = fullname.length;
var lastLetter = fullname.lastIndexOf(' ');
var space = fullname.lastIndexOf(' ') + 1;
var firstName = fullname.slice(0, lastLetter);
var lastName = fullname.slice(space,size);
return [firstName, lastName];
},
/**
* Uses an email address to check if contact exists - AskCalSAWS
*/
checkContact: function(){
var fullname = this.getParameter("sysparm_fullname");
var email = this.getParameter("sysparm_email");
var contact = '';
var names = this.splitFullname(fullname);
var emailAddress = email.trim();
var cr = new GlideRecord('customer_contact');
// just return the sys_id if entry exists
if(cr.get('email', emailAddress)) {
contact = cr.sys_id;
}
// otherwise create a new contact and return sys_id
else {
contact = this.createContact(names, emailAddress);
}
return contact;
},
/**
* Creates a new Contact entry from a splitFullName() return
*/
createContact: function(names, email){
var contact_record = new GlideRecord('customer_contact');
contact_record.initialize();
contact_record.first_name = names[0];
contact_record.last_name = names[1];
contact_record.email = email;
var sysID = contact_record.insert();
return sysID;
},
/**
* Just grabs the contact name using the associated sys_id
*/
getContactName: function(sysid) {
var contact = new GlideRecord('customer_contact');
var contactName = '';
if(contact.get(sysid)) {
contactName = contact.name;
}
else {
contactName = "The name is not here"; // need a better error here
}
return contactName;
},
type: 'ContactUtils'
});
and I call the script include here, as part of a widget in the client script section using a helper function,
// client script call
var contact = getContactSysID(user_name,user_email);
// helper function
function getContactSysID(user,email,callback) {
var userName = user;
var userEmail = email;
var ga = new GlideAjax('ContactUtils');
ga.addParam('sysparm_name','checkContact');
ga.addParam('sysparm_fullname',userName);
ga.addParam('sysparm_email',userEmail);
ga.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
callback(answer);
});
}
Ok, this does in fact work so far. The problem is, I need to use what's returned from this to lookup the Contact Name in the customer_contact table. Of course the problem is, that the script is not returning the sys_id in time for me to catch it and use it to lookup the Contact Name.
Something like this
// how the contact name might be gotten
var contactName = getContactName(contact);
gs.info("Contact Name: " + contactName); // is always undefined, much sadness
So I'm guessing that I need to somehow craft the perfect GlideAjax callback to wait for the first function call to finish before actually making the next call to get the data from the customer_contact table.
Any input would be greatly appreciated on this. Wrapping my head around callbacks has been trying for me. Thank you in advance.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2020 10:52 AM
Ok folks, here's where you learn from my mistakes.
Mistake 1: not realizing which context you're in....
I thought I was working in the Client Script portion of my widget, I was not. I was in the Server Script side. Because I wrongfully thought I was in the client side, wrongfully created GlideAjax based function calls to my script include. Which I did not need.
Mistake 2: not trying the KISS principle straight off.
After using the wrong function call types, and finally getting the info I thought I should be getting, I never bothered to do a typeOf to verify what I was receiving from my calls. I thought I was receiving plain ole strings, this was NOT the case.
Because of this, my widget scripting would NOT fill in one field with the values I was returning. I wasted many hours attempting to figure out why the JSON object I passed via REST was not capturing 1 silly field. Many hours, much gnashing of teeth.
The very simple answer to my issue was to append .toString() onto the value returned from my function call to a script include. So all that above, became...
/**
* Call the script include ContactUtils here...
* splits the name into first/last name components
* uses the email address to check the Contacts table for the user
* if not there inserts a new contact
* if there just returns the sys_id
**/
var contactInfo = new ContactUtils();
var contact = contactInfo.checkContact(user_name,user_email).toString(); // returns contact sys_id
Which when ran gave me the STRING version of the sys_id to pass into my REST call, to create my table entry, to fill in my reference field. I spent hours on this. HOURS all because I didn't think to do a simple check of what the value was my function was returning. In this case NOT a string.
Ugh.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2020 07:12 AM
Hi,
It's made complex by using GlideAjax which is rather strictly forced to be asynchronous. Have you looked into using the server.get() method to call to the widget server script to get the contact and then continue with what you're needing with the .then promise?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2020 07:44 AM
I have not. I'll look for more specifics and try it out too.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2020 10:52 AM
Ok folks, here's where you learn from my mistakes.
Mistake 1: not realizing which context you're in....
I thought I was working in the Client Script portion of my widget, I was not. I was in the Server Script side. Because I wrongfully thought I was in the client side, wrongfully created GlideAjax based function calls to my script include. Which I did not need.
Mistake 2: not trying the KISS principle straight off.
After using the wrong function call types, and finally getting the info I thought I should be getting, I never bothered to do a typeOf to verify what I was receiving from my calls. I thought I was receiving plain ole strings, this was NOT the case.
Because of this, my widget scripting would NOT fill in one field with the values I was returning. I wasted many hours attempting to figure out why the JSON object I passed via REST was not capturing 1 silly field. Many hours, much gnashing of teeth.
The very simple answer to my issue was to append .toString() onto the value returned from my function call to a script include. So all that above, became...
/**
* Call the script include ContactUtils here...
* splits the name into first/last name components
* uses the email address to check the Contacts table for the user
* if not there inserts a new contact
* if there just returns the sys_id
**/
var contactInfo = new ContactUtils();
var contact = contactInfo.checkContact(user_name,user_email).toString(); // returns contact sys_id
Which when ran gave me the STRING version of the sys_id to pass into my REST call, to create my table entry, to fill in my reference field. I spent hours on this. HOURS all because I didn't think to do a simple check of what the value was my function was returning. In this case NOT a string.
Ugh.