Auto fill user's phone on Incident form

brown9394
Tera Expert

Hello Experts,

I have a requirement to auto fill user's phone number from caller's profile on the Incident form, how can I populate caller's 'Business Phone' number from the user's profile automatically when the Caller is selected?

Screen Shot 2017-01-14 at 1.57.03 PM.png

1 ACCEPTED SOLUTION

Hi James,



As previously mentioned, the simplest approach is to model your solution after the base platform incident client script '(BP) Set Location to User'. This script runs client-side 'onChange' for field 'Caller'. It essentially gets the referenced user's location and copies it to the location field on the form. This use-case is pretty simple since both the source and target fields are a reference type and do not require any formatting.



It is possible to use the same approach for a phone number, but depending on your requirements for localization/normalization, the answer is a bit more complicated..



If you plan on using the base platform field "sys_user.phone) as your source, this column is of type 'Phone Number' (ph_phone). This can easily be mapped to a string type field, however you will not have the basic formatting capabilities on the incident form. It is technically possible to create a column with the legacy ph_phone type to enable one-to-one mapping with basic formatting, but I would recommend reaching out to ServiceNow for guidance as this field type is not readily available to customers.



Here's a sample of what an incident client script could look like as an 'onChange' type for field 'Caller' assuming your source field is named 'phone' and target field is 'u_contact_number'.



function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading) {


          return;


  }



  if (newValue == '') {


          g_form.setValue('u_contact_number', '');


          return;


  }



  if (!g_form.getControl('u_contact_number')) {


          return;


  }



  var caller = g_form.getReference('caller_id', setContactNumber);


}




function setContactNumber(caller) {


  if (caller) {


          g_form.setValue('u_contact_number', caller.phone);


  }


}



If you plan on using a source or target field that is an E.164 Phone Number, I would recommend reviewing the docs and community posts regarding usage as the E.164 field type can be a bit difficult to setup and utilize in certain cases.



Thanks,


Aric


View solution in original post

11 REPLIES 11

Aric3
Giga Expert

Hi James,



Assuming you have added a custom field on the incident or task table for "Contact Number", I would recommend checking out the base platform Client Script "(BP) Set Location to User" as a starting point to model a solution from. In previous versions of the incident form, the Caller's location was copied from the referenced user to a dedicated Location field via Client Script.



One warning with this approach: The base platform field sys_user.phone is of type "Phone Number" (ph_phone) and your custom "Contact Number" field may be a different type (appears to be a reference) so the direct copy/assignment of the value as it appears in the referenced Client Script may not be appropriate and some further mapping may be necessary.



Edit:


The approach outlined above will essentially take a 'snapshot' of the selected user's global phone number. Changes will not impact or update the users's global phone number, nor will updates the global phone number change on past incidents.



If you are looking to dot-walk directly to the phone number (as outlined in an earlier solution), any modification of the contact number on the incident will modify the selected users's global phone number.



Let me know if I can provide any further info.



Thanks,


Aric


Thanks for the replying Aric, I initially did have 'Contact Number' field created, but now that is deleted. The solution Rajiv provided using extended field 'caller.business phone' does the trick, however having hard time trying to get the dictionary override to change the label to 'contact number'.



Any ideas? I've attached screenshots above of the error I'm getting trying to set a new label. Is there another way to do a dictionary override?



Screen Shot 2017-01-14 at 4.20.43 PM.png


Hi James,



Ah sorry, I just realized this and provided an update/edit to my previous reply.



Regarding the dot-walk field approach, I've run into a similar issue in the past and unfortunately do not have a solution. Perhaps another member of the community could provide some feedback if this is possible on the platform or not?



Thanks,


Aric


So according to your post you edited, the dot walking solution will NOT work for me because the number on the form can and will change, but the number in user profile should remain the same.



How can I execute this with a new field 'Contact Number' and auto populate 'business phone' from caller's profile? Please help!


Hi James,



As previously mentioned, the simplest approach is to model your solution after the base platform incident client script '(BP) Set Location to User'. This script runs client-side 'onChange' for field 'Caller'. It essentially gets the referenced user's location and copies it to the location field on the form. This use-case is pretty simple since both the source and target fields are a reference type and do not require any formatting.



It is possible to use the same approach for a phone number, but depending on your requirements for localization/normalization, the answer is a bit more complicated..



If you plan on using the base platform field "sys_user.phone) as your source, this column is of type 'Phone Number' (ph_phone). This can easily be mapped to a string type field, however you will not have the basic formatting capabilities on the incident form. It is technically possible to create a column with the legacy ph_phone type to enable one-to-one mapping with basic formatting, but I would recommend reaching out to ServiceNow for guidance as this field type is not readily available to customers.



Here's a sample of what an incident client script could look like as an 'onChange' type for field 'Caller' assuming your source field is named 'phone' and target field is 'u_contact_number'.



function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading) {


          return;


  }



  if (newValue == '') {


          g_form.setValue('u_contact_number', '');


          return;


  }



  if (!g_form.getControl('u_contact_number')) {


          return;


  }



  var caller = g_form.getReference('caller_id', setContactNumber);


}




function setContactNumber(caller) {


  if (caller) {


          g_form.setValue('u_contact_number', caller.phone);


  }


}



If you plan on using a source or target field that is an E.164 Phone Number, I would recommend reviewing the docs and community posts regarding usage as the E.164 field type can be a bit difficult to setup and utilize in certain cases.



Thanks,


Aric