locked
Workflow not executing when called programmatically RRS feed

  • Question

  • Hi All,

    I have created an "On Demand" workflow whose functionality is to send emails. This workflow is being called by a windows service using ExecuteWorkflowRequest Method. I am passing the ID of the opportunity entity on which the workflow has to be executed as well as the parent workflow ID in the request.

    Workflow is invoked on the opportunity but it goes in waiting state after invocation. If I manually invoke the workflow using "Run Workflow" it gets executed.

    Following is the code that is used in Windows Service to invoke the workflow:

     

    // Create an ExecuteWorkflow request.

     

    ExecuteWorkflowRequest request = new ExecuteWorkflowRequest();

     

    //Assign the ID of the workflow you want to execute to the request.

     

    // use this query to get the id select parentworkflowid,name,* from dbo.Workflow

     

    // id is the parentworkflowid

    request.WorkflowId =

    new Guid("CE3C112F-984F-4243-8C1F-731255157393");

    request.EntityId =

    new Guid(opp3.opportunityid.Value.ToString());

     

    ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)svc.Execute(request);

     

    Following is the error when the workflow is in Waiting State:

    Workflow paused due to error: Unhandled Exception: System.Reflection.TargetInvocationException: The following error was encountered while executing method Compiled.Workflow1f1694d1e722e01188fd00215a495844.GetEntityProperty: Invalid type conversion  at System.Workflow.Activities.Rules.MethodInvokeExpression.Evaluate(CodeExpression expression, RuleExecution execution)  at System.Workflow.Activities.Rules.RuleExpressionWalker.Evaluate(RuleExecution execution, CodeExpression expression)  at System.Workflow.Activities.Rules.MethodInvokeExpression.Evaluate(CodeExpression expression, RuleExecution execution)  at System.Workflow.Activities.Rules.RuleExpressionWalker.Evaluate(RuleExecution execution, CodeExpression expression)  at System.Workflow.Activities.Rules.MethodInvokeExpression.Evaluate(CodeExpression expression, RuleExecution execution)  at System.Workflow.Activities.Rules.RuleExpressionWalker.Evaluate(RuleExecution execution, CodeExpression expression)  at System.Workflow.Activities.Rules.MethodInvokeExpression.Evaluate(CodeExpression expression, RuleExecution execution)  at System.Workflow.Activities.Rules.RuleExpressionWalker.Evaluate(RuleExecution execution, CodeExpression expression)  at System.Workflow.Activities.Rules.MethodInvokeExpression.Evaluate(CodeExpression expression, RuleExecution execution)  at System.Workflow.Activities.Rules.RuleExpressionWalker.Evaluate(RuleExecution execution, CodeExpression expression)  at System.Workflow.Activities.Rules.MethodInvokeExpression.Evaluate(CodeExpression expression, RuleExecution execution)  at System.Workflow.Activities.Rules.RuleExpressionWalker.Evaluate(RuleExecution execution, CodeExpression expression)  at System.Workflow.Activities.Rules.MethodInvokeExpression.Evaluate(CodeExpression expression, RuleExecution execution)  at System.Workflow.Activities.Rules.RuleExpressionWalker.Evaluate(RuleExecution execution, CodeExpression expression)  at System.Workflow.Activities.Rules.ExpressionStatement.Execute(RuleExecution execution)  at System.Workflow.Activities.Rules.RuleStatementAction.Execute(RuleExecution context)  at System.Workflow.Activities.Rules.Executor.ExecuteRuleSet(IList`1 orderedRules, RuleExecution ruleExecution, Tracer tracer, String trackingKey)  at System.Workflow.Activities.Rules.RuleEngine.Execute(RuleExecution ruleExecution)  at System.Workflow.Activities.Rules.RuleSet.Execute(Activity activity, ActivityExecutionContext executionContext)  at System.Workflow.Activities.PolicyActivity.Execute(ActivityExecutionContext executionContext)  at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(T activity, ActivityExecutionContext executionContext)  at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(Activity activity, ActivityExecutionContext executionContext)  at System.Workflow.ComponentModel.ActivityExecutorOperation.Run(IWorkflowCoreRuntime workflowCoreRuntime)  at System.Workflow.Runtime.Scheduler.Run() Inner Exception: Microsoft.Crm.CrmNotSupportedException: Invalid type conversion  at Microsoft.Crm.Workflow.ValueConverters.ValueConverterBase.ConvertTo(Type targetType, Object sourceValue, String entityName, String attributeName)  at Microsoft.Crm.Workflow.Services.CrmWorkflowService.ConvertToCrmTypeWithMetadata(Type targetType, Object sourceValue, String entityName, String attributeName)  at Microsoft.Crm.Workflow.Services.CrmWorkflowService.FormatValue(Object sourceValue, String entityName, String attributeName)  at Microsoft.Crm.Workflow.CrmWorkflow.GetEntityProperty(DynamicEntity entity, String propertyName) Inner Exception: System.Web.Services.Protocols.SoapException: Server was unable to process request. Detail: <detail><error>  <code>0x80040217</code>  <description>usersettings With Id = a5e29fd8-3f1d-df11-b5ec-00215a495844 Does Not Exist</description>  <type>Platform</type> </error></detail>  at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)  at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)  at Microsoft.Crm.SdkTypeProxy.CrmService.Execute(Request Request)  at Microsoft.Crm.Asynchronous.SdkTypeProxyCrmServiceWrapper.Execute(Object request)  at Microsoft.Crm.Asynchronous.UserSettingsCacheLoader.RetrieveUserSettings(ICrmService crmService, Guid userId)  at Microsoft.Crm.Asynchronous.UserSettingsCacheLoader.LoadCacheData(Guid userId, IOrganizationContext context)  at Microsoft.Crm.Caching.BasicCrmCache`2.CreateEntry(TKey key, IOrganizationContext context)  at Microsoft.Crm.Caching.BasicCrmCache`2.LookupEntry(TKey key, IOrganizationContext context)  at Microsoft.Crm.Workflow.WorkflowUserContext.get_User()  at Microsoft.Crm.Workflow.ValueConverters.ValueConverterBase.FormatEnum(Object rawValue)  at Microsoft.Crm.Workflow.ValueConverters.PicklistConverter.InternalConvert(Type targetType, Object value)  at Microsoft.Crm.Workflow.ValueConverters.ValueConverterBase.ConvertTo(Type targetType, Object sourceValue, String entityName, String attributeName)

    I hope this helps to explain my issue. Thanks in Advance for your replies.

     

    Wednesday, January 19, 2011 4:10 PM

Answers

  • I didn't read carefully enough to realize that you were using SYSTEM account to invoke the workflow. 

    When it comes to SYSTEM account and workflow, there are two general rules that you should be aware: 

    1. Don't let SYSTEM user account own an automatic workflow
    2. Don't invoke a workflow using the SYSTEM account

    The reason for that is, the SYSTEM account is a special account, which doesn't actually have a complete user profile. 

    Hope this can help clarify the confusion that you may have. 

    Daniel Cai | http://danielcai.blogspot.com
    Thursday, January 20, 2011 2:42 PM

All replies

  • If the workflow is in waiting state, it means your Windows Services code has just worked fine. It's the problem of the workflow itself, where you may want to spend time to troubleshoot. 

    When it comes to the workflow with emails, you need to make sure the recipient has email addressed entered in the CRM system. 


    Daniel Cai | http://danielcai.blogspot.com
    Wednesday, January 19, 2011 4:19 PM
  • The issue appears to be highlighted in this line:

    usersettings With Id = a5e29fd8-3f1d-df11-b5ec-00215a495844 Does Not Exist

    Particularly as it is working from within the CRM, but not from your service.  How are you authenticating with the CRM from your Windows Service?  Ensure that either the credentials you are passing, or the user under which your service is running (if default credentials) is a valid CRM User with appropriate roles etc.

    Perhaps try setting the service's logon properties to the same user under which the CRM Async service is running.


    --pogo (pat)
    Wednesday, January 19, 2011 11:27 PM
  • It is the 'SYSTEM' user. When I posted this the user was disabled. I enabled the user but still the same error. I think the system user does not have the rights to execute Workflow due to which it has been failing.

    I went through the following thread: http://social.microsoft.com/Forums/en/crmdevelopment/thread/5c16762e-075c-457c-bd62-753394b22885

    It said either I have to install Update Rollup 12 or I have to execute the workflow using the Admin Credentials.

    Right now the credentials is default. So now I am setting it as Admin User.

    Thursday, January 20, 2011 6:11 AM
  • <description>usersettings With Id = a5e29fd8-3f1d-df11-b5ec-00215a495844 Does Not Exist</description>

    just check is the workflow is in User level or Organization level

    and whether the user credential you are using in your code is having the role with workflow execute permossion.

    just reverify these two things.


    yes.sudhanshu

    http://bproud2banindian.blogspot.com
    http://ms-crm-2011-beta.blogspot.com
    Thursday, January 20, 2011 8:10 AM
  • I didn't read carefully enough to realize that you were using SYSTEM account to invoke the workflow. 

    When it comes to SYSTEM account and workflow, there are two general rules that you should be aware: 

    1. Don't let SYSTEM user account own an automatic workflow
    2. Don't invoke a workflow using the SYSTEM account

    The reason for that is, the SYSTEM account is a special account, which doesn't actually have a complete user profile. 

    Hope this can help clarify the confusion that you may have. 

    Daniel Cai | http://danielcai.blogspot.com
    Thursday, January 20, 2011 2:42 PM