locked
Stop ExecuteWorkflowResponse from exiting on Exception RRS feed

  • Question

  • Problem: The ExecuteWorkflowResponse call will throw an exception and exit if any of the records experience an issue during workflow execution.

    Objective: Have the foreach loop continue to execute the workflow against remaining records in the list of ID's if an exception is thrown for one of the records.  Additionally, have the information around a record's exception logged with the rest of the process logging.

    I'm not seeing anything around the webs for doing this, but am hopefully that someone here can point me in the right direction.  Here's the method I'm using to execute the workflow against all records in a view.  Keep in mind that I have a helper method that pulls the view into the process and places all record IDs into a List<Guid> that's then passed into this method...

    private bool ExecuteWorkflow(OrganizationServiceContext orgContext, Guid workflowId, List<Guid> recordIds)
    {
      bool isSuccess = true;
    
      try
      {
        foreach (Guid recordId in recordIds)
        {
          ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()
          {
            WorkflowId = workflowId,
            EntityId = recordId
          };
    
          ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)_serviceProxy.Execute(request);
    
          _flatLog.WriteLine(recordId);
        }
      }
      catch
      {
        isSuccess = false;
      }
    
      return isSuccess;
    }

    Wednesday, December 30, 2015 2:55 PM

Answers

  • hi Josh,

    actually the ExecuteWorkflow takes a single entity id as parameter, if you want to handle the exception you should move the try catch block just around the execution:

    private bool ExecuteWorkflow(OrganizationServiceContext orgContext, Guid workflowId, List<Guid> recordIds)
    {
      bool isSuccess = true;
    
        foreach (Guid recordId in recordIds)
        {
          ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()
          {
            WorkflowId = workflowId,
            EntityId = recordId
          };
    
      try
      {
          ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)_serviceProxy.Execute(request);
      }
      catch
      {
        isSuccess = false;
      }
          _flatLog.WriteLine(recordId);
        }
    
    
      return isSuccess;
    }
    in this way all the guids will be processed. in addition if you want instead of returning a bool, you can return a Dictionary<Guid, bool> containing the guids processed and the result associated


    My blog: www.crmanswers.net - CRM Theme Generator

    Wednesday, December 30, 2015 3:37 PM

All replies

  • hi Josh,

    actually the ExecuteWorkflow takes a single entity id as parameter, if you want to handle the exception you should move the try catch block just around the execution:

    private bool ExecuteWorkflow(OrganizationServiceContext orgContext, Guid workflowId, List<Guid> recordIds)
    {
      bool isSuccess = true;
    
        foreach (Guid recordId in recordIds)
        {
          ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()
          {
            WorkflowId = workflowId,
            EntityId = recordId
          };
    
      try
      {
          ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)_serviceProxy.Execute(request);
      }
      catch
      {
        isSuccess = false;
      }
          _flatLog.WriteLine(recordId);
        }
    
    
      return isSuccess;
    }
    in this way all the guids will be processed. in addition if you want instead of returning a bool, you can return a Dictionary<Guid, bool> containing the guids processed and the result associated


    My blog: www.crmanswers.net - CRM Theme Generator

    Wednesday, December 30, 2015 3:37 PM
  • So with the response call being outside the foreach loop, is that going to execute a response against every record in the set or just the last record that went through the foreach loop to build the request?
    Wednesday, December 30, 2015 4:01 PM
  • the execution (repeated for each record) is still inside the foreach loop

    My blog: www.crmanswers.net - CRM Theme Generator

    Wednesday, December 30, 2015 4:06 PM
  • Didn't see that; lemme give it a try.
    Wednesday, December 30, 2015 4:10 PM
  • Hey that works perfectly; end of the year, running out of brain power...

    Thank you for the assistance!

    Wednesday, December 30, 2015 6:36 PM