Create Task via Escalation Rule

SOLVED

Hi 

a customer wants a follow up task created when a new case is entered - i though this would have been possible via the workflow but looks like the create new task action is not available on primary workflow rules so thought i would try it as an escalation rule but there is not option for creating a new task on their either

whats the best way around this issue? has anyone done this before that could post some sample code please?

  • 0

    Hi

    You are correct.  Primary Workflow Rules cant create Tasks because follow up Workflow Actions assume that the record has been saved.  I've covered this here:  Create a reminder task for a user when an opportunity is inserted. - Sage CRM Hints, Tips and Tricks - Sage CRM - Sage City Community

    And you can use Escalation Rules and Notifications to do what you want too. 

  • 0 in reply to Sage CRM

    thanks for the reply - ive tried this for cases but im getting the following error:

    this is my tablescript:

    function InsertRecord()

    {

    // Handle insert record actions here

    }


    function PostInsertRecord()

    {

    var newCaseRecord = CRM.FindRecord("cases",WhereClause);
    var intCaseID = newCaseRecord.Case_casesid;
    var intCaseUserID = newCaseRecord.Case_assigneduserid;
    var intPersID = newCaseRecord.Case_primarypersonid;
    var intCompID = newCaseRecord.Case_primarycompanyid;
    var PersRecord = CRM.FindRecord("person","pers_personid="+intPersID);
    var CompRecord = CRM.FindRecord("company","comp_companyid="+intCompID);
    var intChannelID = CRM.GetContextInfo('user','user_primarychannelid');

    var strMsg ="";
    strMsg = "Follow up case - please contact ";
    strMsg += PersRecord.pers_firstname +" ";
    strMsg += PersRecord.pers_lastname+" of ";
    strMsg += CompRecord.comp_name+ " regarding ";
    strMsg + =newCaseRecord.Case_description;

    var CommRecord = CRM.CreateRecord("Communication");

    CommRecord.Comm_ChannelId = intChannelID;
    CommRecord.Comm_Type = 'Task';
    CommRecord.Comm_Action = 'ToDo';
    CommRecord.Comm_Status = 'Pending';
    CommRecord.Comm_Priority = 'Normal';
    CommRecord.Comm_Note = strMsg;
    CommRecord.Comm_Description = strMsg;
    CommRecord.comm_casesid = intCaseID;
    CommRecord.SaveChanges();

    var intCommID = CommRecord.comm_communicationid;
    var CmLiRecord = CRM.CreateRecord('comm_link');
    CmLiRecord.CmLi_Comm_UserId = intCaseUserID; CmLiRecord.CmLi_Comm_CommunicationId = intCommID;
    CmLiRecord.CmLi_Comm_PersonId = intPersID;
    CmLiRecord.CmLi_Comm_CompanyId = intCompID;
    CmLiRecord.SaveChanges();

    }


    function UpdateRecord()

    {

    // Handle update record actions here

    }


    function DeleteRecord()

    {

    // Handle delete record actions here

    }

    are you able to advise where i have gone wrong please - also how can i use escalation rules to create a task as there is no create task option on escalation rules - and notifications only seem to allow on screen notifications or email notifications no tasks as such

  • +1 in reply to SIQ
    verified answer

    Hi SIQ

    I offer another route here, I do these by SQL and Stored Procedures - the Workflow / Escalation rule calls on the SP and the SP does the heavy lifting. 

    For example the following would create an appointment against an opportunity, but you can change the Comm_Type entry from Appointment to Task. 

    I use this as my base script and over time included more things into it (e.g. the follow up date defaults to 2 days in the future but ignores weekends) 

    CREATE PROCEDURE uspCreateOppoApp
    @Opportunityid int

    AS
    declare @commid int
    declare @commlinkid int
    declare @companyid int,
    decalre @personid int
    declare @currentdatetime datetime
    declare @territoryid int
    declare @Location nVarChar(255)
    declare @description NVarChar(50)
    declare @details nVarChar(MAX)
    declare @UserID INT

     set @currentdatetime = GETDATE()
     
    BEGIN
          select 
                @companyid = oppo_primarycompanyid,
                @personid = oppo_primarypersonid,
                @territoryid=oppo_secterr, 
                @UserID = oppo_assigneduserid,
                @description = oppo_description,
                @Details=oppo_note,
                @Location=RTrim(Comp_Name)+' - '+RTrim(Addr_Address1)+', '+RTrim(Addr_City)+', '+RTrim(Addr_PostCode)
          from opportunity with(nolock) 
          LEFT OUTER JOIN Company with(nolock) ON Oppo_PrimaryCompanyId = Comp_CompanyID
          LEFT OUTER JOIN Address with(nolock) ON comp_primaryaddressid = addr_addressid
          where oppo_opportunityid=@opportunityid
          
          INSERT INTO [Communication]
                ([Comm_Type]
                ,[Comm_Action]
                ,[Comm_Status]
                ,[Comm_DateTime]
                ,[Comm_ToDateTime]
                ,[Comm_CreatedBy]
                ,[Comm_CreatedDate]
                ,[Comm_UpdatedBy]
                ,[Comm_UpdatedDate]
                ,[Comm_TimeStamp]
                ,[Comm_SecTerr]
                ,[Comm_OpportunityId]
                ,[Comm_Organizer]
                ,[Comm_Subject]
                ,[Comm_Location]
                ,[comm_note])
               VALUES
                ('Appointment'
                ,'PhoneOut'
                ,'Pending'
                ,@currentdatetime
                ,@currentdatetime
                ,1
                ,@currentdatetime
                ,1
                ,@currentdatetime
                ,@currentdatetime
                ,@territoryid
                ,@Opportunityid
                ,@userid
                ,@description
                ,@location
                ,@details)  
                
                SELECT @commid = @@IDENTITY
              
    INSERT INTO [Comm_Link]
                ([CmLi_Comm_CommunicationId]
                ,[CmLi_CreatedBy]
                ,[CmLi_CreatedDate]
                ,[CmLi_UpdatedBy]
                ,[CmLi_UpdatedDate]
                ,[CmLi_TimeStamp]
                ,[CmLi_Comm_CompanyId]
                ,[CmLi_Comm_PersonId]
                ,[CmLi_Comm_UserId])
              VALUES
                (@commid
                ,1
                ,@currentdatetime
                ,1
                ,@currentdatetime
                ,@currentdatetime
                ,@companyid
                ,@personid
                ,@userid)
       end
  • 0 in reply to Matthew Shaw

    Hi Matthew

    thanks for this, it works brilliantly! - are you able to post the code set set the follow up to 2 days ignoring weekends please, i want it to set the days 7 days in the future but not taking into account weekends

  • 0 in reply to SIQ

    What you need is the following e.g. 

    SET @Currentdate = CASE WHEN DATEPART(dw,GETDATE()+7) IN (7,1) THEN GETDATE()+9 END

    if +7 hits a Saturday or Sunday then it add an additional 2 days to it to get it past the weekend.

  • 0 in reply to Matthew Shaw

    brill thanks very much!

  • 0 in reply to SIQ

    Sorry I was a bit quick in my answer you need the full bit (I forgot the 'ELSE')

    CASE WHEN DATEPART(dw,GETDATE()+7) IN (7,1) THEN GETDATE()+9  ELSE GETDATE()+7 END