- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-14-2018 10:39 AM
Hello, when we create an incident from a connect support chat session the chat session caller's name will load on the incident form, we have a client script below that sets the caller's business phone on the incident form so support doesn't have to type in the callers phone number every time. The script works fine for this but when we open an incident from Create New since a caller does not load the script sets the business phone field as undefined (see attached). Would like the business phone field to not display "undefined" if a caller does not load, can anyone help with this? Thanks
function onLoad() {
var caller = g_form.getReference('caller_id', loadLocation);
}
function loadLocation(caller) {
if (caller){
g_form.setValue('location', caller.location);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-14-2018 06:34 PM
Guys,
This solution works but I wanted to point out that this code isn't formatted the best and can be VERY inefficent. Nesting if statements makes the code harder to debug and is not as maintainable moving forward. I wanted to take a moment to point out Short-Circuit or minimal evaluation...
Basically, in C based languages (like Javascript) you can write a single line conditional statement such as:
if(caller && caller.phone){
//if both conditions are true, this code executes
}
Why is this better and what constitutes a short circuit? If the first value evaluates to false, the second value will NEVER be evaluated. This can dramatically speed up code execution! For example, if I want to check if a local variable is true BEFORE I evaluate a long running function (like a full round trip back to the database), I just have to put things in the right order:
if(localVar && GetFromDatabase()){
//The round trip to the database will only happen if localVar evaluates to true!
}
This will especially be useful in your OnLoad client script if you have any blocking calls. Don't make the user wait for the page to load if it isn't necessary!!
In the example that you gave, both variables will need to be evaluated every...single...time the code is executed.
/endrant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-14-2018 12:37 PM
Hi
Then Add a new checkbox or another type of field on the incident form.
When the incident is created from a chat session set the checkbox to true or the other type of field to some default value. If it is created from create new don't add any code to update the field.
In the onload client script add an if condition to run only when the checkbox is true or the field has any default value(this field would have value or will be true only when the incident is created from chat session).
So only the script would run if the incident is created from chat session.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-14-2018 01:23 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-14-2018 01:35 PM
You can put another if to validate that the looked up caller has a phone:
function onLoad() {
var caller = g_form.getReference('caller_id', loadPhone);
}
function loadPhone(caller) {
if (caller){
if (caller.phone) {
g_form.setValue('u_business_phone', caller.phone);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-14-2018 06:34 PM
Guys,
This solution works but I wanted to point out that this code isn't formatted the best and can be VERY inefficent. Nesting if statements makes the code harder to debug and is not as maintainable moving forward. I wanted to take a moment to point out Short-Circuit or minimal evaluation...
Basically, in C based languages (like Javascript) you can write a single line conditional statement such as:
if(caller && caller.phone){
//if both conditions are true, this code executes
}
Why is this better and what constitutes a short circuit? If the first value evaluates to false, the second value will NEVER be evaluated. This can dramatically speed up code execution! For example, if I want to check if a local variable is true BEFORE I evaluate a long running function (like a full round trip back to the database), I just have to put things in the right order:
if(localVar && GetFromDatabase()){
//The round trip to the database will only happen if localVar evaluates to true!
}
This will especially be useful in your OnLoad client script if you have any blocking calls. Don't make the user wait for the page to load if it isn't necessary!!
In the example that you gave, both variables will need to be evaluated every...single...time the code is executed.
/endrant