Retrieving Requester(caller_id) Email address / company information etc. on a client side Script

Community Alums
Not applicable

Hi,

How would I retrieve Requester email information which is displayed under the float when checking additional information ('i') on a requester in a incident.

It seems to be defined id="sys_readonly.sys_user.email" under the float, is there a way to refer directly to the information like in g_user.userName or is there a way to refer to it some other way? I have tried searching online for solutions but I seem to be hitting a dead end here.

Please let me know if additional information is required or if I'm being unclear

1 ACCEPTED SOLUTION

OK, thanks.


So, in the incident script:


Type: onLoad


Script:


function onLoad() {



var email = g_form.getReference('caller_id', setEmail);



function setEmail(email) {


if (email)


alert('email is '+ email.email);


g_form.setValue('short_description', email.email); //this changes the short_description, but change it to populate the right field, or remove it if you do not need it.


}


}



For the catalog task, you can use:


Again, an onLoad client script:


function onLoad() {


    //Type appropriate comment here, and begin script below



var email = g_form.getReference('request_item.request.requested_for', setEmail);


    }



function setEmail(email){


alert('email ' + email.email);


g_form.setValue('short_description', email.email); //this changes the short_description, but change it to populate the right field, or remove it if you do not need it.


}



harel


Edit:


I just noticed that I have the requested for field on my catalog task form. If you don't have it or don't want to add it, you will need to use a GlideAjax call in a client script and a script include:


Client script:


Table: sc_task


Type: onLoad


Script:


function onLoad() {


    //Type appropriate comment here, and begin script below



var ri = g_form.getValue('request_item');



var ga = new GlideAjax('getEmail'); //this is the script include


ga.addParam('sysparm_name', 'userEmail'); //this is the function within the script include


ga.addParam('sysparm_ri', ri);


ga.getXML(getResponse);



function getResponse(response) {


var values = response.responseXML.documentElement.getAttribute('answer');



alert(values);



}


}



Script include:


Name: getEmail


Client callable: yes


Script:


var getEmail = Class.create();


getEmail.prototype = Object.extendsObject(AbstractAjaxProcessor, {



userEmail : function() {



var ri = this.getParameter('sysparm_ri');



var req =   new GlideRecord('sc_req_item');


req.addQuery('sys_id', ri);


req.query();


while(req.next()) {


return req.request.requested_for.email;


}


},


      type: 'getEmail'


});


harel


View solution in original post

10 REPLIES 10

Hi Alex,


I am a bit confused:


In your question you said it's incident and caller_id.


In the post above I see it's a catalog task?


Where are you running the script? on which table? That can change the script a bit.



harel


Community Alums
Not applicable

Hello Harel,



Sorry for the confusion.



Actually need It to get working on both sc_task and incident but for testing purposes Incident is fine if it's working in incidents I'm sure I can get it working for tasks also



First function posted seems to be working (it does not crash the script) but it's not doing anything in the incident. If you could help make it on load that would be very helpful! Maybe even get the value visible in an alert?   as in our environment there's some server side scripting that is running in the background following field changes I have had some issues with changing and applying changes some fields because of it on the client side.. Also the caller field has been forced from the server side to be unchangeable once the incident has been created.


OK, thanks.


So, in the incident script:


Type: onLoad


Script:


function onLoad() {



var email = g_form.getReference('caller_id', setEmail);



function setEmail(email) {


if (email)


alert('email is '+ email.email);


g_form.setValue('short_description', email.email); //this changes the short_description, but change it to populate the right field, or remove it if you do not need it.


}


}



For the catalog task, you can use:


Again, an onLoad client script:


function onLoad() {


    //Type appropriate comment here, and begin script below



var email = g_form.getReference('request_item.request.requested_for', setEmail);


    }



function setEmail(email){


alert('email ' + email.email);


g_form.setValue('short_description', email.email); //this changes the short_description, but change it to populate the right field, or remove it if you do not need it.


}



harel


Edit:


I just noticed that I have the requested for field on my catalog task form. If you don't have it or don't want to add it, you will need to use a GlideAjax call in a client script and a script include:


Client script:


Table: sc_task


Type: onLoad


Script:


function onLoad() {


    //Type appropriate comment here, and begin script below



var ri = g_form.getValue('request_item');



var ga = new GlideAjax('getEmail'); //this is the script include


ga.addParam('sysparm_name', 'userEmail'); //this is the function within the script include


ga.addParam('sysparm_ri', ri);


ga.getXML(getResponse);



function getResponse(response) {


var values = response.responseXML.documentElement.getAttribute('answer');



alert(values);



}


}



Script include:


Name: getEmail


Client callable: yes


Script:


var getEmail = Class.create();


getEmail.prototype = Object.extendsObject(AbstractAjaxProcessor, {



userEmail : function() {



var ri = this.getParameter('sysparm_ri');



var req =   new GlideRecord('sc_req_item');


req.addQuery('sys_id', ri);


req.query();


while(req.next()) {


return req.request.requested_for.email;


}


},


      type: 'getEmail'


});


harel


Community Alums
Not applicable

Hi Harel,



Thanks a lot! Both of your first examples in your last post worked like a charm and were exactly what I was going for Kudos!




This is kinda off topic but still quite related when I try the following code I get the unique user key value of the manager something like this (474015d75893a400df298f8b9ff11ca) value seems to be under sys_user.manager




function onLoad() {


    //Type appropriate comment here, and begin script below




var manager = g_form.getReference('request_item.request.requested_for', setManager);


    }




function setManager(manager){


alert('manager ' + manager .manager );


g_form.setValue('short_description', manager .manager ); //this changes the short_description, but change it to populate the right field, or remove it if you do not need it.


}


onLoad();



I wonder if you know how retrieve/convert this value to name format such as displayed under sys_display.sys_user.manager or even better retrieve the email address of the users manager to a variable?


Hi Alex,



A slight modification of the script is needed - this will work for the incident table:


function onLoad() {



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



function setEmail(caller) {


if (caller){


alert('email is '+ caller.email);


}


var user = new GlideRecord('sys_user');


user.addQuery('sys_id', caller.manager);


user.query();


while(user.next()){


alert('manager name is ' + user.name + ' email: ' + user.email);


//g_form.setValue('SOME_FIELD', user.manager) // do not forget to add / change this line


}


g_form.setValue('short_description', caller.email); //change this...


}


}



If on the catalog task, then the change is very simple: in the script include, line 12 was:


return req.request.requested_for.email;


change it to:


return req.request.requested_for.manager.email; --> in this case, you can dot walk as you wish.



Let me know if more help is needed.


harel