Inbound email action

test1231998
Tera Contributor

gs.include('validators');

if (current.getTableName() == "incident" && current.incident_state !='7' && current.incident_state !='6' )
 {
        
    //gs.log('Enter');
    var str=email.body_text;
    current.comments ="reply from: " + email.origemail + "\n\n" + str;
    current.update();
}

else if(current.incident_state == '7' || current.incident_state=='6')

    
 
{
    gs.log('Enter 2');
    var callNew = new GlideRecord('new_call');
    if( email.origemail.toLowerCase() == 'abc.example.com') {
        //if( email.from == 'SNOW.Notifications@client.com') {
    //if( email.from == 'abc.example.com'){
        //if( email.subject.toLowerCase().indexOf('closed') >= 0 );
        gs.log('sh name');
    callNew.contact_type = "email";
        //GlideSysAttachment.copy('sys_email', sys_email.sys_id, 'incident', incident.sys_id);
    //var emailAttachmentObj = new GlideSysAttachment();
//var bytesContent = emailAttachmentObj.getBytes('sys_email', sys_email.sys_id);
        //GlideSysAttachment.copy('sys_email', sys_email.sys_id, 'incident');
    callNew.caller = current.caller_id;
    callNew.short_description = current.short_description; //assuming you want incident short description in call record. else you can have earler statement only
        var str=email.body_text;
    callNew.description  = "**CREATED FROM REPLAY TO Resolved TICKET**\n\nTicket #:" + current.number +"reply from: " + email.origemail + "\n\n" + str;
        gs.log('email');
    callNew.update();
    //callNew.description = "**CREATED FROM REPLAY TO Resolved TICKET**\n\nTicket #:" + current.number + email.body;
    
        


       gs.log('End');
        callNew.insert();
    }

}

 

 

 

Hi Developer community,

I am new to servicenow

Can anyone please explain me this script which is used in inbound email action with target table as incident.

1 ACCEPTED SOLUTION

psphanindrakuma
Tera Guru
Tera Guru

Hi Deepika,

if (current.getTableName() == "incident" && current.incident_state != '7' && current.incident_state != '6') {

  //gs.log('Enter');
  var str = email.body_text;
  current.comments = "reply from: " + email.origemail + "\n\n" + str;
  current.update();
}

 

This must be a reply inbound action. in the above if part  there is a mistake

(current.incident_state != '7' || current.incident_state != '6')
 
Inbound action finds the incident record according to the reference watermark in the received email. If the incident state is not resolved or closed then inbound action updates the comments in incident.

else if (current.incident_state == '7' || current.incident_state == '6') {
  gs.log('Enter 2');
  var callNew = new GlideRecord('new_call');
  if (email.origemail.toLowerCase() == 'abc.example.com') {
    //if( email.from == 'SNOW.Notifications@client.com') {
    //if( email.from == 'abc.example.com'){
    //if( email.subject.toLowerCase().indexOf('closed') >= 0 );
    gs.log('sh name');
    callNew.contact_type = "email";
    //GlideSysAttachment.copy('sys_email', sys_email.sys_id, 'incident', incident.sys_id);
    //var emailAttachmentObj = new GlideSysAttachment();
    //var bytesContent = emailAttachmentObj.getBytes('sys_email', sys_email.sys_id);
    //GlideSysAttachment.copy('sys_email', sys_email.sys_id, 'incident');
    callNew.caller = current.caller_id;
    callNew.short_description = current.short_description; //assuming you want incident short description in call record. else you can have earler statement only
    var str = email.body_text;
    callNew.description = "**CREATED FROM REPLAY TO Resolved TICKET**\n\nTicket #:" + current.number + "reply from: " + email.origemail + "\n\n" + str;
    gs.log('email');
    callNew.update();
    //callNew.description = "**CREATED FROM REPLAY TO Resolved TICKET**\n\nTicket #:" + current.number + email.body;




    gs.log('End');
    callNew.insert();
  }
If the incident state is resolved or closed, then else pard creates a new call record in the new call table.
 
you don't need to use update() and insert() both at the same time. you can use insert().
 
Please mark the comment as a correct answer and helpful if this answers your question.
 
Regards,
Phanindra
 
 

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

whenever email is received to ServiceNow the inbound action would be processed

1) it will check if the table is incident; if incident state is not closed or resolved

a) it would update

Else it would insert new record with the fields populated in new_call table

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

asifnoor
Kilo Patron

Hello Deepika,

This is a inbound action which will trigger whenever an email is sent to ServiceNow.

Coming to this inbound action, this is a reply mail to an existing record.

1. it basically checking first if the table name is incident and the state of the incident is NOT resolved or closed. If it matches, then it will update the incident with the body of the mail under comments.

2. If it does not match, then it checks if the current incident is resolved or closed. If it matches, then it will create a new incident (becuase the existing incident is resolved/closed).

Hope this clarifies.

Kindly mark the comment as a correct answer and helpful if this answers your question.

 

psphanindrakuma
Tera Guru
Tera Guru

Hi Deepika,

if (current.getTableName() == "incident" && current.incident_state != '7' && current.incident_state != '6') {

  //gs.log('Enter');
  var str = email.body_text;
  current.comments = "reply from: " + email.origemail + "\n\n" + str;
  current.update();
}

 

This must be a reply inbound action. in the above if part  there is a mistake

(current.incident_state != '7' || current.incident_state != '6')
 
Inbound action finds the incident record according to the reference watermark in the received email. If the incident state is not resolved or closed then inbound action updates the comments in incident.

else if (current.incident_state == '7' || current.incident_state == '6') {
  gs.log('Enter 2');
  var callNew = new GlideRecord('new_call');
  if (email.origemail.toLowerCase() == 'abc.example.com') {
    //if( email.from == 'SNOW.Notifications@client.com') {
    //if( email.from == 'abc.example.com'){
    //if( email.subject.toLowerCase().indexOf('closed') >= 0 );
    gs.log('sh name');
    callNew.contact_type = "email";
    //GlideSysAttachment.copy('sys_email', sys_email.sys_id, 'incident', incident.sys_id);
    //var emailAttachmentObj = new GlideSysAttachment();
    //var bytesContent = emailAttachmentObj.getBytes('sys_email', sys_email.sys_id);
    //GlideSysAttachment.copy('sys_email', sys_email.sys_id, 'incident');
    callNew.caller = current.caller_id;
    callNew.short_description = current.short_description; //assuming you want incident short description in call record. else you can have earler statement only
    var str = email.body_text;
    callNew.description = "**CREATED FROM REPLAY TO Resolved TICKET**\n\nTicket #:" + current.number + "reply from: " + email.origemail + "\n\n" + str;
    gs.log('email');
    callNew.update();
    //callNew.description = "**CREATED FROM REPLAY TO Resolved TICKET**\n\nTicket #:" + current.number + email.body;




    gs.log('End');
    callNew.insert();
  }
If the incident state is resolved or closed, then else pard creates a new call record in the new call table.
 
you don't need to use update() and insert() both at the same time. you can use insert().
 
Please mark the comment as a correct answer and helpful if this answers your question.
 
Regards,
Phanindra