diff --git a/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/Display_CustomField_Autopopulate_Caller_1.png b/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/Display_CustomField_Autopopulate_Caller_1.png new file mode 100644 index 0000000000..9bdf814c7c Binary files /dev/null and b/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/Display_CustomField_Autopopulate_Caller_1.png differ diff --git a/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/Display_CustomField_Autopopulate_Caller_2.png b/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/Display_CustomField_Autopopulate_Caller_2.png new file mode 100644 index 0000000000..5bbafd0f19 Binary files /dev/null and b/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/Display_CustomField_Autopopulate_Caller_2.png differ diff --git a/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/Display_CustomField_Autopopulate_Caller_3.png b/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/Display_CustomField_Autopopulate_Caller_3.png new file mode 100644 index 0000000000..34f020bb30 Binary files /dev/null and b/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/Display_CustomField_Autopopulate_Caller_3.png differ diff --git a/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/GlideAjaxCallerInfo.js b/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/GlideAjaxCallerInfo.js new file mode 100644 index 0000000000..d5653ff5f5 --- /dev/null +++ b/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/GlideAjaxCallerInfo.js @@ -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' +}); diff --git a/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/README.md b/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/README.md new file mode 100644 index 0000000000..d91289cec2 --- /dev/null +++ b/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/README.md @@ -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) + +--- + + + + + + diff --git a/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/clientScriptOnChangeCaller.js b/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/clientScriptOnChangeCaller.js new file mode 100644 index 0000000000..b3001513b2 --- /dev/null +++ b/Client-Side Components/Client Scripts/Display Custom Field Based on Incident Channel Field and populate with Caller Information/clientScriptOnChangeCaller.js @@ -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 || ''); + + }); +}