web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM PLUGIN: QueryExpression with foreach Loop

(0) ShareShare
ReportReport
Posted on by 180

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)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans