Best way to create a field that combines static values with a variable within the record?

jlaue
Kilo Sage

Hi All -

I am looking for the best way to create/populate a custom field on the User form that I will name MGR Email, and it would do the following:

- prefix with a static value of:     mgr

- then grab the value in the Location field for that user (Location on sys_user)

- then suffix it with the email domain:   @xyz.com

So if I had a user record open and that user's location value was 123, then this MGR Email field would be populated for that user as:   mgr123@xyz.com    

If the user's location was empty - then it should result in no value populated within this MGR Email field for that user.

Thank you!!

1 ACCEPTED SOLUTION

Did you try this ?



This should be a before business rule with insert and update check boxes checked. Below is the script.


if (current.location!='')


        current.your_mgr_email_field = 'mgr'+current.location.name+'@xyz.com';


else


        current.your_mgr_email_field = '';



Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

11 REPLIES 11

SanjivMeher
Kilo Patron
Kilo Patron

Hi,



You need a string field to do that. You can write an onBefore Insert/Update business rule to set this field.



Please mark this response as correct or helpful if it assisted you with your question.

Thank you for your response and guidance!   Would you kindly provide some sample code for this in the business rule, I am not sure how to make the value of this field empty if the Location value for the user record is null, and also not sure how to combine the static text before and after the Location value.  



Thanks!


This should be a before business rule with insert and update check boxes checked. Below is the script.



if (current.location!='')


        current.your_mgr_email_field = 'mgr'+current.location.name+'@xyz.com';


else


        current.your_mgr_email_field = '';



Please mark this response as correct or helpful if it assisted you with your question.

Can we try in this way to auto populate the value



onChange() client script on user field:


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


if (isLoading || newValue === '') {


return;


}


var ga = new GlideAjax('getUserLocEmailID');


ga.addParam('sysparm_name','getdetails');


ga.addParam('sysparm_caller', newValue);


ga.getXML(AsyncCall);


}


function AsyncCall(response) {


var answer = response.responseXML.documentElement.getAttribute("answer");


if(answer != ''){


var msgemail = 'mgr' + answer + '@xyz.com';


g_form.setValue('u_mgr_email', msgemail);


}


else


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


}



Client Callable Script Include:


var getUserLocEmailID = Class.create();


getUserLocEmailID.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getdetails: function(){


var gr = new GlideRecord('sys_user');


if(gr.get(this.getParameter('sysparm_caller')))


{


if(gr.getValue('location') != '')


return gr.getDisplayValue('location');


}


},


type: 'getUserLocEmailID'


});