Inbound action call within inbound action

vstefano
Giga Contributor

I was wondering if there was a possible way to script a call to an inbound action within the script of another inbound action or if there is a good way to have an inbound action reclassify an email's receive type and run the gambit of the inbound actions with the new reply type.

Here is my scenario:

Implementation partner built a custom table that gets "called" from an inbound action which acts similarly to inbound actions but separate. It governs field mapping when creating a new task of a specific type but is only called when an email receive type is "new". We have a need to essentially reclassify the receive type of an email from Reply to New when it identifies the email a reply to an inactive task so that it can be reprocessed as if it were a   new email to create a new ticket.

Is there a good way to do this?

11 REPLIES 11

Jim Coyne
Kilo Patron

I have not tried this, but yes, it should be possible.   You will need to have the "Ordered Email Processing" Plugin enabled.



In the block of code where you decide whether the email should be reprocessed or not, add the following lines:



//stop further processing of the Email


event.state="stop_processing";



//update the Email record


sys_email.receive_type = "new";


sys_email.update();



//and then reprocess from the start


//code taken from the "Reprocess Email" UI Action


var evt = new GlideRecord('sysevent');


evt.initialize();


evt.process_on = gs.nowDateTime();


evt.name = "email.read";


evt.parm1 = sys_email.getValue("sys_id");


evt.insert();



That will tell your instance to stop processing the email, update the Email record to be of type "New" and then reprocess the email from the beginning.



Again, I have not actually tried it, but that should work, in theory.


Thanks for the reply Jim!



I'll have to do a little more research on this and see if I can get it to work. I have attempted to manually change the reply type then reprocess but when doing that it acts as if it was a brand new email and changes it back to reply as it was running that check. It may act different with the script. I'll let you know if I find anything out.



Thanks again!


I have found a workaround in a sense. I was unable to find a way to call back to an inbound action but rather a way to achieve what i needed.



I had to copy the code I needed in the Lower order Inbound Action into a new Inbound Action I called "Reply to Closed Incident". I have to make a few more modifications so that another inbound action is encompassed here but that one is larger and is coming after I thoroughly test this.



-----------



Target Table = Incident


Type = Reply



The key pieces to my script are as follows (Note: we have a custom task called Intake ['ticket'] rather than call but it functions about the same):



//Condition - we use an active state called resolved that transitions to closed after 3 business days



current.active == false || current.state.getDisplayValue()=="Resolved"



//Script



createIntakeFromEmail();



function createIntakeFromEmail() {



        //Initialize the task you will want to create, ours is an intake (table name of 'ticket')



        var Intake = new GlideRecord ('ticket');


        Intake.initialize();



        //Calculate and set some values....



        var user = getUser(); //Function call. Function is listed below



        if(user && user.user_name != 'guest') {


                      Intake.u_requested_by = user.sys_id;


                      Intake.u_requested_for = user.sys_id;


                      Intake.location = user.location;


                      Intake.company = user.company;


            }



            else{


                      //set default company on intake record


                      var ccgr = new GlideRecord('core_company');


                      ccgr.addQuery('primary',true);


                      ccgr.setLimit(1);


                      ccgr.query();


                      if(ccgr.next()){


                                var cid = ccgr.sys_id.toString();


                                Intake.company = cid;


                    }


            }



              // Set more values


              //Short Description to indicate what task was replied to in the subject and what state it is in



              Intake.short_description = "Reply to " + current.state.getDisplayValue() + " task: " + current.number;


              Intake.description = email.body_text;


              Intake.contact_type = 'email';



              //Insert the intake



              Intake.insert();



              //Update the sys_email record to indicate the proper target which ended up being the newly created ticket



              var em = new GlideRecord('sys_email');


              em.addQuery('uid','=',sys_email.uid);


              em.query();


              if(em.next()){


                        em.target_table = 'ticket';


                        em.instance = Intake.sys_id;


                          //Uncomment next line for log write if needed


                          // gs.log("INTAKE #: " + Intake.number +   "Instance is: " + em.instance + "Sys Id: " + Intake.sys_id + "document id: " + Intake.sys_id);


                        em.update();


          }


       


        //Exit the inbound actions so no further processing occurs.


          event.state="stop_processing";



}



function getUser() {


            var userRec = new GlideRecord('sys_user');


            userRec.get(gs.getUserID());


            return userRec;


}


Hi Stefano,



I am having similar requirement , I tried the way you have mentioned. Butthe new record is is not getting updated as the instance for replied mail.


Please let me know if am missing anything here.



Thanks,


Sitarani