locked
I am not able to call custom workflow when i receive email in mscrm4 RRS feed

  • Question

  • Hi,

    I am not able to call custom workflow when i receive email in mscrm4.

    I wrote the code like below

    // Input property

     

    public static DependencyProperty senderProperty = DependencyProperty.Register("sender", typeof(string), typeof(ContactWorkflow));

    [

    CrmInput("Sender")]

     

    public string sender

    {

     

    get

    {

     

    return (string)base.GetValue(senderProperty);

    }

     

    set

    {

     

    base.SetValue(senderProperty, value);

    }

    }

     

    // Output property

     

    public static DependencyProperty contactIdProperty = DependencyProperty.Register("contactId", typeof(Lookup), typeof(ContactWorkflow));

    [

    CrmOutput("ContactId")]

    [

    CrmReferenceTarget("contact")]

     

    public Lookup contactId

    {

     

    get

    {

     

    return (Lookup)base.GetValue(contactIdProperty);

    }

     

    set

    {

     

    base.SetValue(contactIdProperty, value);

    }

    }

     

    private Guid MatchSenderWithExistingContact(ICrmService crmService, string fromAddress)

    {

     

    try

    {

     

    ConditionExpression con = new ConditionExpression();

    con.AttributeName =

    "emailaddress1";

    con.Operator =

    ConditionOperator.Equal;

    con.Values =

    new object[] { fromAddress };

     

    ConditionExpression[] conarr = { con };

     

    FilterExpression filter = new FilterExpression();

    filter.FilterOperator =

    LogicalOperator.And;

     

    QueryExpression query = new QueryExpression();

    query.EntityName =

    "contact";

    query.ColumnSet =

    new AllColumns();

     

    RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();

    retrieve.Query = query;

    retrieve.ReturnDynamicEntities =

    false;

     

    RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)crmService.Execute(retrieve);

     

    Guid contactId = Guid.Empty;

     

     

     

    foreach (BusinessEntity busEntity in retrieved.BusinessEntityCollection.BusinessEntities)

    {

    contactId = ((Microsoft.Crm.SdkTypeProxy.

    contact)(busEntity)).contactid.Value;

     

    break;

    }

     

    return contactId;

    }

     

    catch (Exception ex)

    {

     

    throw new Exception("Error:", ex);

    }

    }

     

    protected override System.Workflow.ComponentModel.ActivityExecutionStatus Execute(System.Workflow.ComponentModel.ActivityExecutionContext executionContext)

    {

     

    try

    {

     

    IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));

     

    IWorkflowContext context = contextService.Context;

     

    // Obtain IcrmService so we can call into CRM SDK to retrieve

     

    // contacts

     

    ICrmService crmService = context.CreateCrmService();

     

    // this.sender property will have the email address that needs to be matched.

     

    Guid contactId = MatchSenderWithExistingContact(crmService, this.sender);

     

    // Set the contactId output property to return this data

     

    // back to the calling workflow

     

    this.contactId = new Lookup("contact", contactId);

     

    return ActivityExecutionStatus.Closed;

    }

     

    catch (Exception ex)

    {

     

    throw new Exception("Error:", ex);

    }

    }

    Please let me know how can i achieve this.

    Thanks,
    Naveen

    Friday, March 5, 2010 7:13 AM

Answers

  • Hi Naveen,

    Can u apply a Wait condition on EmailStatus(email.statuscode) and wait unit the status become received.
    somethign like this.
               Wait   EmailStatusCode Equals Received
                         Trigger your workflow

    ( However i wonder, CRM is differenting between Received & Sent because Recieved acutally means recieved on the client).

    EmailStatus Class (Sdk Assembly)

    This class contains integer flags to set the email.statuscode property. Although the property may accept any value, these are the valid values.

    This static class does not need instantiation or initialization because the fields represent constant integer values.

    The EmailStatus class exposes the following members.

    Field Value Description
    Canceled 5 Specifies that the e-mail status is canceled.
    Completed 2 Specifies that the e-mail status is completed.
    Draft 1 Specifies that the e-mail status is draft.
    Failed 8 Specifies that the e-mail status is failed.
    PendingSend 6 Specifies that the e-mail status is pending send.
    Received 4 Specifies that the e-mail status is received.
    Sending 7 Specifies that the e-mail status is sending.
    Sent 3 Specifies that the e-mail status is sent.


    Muhammad Ali Khan
    My MS CRM blog
    Friday, March 5, 2010 10:18 AM

All replies

  • Hi Naveen,
    I am sorry but i couldn'g get your problem

    you have pasted some workflow code above. is there any issue with your workflow code? what error you are receiving, get the detail error by catching hte soapException (ex.Detail.ToString())

    Also you can debug your workflow code.

    'I am not able to call custom workflow when i receive email in mscrm4'

    Means the workflow is never triggered? you have to configure your workflow on "Email Entity Sent"
    Once the email is sent the above workflow can be triggered.



    Muhammad Ali Khan
    My MS CRM blog
    Friday, March 5, 2010 7:44 AM
  • Hi Ali,

    I configure the custom workflow on Email entity and select the Start when option as "Record is created".
    When i recieve new email in crm web application, my custom workflow is not triggering.

    If i sent email my same custom workflow is triggering.

    Please let me know required changes to trigger my custom workflow when i recieve new email.

    Thanks,
    Naveen 




       
    Friday, March 5, 2010 8:30 AM
  • Hi Naveen,
    I don't think there is any such out of the box functionality to trigger the workflow once the email is received.
    Muhammad Ali Khan
    My MS CRM blog
    Friday, March 5, 2010 8:32 AM
  • Hi Ali,

    Thnaks for your reply.

    So custom workflow will trigger only the case when sent the e-mail not in recieve right?


    Thanks,
    Naveen



     

    Friday, March 5, 2010 8:59 AM
  • Hi naveen,

    can you firstly please check whether any other workflow runs or not?

    if they are they make sure scope is selected as Organization on your current workflow and it should get fired...
    Friday, March 5, 2010 9:16 AM
    Answerer
  • Hi Mayan,

    This is the only custom workflow is running and i am not able to trigger this workflow when i recieve the new email.

    please suggest me.

    Thanks,
    Naveen
    Friday, March 5, 2010 9:29 AM
  • what is scope set for this workflow?

    Also any other normal workflow (not custom) is working or not?
    Friday, March 5, 2010 9:44 AM
    Answerer
  • Hi Naveen,

    Can u apply a Wait condition on EmailStatus(email.statuscode) and wait unit the status become received.
    somethign like this.
               Wait   EmailStatusCode Equals Received
                         Trigger your workflow

    ( However i wonder, CRM is differenting between Received & Sent because Recieved acutally means recieved on the client).

    EmailStatus Class (Sdk Assembly)

    This class contains integer flags to set the email.statuscode property. Although the property may accept any value, these are the valid values.

    This static class does not need instantiation or initialization because the fields represent constant integer values.

    The EmailStatus class exposes the following members.

    Field Value Description
    Canceled 5 Specifies that the e-mail status is canceled.
    Completed 2 Specifies that the e-mail status is completed.
    Draft 1 Specifies that the e-mail status is draft.
    Failed 8 Specifies that the e-mail status is failed.
    PendingSend 6 Specifies that the e-mail status is pending send.
    Received 4 Specifies that the e-mail status is received.
    Sending 7 Specifies that the e-mail status is sending.
    Sent 3 Specifies that the e-mail status is sent.


    Muhammad Ali Khan
    My MS CRM blog
    Friday, March 5, 2010 10:18 AM
  • Hi Ali,

    I set the conditions in workflow rule wizard like below

    1) Wait condition:               Email        Status Reason            Equals                                Received
    2) Check condition:            Email        Regarding                   Does Not Contain Data
    3) Calling custom workflow:Sender (Input parameter)
    4) Update Record:              Regarding (field set with contactid)

    Still i am not able to fire the custom workflow when i received the email.

    Please give me inputs.

    Thanks,
    Naveen

     
    Saturday, March 6, 2010 6:26 PM
  • As i told you i have doubt about "Recieved".
    ( However i wonder, CRM is differenting between Received & Sent because Recieved acutally means recieved on the client).

    If that is the case i am afriad, then you have no other option other than "Sent" only :(
    Muhammad Ali Khan
    My MS CRM blog
    Saturday, March 6, 2010 6:54 PM