Glide Ajax script include Glide record returning empty
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-22-2022 11:46 AM
Hello everyone,
Thank you in advance for any help you can provide me. My setup is this:
I have a portal for end users to submit records through to a custom table on a scoped application. One of the tabs in the form of this portal has a reference field that points to a custom contacts table belonging to this application. The field in question is called "u_contact" and underneath it, there are related fields that correspond to the selected users e-mail, address, state, and etc.
My goal is to have the end users, who are contacts and do not have the ITIL role, be able to select the contact on this dropdown reference field, and have the related information automatically populate. The issue I am having, is that obviously getReference() does not work in service portal. This functionality used to work when I had a XMLHttpRequest() implemented but that has since been deprecated, so I am getting a script error now, forcing me to change it. (The script error only applies to the contact users since they dont have itil or admin rights.)
Thus, I am attempting to use Glide Ajax.
I have been logging various portions of it, but it seems that the glide record I am getting back from the script include, is blank. so my fields are all null. I have tried every combination of client script and script include on all forums i can find and nothing is working.
CLIENT SCRIPT:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var contact = g_form.getValue("u_contact"); //Gives the ID of the person in the contact field. The sys id corresponds to the user in the custom table and not the user on sys_user table which is what I want.
alert("CONTACT GET VALUE " + contact); // this provides the ID above and when I search for it on the custom table, i find the user with all their data.
var ga = new GlideAjax('stpGrabContactDataFromClientSide');
ga.addParam('sysparm_name', "getUserInfo"); // calling the script include
ga.addParam('sysparm_user', contact); //passing the id above
ga.getXMLAnswer(function(answer){
var response = JSON.parse(answer);
var str = JSON.stringify(response);
//g_form.setValue('u_contact_business_phone', response.phone);
g_form.setValue('u_contact_email', response.email);
//g_form.setValue('u_contact_street', response.street);
//g_form.setValue('u_contact_city', response.city);
//g_form.setValue('u_contact_state', response.state);
//g_form.setValue('u_contact_zip', response.zip);
});
}
SCRIPT INCLUDE:
//gs.addInfoMessage("HERE I AM"); this fires and tells me the script include is being activated
var stpGrabContactDataFromClientSide = Class.create();
stpGrabContactDataFromClientSide.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getUserInfo : function() {
var usr = this.getParameter('sysparm_user');
var obj = {};
var gr = new GlideRecord('u_ncb_users'); //this is the custom table I am querying
gr.addQuery('sys_id', usr);
gr.query();
if(gr.next()) {
gs.addInfoMessage("gr " + JSON.stringify(gr)); // I am getting here, but the GR Object is empty even though the sys id points to a contact that has info
//obj.phone = gr.phone.toString();
obj.email = gr.email.toString();
//obj.city = gr.city.toString();
//obj.state = gr.state.sys_id.toString();
//obj.zip = gr.zip.sys_id.toString();
}
var json = new global.JSON();
var data = json.encode(obj);
return data;
},
type: 'stpGrabContactDataFromClientSide'
});
This is the object I am getting in the addInfoMessage above.
{"sys_meta":null,"u_department":{},"u_mailing_zip_postal_code":{},"u_mailing_same_as_street_address":{},"sys_mod_count":{},"u_mailing_street":{},"u_street_address_ims_code":{},"sys_updated_on":{},"sys_tags":{},"u_user":{},"sys_id":{},"u_mailing_city":{},"sys_updated_by":{},"u_agency":{},"u_mailing_state_province":{},"u_member_of":{},"sys_created_on":{},"u_active":{},"u_ims_code":{},"u_mailing_assets_ims_code":{},"u_signatory":{},"u_institution":{},"u_organization":{},"sys_created_by":{}}
My Client Script is Global and my script include is Client Callable.
Any advice is much appreciated.
Thank you kindly.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-22-2022 01:06 PM
Hi,
can u try this in your client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var contact = g_form.getValue("u_contact"); //Gives the ID of the person in the contact field. The sys id corresponds to the user in the custom table and not the user on sys_user table which is what I want.
alert("CONTACT GET VALUE " + contact); // this provides the ID above and when I search for it on the custom table, i find the user with all their data.
var ga = new GlideAjax('stpGrabContactDataFromClientSide');
ga.addParam('sysparm_name', "getUserInfo"); // calling the script include
ga.addParam('sysparm_user', contact); //passing the id above
ga.getXML(ajaxResponse);
function ajaxResponse(response){
var result= response.responseXML.documentElement.getAttribute('answer')
var resp = JSON.parse(result);
//g_form.setValue('u_contact_business_phone', response.phone);
g_form.setValue('u_contact_email', resp.email);
//g_form.setValue('u_contact_street', response.street);
//g_form.setValue('u_contact_city', response.city);
//g_form.setValue('u_contact_state', response.state);
//g_form.setValue('u_contact_zip', response.zip);
});
}
and in script include paste this
var stpGrabContactDataFromClientSide = Class.create();
stpGrabContactDataFromClientSide.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getUserInfo : function() {
var usr = this.getParameter('sysparm_user');
var obj = {};
var gr = new GlideRecord('u_ncb_users'); //this is the custom table I am querying
gr.addQuery('sys_id', usr);
gr.query();
if(gr.next()) {
//obj.phone = gr.phone.toString();
obj.email = gr.email.toString();
//obj.city = gr.city.toString();
//obj.state = gr.state.sys_id.toString();
//obj.zip = gr.zip.sys_id.toString();
}
var data= JSON.stringify(obj);
return data;
},
type: 'stpGrabContactDataFromClientSide'
});
Please Mark this is as correct if its helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-22-2022 02:51 PM
Hello Charles,
Thank you for providing me with assistance on this. Unfortunately, that did not work. I had to remove the last closing paragraph tag in the client script you pasted since it did not have an opener and was flagged red. It did not seem necessary but correct me if I am wrong.
The only other thing I did was add the info message back in to see if the GR was coming in empty still. It appears so.
{"sys_meta":null,"u_department":{},"u_mailing_zip_postal_code":{},"u_mailing_same_as_street_address":{},"sys_mod_count":{},"u_mailing_street":{},"u_street_address_ims_code":{},"sys_updated_on":{},"sys_tags":{},"u_user":{},"sys_id":{},"u_mailing_city":{},"sys_updated_by":{},"u_agency":{},"u_mailing_state_province":{},"u_member_of":{},"sys_created_on":{},"u_active":{},"u_ims_code":{},"u_mailing_assets_ims_code":{},"u_signatory":{},"u_institution":{},"u_organization":{},"sys_created_by":{}}
I am however, now noticing a new error in the console. I am attaching it below.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-22-2022 04:22 PM
I need to add some further confusion for me but possible clarification for someone else.
As stated above, the u_contact field references a record on the u_ncb_users table. The sys ID I get from getting the value of u_contact, can be queried (filter) on the u_ncb_users table and it does indeed give me the user data. The data is in the "ncbUserProfile" attachment.
However, when running a glide record query for this user on the script include querying the table it lives in, it returns nothing.
Now here is the interesting or rather confusing part for me. When I look at the XML for this user record that comes up, there is a field at the bottom called <u_user Display Value="test test">cdsfs32423423dsf...</u_user>.- This corresponds to the u_user field on the form which is a reference field to the "customer_contact" table.
If I now take this sys ID in the u_user field, and create a glide record query on the same script include, replaceing the old one, only this time, I run it on the "customer_contact" table, it gives me a record with the user data as I would need it in it's entirety. The e-mail comes in along with anything else I need..
The second screenshot shows this field.
The third screenshot shows that it corresponds to the same user but on the customer_contacts table.
It seems like the u_ncb_users table is a shell table I cannot use.. but the problem is that I don't have access to that u_user sys id unless I query the table for the record I can then dot walk.
The problem is I do need the u_contact field to only reference the users on the u_ncb_users table and not all the users in the contacts table.
I am sorry if this is confusing. If I can elaborate or clarify on anything specific, I would be happy to.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2022 01:32 PM
Well, none of the fields are accessible in the incoming GR apart from one; the user id of the parent contact record.
I have it working with this configuration for myself when I am logged in (Admin). However when a user logs in and changes the field, all other fields come in as undefined. This tells me its an ACL issue, but I have given the user read access to both u_ncb_users table and customer_contact table. What other access would they need? Is there another way to bring in these values without giving them access to the tables?
CLIENT SCRIPT:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var contact = g_form.getValue("u_contact"); //Gives the ID of the person in the contact field. The sys id corresponds to the user in the custom table and not the user on sys_user table which is what I want.
//alert("CONTACT GET VALUE " + contact.u_user); // this provides the ID above and when I search for it on the custom table, i find the user with all their data.
var ga = new GlideAjax('stpGrabContactDataFromClientSide');
ga.addParam('sysparm_name', "getUserInfo"); // calling the script include
ga.addParam('sysparm_user', contact); //passing the id above
ga.getXML(ajaxResponse);
function ajaxResponse(response){
var result= response.responseXML.documentElement.getAttribute('answer');
var resp = JSON.parse(result);
g_form.setValue('u_contact_business_phone', resp.phone);
g_form.setValue('u_contact_email', resp.email);
g_form.setValue('u_contact_street', resp.street);
g_form.setValue('u_contact_city', resp.city);
g_form.setValue('u_contact_state', resp.state);
g_form.setValue('u_contact_zip', resp.zip);
}
}
SCRIPT INCLUDE:
var stpGrabContactDataFromClientSide = Class.create();
stpGrabContactDataFromClientSide.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getUserInfo : function() {
var usr = this.getParameter('sysparm_user');
var obj = {};
var gr = new GlideRecord("u_ncb_users"); //this is the custom table I am querying
gr.addQuery('sys_id', usr);
gr.query();
while(gr.next()){
var grs = new GlideRecord("customer_contact");
grs.addQuery('sys_id', gr.u_user);
grs.query();
if(grs.next()){
gs.addInfoMessage("grs e-mail " + JSON.stringify(grs.email)); // I am getting here, but the GR Object is empty even though the sys id points to a contact that has info
obj.email = grs.email.toString();
obj.phone = grs.phone.toString();
obj.street = grs.street.toString();
obj.city = grs.city.toString();
obj.state = grs.state.toString();
obj.zip = grs.zip.toString();
}
}
var data= JSON.stringify(obj);
return data;
},
type: 'stpGrabContactDataFromClientSide'
});
Attaching screenshot of what the end user sees: