Need help with OnLoad client script

mhurley
Kilo Contributor

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);
}

}

1 ACCEPTED SOLUTION

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

View solution in original post

8 REPLIES 8

Abhishek77
ServiceNow Employee
ServiceNow Employee

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

 

 

mhurley
Kilo Contributor

I have the option below to create the incident from the support session but I don't know how or where to populate a checkbox when the incident is created from this, any clues?

 

find_real_file.png

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

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);
		}
	}
	
}

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