- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Out of the cloud, there are 2 "onChange" Client Scripts that fire when the Caller field is changed, "(BP) Set Location to User" and "Highlight VIP Caller". That's really not a good idea if you want to maximize performance. Now, I'll admit both are individually optimized in that they are using "g_form.getReference" with a callback function, but the problem is 2 individual calls are being made back to the server for a change to the same field. We also need to think of the form as a whole and optimize its performance as well.
What we should do instead is consolidate both scripts into just one so we can do things more efficiently. We will also add a Display Business Rule to tell us if the Caller on an existing record is a VIP or not - no point going back to the server to find out if the Caller is a VIP or not, lets just send the info along with the record in the first place.
Business Rule:
Name: Custom - Is Caller a VIP
Table: Incident
When: display
Script:
(function(){
g_scratchpad.u_vip = current.caller_id.vip;
})();
The Business Rule simply creates a new Scratchpad variable with "true" or "false" based on the user record.
Client Script:
Name: Custom - Set Caller Info
Type: onChange
Table: Incident
Field: Caller
Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
if (!g_form.isNewRecord()) {
u_highlightVipCaller(g_scratchpad.u_vip);
}
return;
}
if (!newValue) {
u_highlightVipCaller(false);
g_form.setValue("location", "");
return;
}
//get a reference to the new caller
g_form.getReference("caller_id", u_setCallerInfo);
function u_setCallerInfo(newCaller) {
//check for VIP status
u_highlightVipCaller(newCaller.vip);
//set Location info
g_form.setValue("location", newCaller.location);
}
function u_highlightVipCaller(vipCaller) {
try {
var callerLabel = $("label.incident.caller_id");
var callerField = $("sys_display.incident.caller_id");
if (vipCaller == "true") {
callerLabel.setStyle({backgroundImage: "url(images/icons/vip.gif)", backgroundRepeat: "no-repeat", backgroundPosition: "95% 55%"});
callerField.setStyle({color: "red"});
} else {
callerLabel.setStyle({backgroundImage: ""});
callerField.setStyle({color: ""});
}
} catch(err){}
}
}
The Client Script will:
- drop out for new records
- use the Scratchpad if loading an existing record
- clear the VIP highlighting and Location field if the Caller field is cleared
- go back to the server if the Caller field is populated with a valid entry
And that's it - we now have 1 optimized Client Script for when the Caller field is changed. You can use the same principle for multiple Client Scripts that you may have that fire when the same field is changed on any form.
You can see them running in demo019 at the moment and I've attached the XML files for both the Busines Rule and Client Script so you can import into your instances (just remove the txt extension).
- 4,834 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.