Trying to create task from inbound email action.

trmiller0805
Kilo Explorer

I know it's not best practice but I am not the decision maker so.... rather than make our IT personal actually log in to Service Now and create a ticket, management wants them to be able to send an email to a person within IT, CC the servicenow instance and have service now create a task and assign it to the person in the To field.

 

I have a condition that says:   if email.copied = hpincdev@service-now.com

 

 

(I copied another inbound action then got help from some people I know, I am not a scripter........yet)

 

current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;

current.short_description = email.subject;

 

var eto = email.direct.split(",");

for(var i=0; i<eto.length; i++)

{

    var isUser=getUserFromEmail(eto[i]);

    if(isUser){

  current.assigned_to = isUser;

    break;

  }

}

 

function getUserFromEmail(toemail){

    var gr = new GlideRecord("sys_user");

    if(gr.get("email",toemail)){

  }return gr;}

5 REPLIES 5

justin_drysdale
Mega Guru

I have too much experience making inbound actions behave outside the box. I guess that is a good thing when these issues come up.   Here is a basic setup for what you want:



Assumptions:


-To: field person is going to be the assigned_to person on the INC (assuming incident ticket).


-Email To: field is going to be the caller_id.


-Email Body will be the INC description.


-Email Subject will be the INC short_description.


-Technicians creating/receiving the work are part of the same assignment group.


-Do not create duplicate tickets (ticket from tech shouldn't trigger other inbound actions.


  (for this one, You will have to conditionalize this and/or other inbound actions so they run only when you want   them to. It is hard to say how to set these conditions


      up without knowing what your other current inbound actions are conditioned for.   )        



Inbound Action:


From: <leave blank>


Type: New


Target Table: Incident [incident]


Condition: sys_email.headers.toLowerCase().indexOf('CC:' + 'your_service_now_instance_email_address');//use your instance email address


                  //This is the base condition, you might have to add more.



Script:


//Setting up the Incident:


current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;


current.short_description = email.subject;


current.description = email.body_text;


current.caller_id = gs.createUser(email.to);


current.assignment_group.setDisplayValue("Service Desk"); //or whatever the assignment group is


current.state = 1;//set state/priority/urgency/etc...



current.insert();


I don't care so much about the assignment group as I do the assigned_to field.   We have the OOB inbound action working for creating an incident.   It automatically assigns it to the service desk and then the service desk determines if it goes somewhere else or not.   In this case, i used the task table and I had all the fields I cared about being filled in.   We want the assigned_to to be whoever the email was sent to.   That person can open the task and add the assignment group and due date.


I used:


current.assigned_to = (email.to);



Then I used:


var to = gs.getUserID(email.direct);



Then someone told me that because the email to and the email direct are both lists i had to tell service now that it was a list and how to split it and get the user id of the first email in the email to field.   We only care about the first email if there is more than one.   The script i posted above was what that person sent me to look at the to field list and split it and get the user ID....... it doesn't work.


current.assigned_to = (email.to);   will not do much for you.   In our instance gs.createUser(email.to); seems to do the trick resolving the user based on email address...   I just read that gs.createUser   is depreciated starting with Calgary (we are still on Aspen) so that is something I will have to address when we upgrade.



Since email.to is a list, to get the first value:



var email_addresses = email.to.split(","); //Assuming the delimiter in the list is a comma.


current.assigned_to = email_addresses[0];//grabs everything before the first comma in the list



You can always log out the list as well to see exactly what you are working with:


gs.log("First address = " + email_addresses[0] + " Second address = " + email_addresses[1]);


Matthew Swarts
Tera Guru

Hi tr,



I think you are on the right track with your function above.   Try using the following and see if it returns the desired result.



var eto = email.direct.split(",");


for(var i=0; i<eto.length; i++)


{


    var isUser=getUserFromEmail(eto[i]);


    if(isUser.length >= 1){


              current.assigned_to = isUser;


              break;


  }


}



function getUserFromEmail(toemail){


    var assigned = "";


    var gr = new GlideRecord("sys_user");


    gr.addQuery("email",toemail);  


    gr.query();


    while(gr.next())   {


              assigned = gr.name;


  }


    return assigned;


}