Skip to content

Commit f92aa64

Browse files
authored
Display Custom Email/Phone Field Based on Incident Channel Field and Populate those Field with Caller Information (#2475)
* Create README.md * Add files via upload * Create clientScriptOnChangeCaller.js * Create GlideAjaxCallerInfo.js
1 parent e2e70a8 commit f92aa64

File tree

6 files changed

+91
-0
lines changed

6 files changed

+91
-0
lines changed
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var CallerInfoHelper = Class.create();
2+
CallerInfoHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
3+
4+
getCallerInfo: function() {
5+
var callerSysId = this.getParameter('sysparm_caller');
6+
if (!callerSysId)
7+
return JSON.stringify({ email: '', mobile: '' });
8+
9+
var userGR = new GlideRecord('sys_user');
10+
if (!userGR.get(callerSysId))
11+
return JSON.stringify({ email: '', mobile: '' });
12+
13+
var userObj = {
14+
email: userGR.email.toString(),
15+
mobile: userGR.mobile_phone.toString()
16+
};
17+
18+
return JSON.stringify(userObj);
19+
},
20+
21+
type: 'CallerInfoHelper'
22+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Display Custom Email/Phone Field Based on Incident Channel Field and Populate those Field with Caller Information
2+
3+
Displays either the **Email** or **Phone** field on the **Incident** form based on the selected **Channel** value (Email or Phone) and populate the fields with the caller’s details.
4+
5+
### Use Case
6+
- When **Channel = Email**, the **Email** field becomes visible and is auto-populated with the caller’s email address
7+
- When **Channel = Phone**, the **Phone** field becomes visible and is auto-populated with the caller’s mobile number
8+
- Both details fetched from the caller’s record from **sys_user** table.
9+
- The custom Email and Phone fields may also serve as placeholder to update if details differ from the caller record
10+
11+
### Prerequisites
12+
- Create Two custom fields on Incident Table
13+
- **u_email** which captures store the caller’s email address
14+
- **u_phone** which capture caller’s mobile number
15+
- Create **Two UI Policies** which hides the u_email and u_phone field unless channel choice is phone or email
16+
- Create an onChange Client Script that calls a GlideAjax Script to fetch the caller’s contact details and populate the custom Email or Phone field on the Incident form
17+
- To further enhance usecase Regex used on Phone field. Refer (https://github.com/ServiceNowDevProgram/code-snippets/pull/2375)
18+
19+
---
20+
21+
### Incident Record when channel choice is other than Email or Phone
22+
23+
![Display_CustomField_Autopopulate_Caller_3](Display_CustomField_Autopopulate_Caller_3.png)
24+
25+
---
26+
27+
### Incident Record when Channel choice is email and populate Email Field by caller's Email
28+
29+
![Display_CustomField_Autopopulate_Caller_1](Display_CustomField_Autopopulate_Caller_1.png)
30+
31+
---
32+
33+
### Incident Record when channel choice is phone and populate Phone Field by caller's Phone Number
34+
35+
![Display_CustomField_Autopopulate_Caller_2](Display_CustomField_Autopopulate_Caller_2.png)
36+
37+
---
38+
39+
40+
41+
42+
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function onChange(control, oldValue, newValue, isLoading) {
2+
if (isLoading || newValue === '') return;
3+
4+
var ga = new GlideAjax('CallerInfoHelper');
5+
ga.addParam('sysparm_name', 'getCallerInfo');
6+
ga.addParam('sysparm_caller', newValue);
7+
8+
ga.getXMLAnswer(function(answer) {
9+
// Confirm what you’re actually receiving
10+
console.log("GlideAjax raw answer:", answer);
11+
12+
if (!answer) return;
13+
14+
var info;
15+
try {
16+
info = JSON.parse(answer);
17+
} catch (e) {
18+
console.log("Error parsing JSON:", e);
19+
return;
20+
}
21+
22+
g_form.setValue('u_email', info.email || '');
23+
g_form.setValue('u_phone', info.mobile || '');
24+
25+
});
26+
}

0 commit comments

Comments
 (0)