- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 06-25-2019 04:19 AM
When a variable has to be assigned depending on another variable, there are two ways to get this done.
For example, We have a field username(reference field) and a field user email(string field). The user email has to be updated depending on the field username onchange of the field UserName.
They are two ways to do this
Method 1 - Using getReference
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.getReference('caller_id', setEmail);
function setEmail(user){
g_form.setValue('caller_id_email', user.email);
}
}
Method 2 - Using GlideAjax
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax("Utils");
ga.addParam ("sysparm_name", "getEmail");
ga.addParam("sysparm_sys_id", newValue);
ga.getXMLAnswer(response);
function response(answer){
g_form.setValue("email", answer);
}
}
Script Include function would look something like -
var Utils = Class.create();
Utils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEmail: function(){
var sysId = this.getParameter('sysparm_sys_id');
var gr = new GlideRecord("sys_user");
gr.get(sysId);
return gr.email;
},
type: "Utils"
});
Suggested would be to go with method 2(GlideAjax). Since getReference will return all the data of the record whereas with GlideAjax you can get the data you want. Also, GlideAjax performs better than getReference.
Using Scratchpad -
In some cases, instead of making a GlideAjax call, u can use scratchpad to transfer data from server(using Business Rule - display) to client scripts. Data that is hidden in the form can be retrieved through scratchpad. One example is as below
Create a Display Business Rule
Add the following to the Advanced Script section
(function executeRule(current, previous /*null when async*/) {
g_scratchpad.assignment_group_inc = current.u_inc_assignment_group;
})(current, previous);
Now, in client scripts we can access the scratchpad variable -
g_scratchpad.assignment_group_inc
Hope this helps! Do let me know if there are any suggestions!
- 6,532 Views