Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var CallerInfoHelper = Class.create();
CallerInfoHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getCallerInfo: function() {
var callerSysId = this.getParameter('sysparm_caller');
if (!callerSysId)
return JSON.stringify({ email: '', mobile: '' });

var userGR = new GlideRecord('sys_user');
if (!userGR.get(callerSysId))
return JSON.stringify({ email: '', mobile: '' });

var userObj = {
email: userGR.email.toString(),
mobile: userGR.mobile_phone.toString()
};

return JSON.stringify(userObj);
},

type: 'CallerInfoHelper'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Display Custom Email/Phone Field Based on Incident Channel Field and Populate those Field with Caller Information

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.

### Use Case
- When **Channel = Email**, the **Email** field becomes visible and is auto-populated with the caller’s email address
- When **Channel = Phone**, the **Phone** field becomes visible and is auto-populated with the caller’s mobile number
- Both details fetched from the caller’s record from **sys_user** table.
- The custom Email and Phone fields may also serve as placeholder to update if details differ from the caller record

### Prerequisites
- Create Two custom fields on Incident Table
- **u_email** which captures store the caller’s email address
- **u_phone** which capture caller’s mobile number
- Create **Two UI Policies** which hides the u_email and u_phone field unless channel choice is phone or email
- 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
- To further enhance usecase Regex used on Phone field. Refer (https://github.com/ServiceNowDevProgram/code-snippets/pull/2375)

---

### Incident Record when channel choice is other than Email or Phone

![Display_CustomField_Autopopulate_Caller_3](Display_CustomField_Autopopulate_Caller_3.png)

---

### Incident Record when Channel choice is email and populate Email Field by caller's Email

![Display_CustomField_Autopopulate_Caller_1](Display_CustomField_Autopopulate_Caller_1.png)

---

### Incident Record when channel choice is phone and populate Phone Field by caller's Phone Number

![Display_CustomField_Autopopulate_Caller_2](Display_CustomField_Autopopulate_Caller_2.png)

---






Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') return;

var ga = new GlideAjax('CallerInfoHelper');
ga.addParam('sysparm_name', 'getCallerInfo');
ga.addParam('sysparm_caller', newValue);

ga.getXMLAnswer(function(answer) {
// Confirm what you’re actually receiving
console.log("GlideAjax raw answer:", answer);

if (!answer) return;

var info;
try {
info = JSON.parse(answer);
} catch (e) {
console.log("Error parsing JSON:", e);
return;
}

g_form.setValue('u_email', info.email || '');
g_form.setValue('u_phone', info.mobile || '');

});
}
Loading