- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2017 07:47 AM
Hi All, I got a task where I need to pull the user email of the callers for the incident . So after my thorough look into the wiki I understand that I have pull the details based on the user object. But since it was the for the first five incidents , I can use the setLimit function , since I have to bring the email address of the users so I need to bring the array into play and iterator which acts as an object and it next drives the things ie, it can pull the details.. This is how my script looks like
function getUserEmail(
var gr = new GlideRecord('incident');
gr.setLimit(5);
gr.query();
var ourUser = gs.getUser();
var needEmail = ourUser.getEmail();
var cars = new array();
var it = needEmail.iterator();
var i= 0;
while (it.hasNext()){
var myUsers = it.next();
cars[i] = myUsers
}
return cars;
)
var test = getUserEmail();
gs.print(test[0]);
But when I run the same in background script it is not working , request you all experts to help me out here on the mistake I have done.
Also I need to understand the purpose of hasNext() .
Thanks,
Rahamathullah
PS : I am learning to code , please be considerate with my mistakes
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2017 08:31 AM
Hi Rahamathullah,
Don't worry for the mistakes, at some point we all were learner.
First, you need to check the language syntax. I assume you are using Javascript for your script. In Javascript, you don't define a function as
function functionName{ ...
}
but you do
var functionName = function(parameter1, parameter2, ...){
...
}
Note also the difference between bracket types for defining the parameters and the body of the function.
In your case, as you don't have parameters the function would be like
var getUserEmail() = function{
...
}
Now, let's see the body of the function. If I understood correctly, you are trying to get the email of the caller for the first 5 incidents. In this case, you started well
var gr = new GlideRecord('incident');
gr.setLimit(5);
gr.query();
This would give us a result set where we can get the data of the 5 incidents.
We define our output variable. Note that Javascript is case sensitive, so new array() would give you an error, you need to specify new Array();
Note also I am going to call it emails instead of cars, as we are going to store emails but not cars 🙂
var emails = new Array();
Then, we need to iterate over the incidents to get the emails. Note that we need to do the iteration over the query which gave us the incident. For that we use gr.
gr.hasNext() would ask if there is still any record in the result set we have not yet iterate over.
gr.next() moves the result set to the next record and also tell us whether there is a record to look up.
so we can simplify the loop to
while(gr.next()){
var caller = gr.caller_id;
emails.push(caller.email);
}
In there, gr.caller_id is accessing the field caller_id in the incident record we are iterating over. Then, caller.email would look up the user record (the caller) and give us the email field. We just push that to the emails array by using push.
Finally we return the array we have been populating.
return emails;
To sum up, your script would like like this
var getUserEmail = function(){
var gr = new GlideRecord('incident');
gr.setLimit(5);
gr.query();
var emails = new Array();
while(gr.next()){
var caller = gr.caller_id;
emails.push(caller.email);
}
return emails;
}
var test = getUserEmail();
gs.print(test);
Hope it helps.
Ginés.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2017 08:31 AM
Hi Rahamathullah,
Don't worry for the mistakes, at some point we all were learner.
First, you need to check the language syntax. I assume you are using Javascript for your script. In Javascript, you don't define a function as
function functionName{ ...
}
but you do
var functionName = function(parameter1, parameter2, ...){
...
}
Note also the difference between bracket types for defining the parameters and the body of the function.
In your case, as you don't have parameters the function would be like
var getUserEmail() = function{
...
}
Now, let's see the body of the function. If I understood correctly, you are trying to get the email of the caller for the first 5 incidents. In this case, you started well
var gr = new GlideRecord('incident');
gr.setLimit(5);
gr.query();
This would give us a result set where we can get the data of the 5 incidents.
We define our output variable. Note that Javascript is case sensitive, so new array() would give you an error, you need to specify new Array();
Note also I am going to call it emails instead of cars, as we are going to store emails but not cars 🙂
var emails = new Array();
Then, we need to iterate over the incidents to get the emails. Note that we need to do the iteration over the query which gave us the incident. For that we use gr.
gr.hasNext() would ask if there is still any record in the result set we have not yet iterate over.
gr.next() moves the result set to the next record and also tell us whether there is a record to look up.
so we can simplify the loop to
while(gr.next()){
var caller = gr.caller_id;
emails.push(caller.email);
}
In there, gr.caller_id is accessing the field caller_id in the incident record we are iterating over. Then, caller.email would look up the user record (the caller) and give us the email field. We just push that to the emails array by using push.
Finally we return the array we have been populating.
return emails;
To sum up, your script would like like this
var getUserEmail = function(){
var gr = new GlideRecord('incident');
gr.setLimit(5);
gr.query();
var emails = new Array();
while(gr.next()){
var caller = gr.caller_id;
emails.push(caller.email);
}
return emails;
}
var test = getUserEmail();
gs.print(test);
Hope it helps.
Ginés.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-15-2017 10:00 AM
Appreciate your help in getting clarified .