
ISSUE: I am trying to populate an email from a QueryExpression that contains a foreach loop to cycle through multiple queries
BACKGROUND: I have a field in CRM called BCCTitle. In it I am putting what titles of CRM users I want the email to send to. In this example BCCTitle = AVP;VP I want to send the email to all Assistant Vice Presidents (AVP) and all Vice Presidents (VP).
CURRENTLY: With my current code, it is only sending to the VPs. I believe it is cycling through, but either my EntityCollection or the EntityReference AVP information is being over-written by the VP info. I am not sure how I can get users with the title AVP and users with the title VP to be on the "bcc" of an email.
Thank you in advance for your help.
string bccTitles = "AVP;VP";
string[] splitBCCTitles = bccTitles.Split(';');
foreach (string bccTitle in splitBCCTitles)
{
QueryExpression stringBCCquery = new QueryExpression()
{
Distinct = false,
EntityName = "systemuser",
ColumnSet = new ColumnSet("systemuserid"),
Criteria =
{
Filters =
{
new FilterExpression
{
Conditions =
{
new ConditionExpression("title", ConditionOperator.Equal, bccTitle)
},
},
new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression("isdisabled", ConditionOperator.Equal, "0")
},
}
}
}
};
DataCollection<Entity> bccEntityCollection = service.RetrieveMultiple(stringBCCquery).Entities;
EntityCollection bccUserParty = new EntityCollection();
bccUserParty.EntityName = "systemuser";
foreach (Entity bccUsers in bccEntityCollection)
{
Entity bccParty = new Entity("activityparty");
EntityReference bccUser = new EntityReference("systemuser", bccUsers.Id);
bccParty.Attributes.Add("partyid", bccUser);
bccUserParty.Entities.Add(bccParty);
}
//I have a class called OppWinInfo.cs where public object TitleBCCPArty {get; set;}
oppWinInfo.TitleBCCParty = bccUserParty;
}
email.Attributes.Add("bcc", oppWinInfo.TitleBCCParty);
*This post is locked for comments
I have the same question (0)Your issue is that although it is circulating on the AVP list as well, you are overwriting it with the values of the VP.
What you need to do is take the bccUserParty outside of the for loop, and add the individual items inside the for loop, before populating the oppWinInfo. This will add all the users of the VP and AVP to the bccUserParty collection. After this logic is run on all of your splits, you can then add the bccUserParty to oppWinInfo.TitleBCCParty
Hope this helps.