CRM - sending email to contacts in sub grid

夙愿已清 提交于 2019-12-08 08:08:12

问题


I work for a charity and we want to use our Dynamics 365 to support our fundraising team.  We have created a 'Fundraising Event' entity with an N:N relationship to the Contact entity (renamed to 'Individual' in our Org.  I have a sub grid on each entity so that we can add contacts to events either from the Contact record or from the event record. We need to be able to send emails to everyone registered for an event to wish them good luck before the event.  I have created a workflow against the 'Fundraising Event' entity to run 7 days before an event start date however when I an testing this the System job shows "Error; Needs Attention" and when I look at the process it shows a message "The email must have at least one recipient before it can be sent." Process error image In the WF send email properties, I have tried using 'Contact' and 'Named Contact(Lookup)' from the list I have available (see screen shot below) in the TO field but I get the same result with both options. To line option list image My test event has 2 Individuals (Contacts) linked to it and I can see them both in the sub grid on the event record but I can't seem to be able to send emails to them. Here are the details from a failed process:

Plugin Trace:
[Microsoft.Xrm.Sdk.Workflow: Microsoft.Xrm.Sdk.Workflow.Activities.SendEmail]
[SendEmailStep1]
Error Message:
Unhandled Exception:
System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: The e-mail must have at least one recipient before it can be sentDetail: 

<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">

<ActivityId>9161cfb2-308e-4b19-af39-b859a78f132f</ActivityId>

<ErrorCode>-2147218684</ErrorCode>

<ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />

<Message>The e-mail must have at least one recipient before it can be sent</Message>

<Timestamp>2017-03-31T11:07:27.6562583Z</Timestamp>

<ExceptionSource i:nil="true" />

<InnerFault>

<ActivityId>9161cfb2-308e-4b19-af39-b859a78f132f</ActivityId>

<ErrorCode>-2147218684</ErrorCode>

<ErrorDetails xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />

<Message>The e-mail must have at least one recipient before it can be sent</Message>

<Timestamp>2017-03-31T11:07:27.6562583Z</Timestamp>

<ExceptionSource i:nil="true" />

<InnerFault i:nil="true" />

<OriginalException i:nil="true" />

<TraceText i:nil="true" />

</InnerFault>

<OriginalException i:nil="true" />

<TraceText>[Microsoft.Xrm.Sdk.Workflow: Microsoft.Xrm.Sdk.Workflow.Activities.SendEmail]

[SendEmailStep1]

</TraceText>

</OrganizationServiceFault>

at Microsoft.Crm.Workflow.Services.SendEmailActivityService.Execute(ActivityContext executionContext, SendEmail sendEmail)

at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)

at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

Is it possible to do what I'm attempting using out of the box functionality? If so, what am I missing? Thanks, Paul


回答1:


What you are trying to achieve here is not possible just by configuration. You have this error because you are not referring to the contacts that you have in your subgrid, you are referring to an attribute named "Contact" which is probably empty and that is causing the error. The only way would be to create a Custom Workflow Activity, that will get all the necessary contacts and put it in your email. If you do not want to code, you can use Aiden Kaskela's workflow elements to achieve your goal:

https://kaskelasolutions.com/how-to-add-dynamic-recipients-to-an-email/




回答2:


We did something like this.

Register a plugin in Pre-operation of Email Create message & have this logic in there.

This will add the recipients from N:N contact intersect entity in email To list.

    ConditionExpression frCondition1 = new ConditionExpression();
    ConditionExpression frCondition2 = new ConditionExpression();
    EntityCollection frResponse = new EntityCollection();
    EntityCollection toOwner = new EntityCollection();

    toOwner = targetEntity.GetAttributeValue<EntityCollection>("to");

    frCondition1 = CreateConditionExpression("parentIdcolummn", ConditionOperator.Equal, new string[] { parent.Id.ToString() });
    frCondition2 = CreateConditionExpression(statuscode, ConditionOperator.Equal, new object[] { 1 });

    ConditionExpression[] conditions = new ConditionExpression[] { frCondition1, frCondition2 };

    FilterExpression filterExpression = new FilterExpression();

filterExpression.Conditions.AddRange(conditions);
filterExpression.FilterOperator = LogicalOperator.And;

QueryExpression queryExpression = new QueryExpression();
queryExpression.ColumnSet = GenerateColumnSet("columnNames");
queryExpression.EntityName = "entityName";
queryExpression.Criteria = filterExpression;
queryExpression.NoLock = true;

    frResponse =  (EntityCollection)service.RetrieveMultiple(queryExpression);

    if(frResponse != null && frResponse.Entities.Count > 0)
    {
        //Adding all FRs in the email
        foreach (Entity FilingRep in frResponse.Entities)
        {
            Guid frGuid = ((EntityReference)FilingRep["contactLookupfieldname"]).Id;

            Entity toParty = new Entity("activityparty");

            toParty[ActivityParty.ActivityPartId] = new EntityReference("contact", frGuid);

            ToOwner.Entities.Add(toParty);
        }
    }

    targetEntity[EmailEntityAttributeName.To] = toOwner;


来源:https://stackoverflow.com/questions/43180462/crm-sending-email-to-contacts-in-sub-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!