Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

QueryExpression with for each loop to get the list of records from BPF entity

(0) ShareShare
ReportReport
Posted on by 1,290

Hello Everyone,

I am trying to create a queryexpression where I query a BPF entity which contains the lead entry records. The entity has the lookup field called "lead" which contains the name of the leads. As it's a lookup field, I am currently using below condition to fetch records and deleting using QueryExpression:

QueryExpression qe2 = new QueryExpression { EntityName = "new_bpf_lead", ColumnSet = new ColumnSet(true) };

qe2.Criteria.AddCondition("bpf_leadid", ConditionOperator.Equal, "********-****-****-****-************");

EntityCollection ec2 = service.RetrieveMultiple(qe2);

ec2.Entities.ToList().ForEach(y =>
{
Guid oldBPFId = y.Id;
service.Delete("new_bpf_lead", oldBPFId);
});

Now, is there any way to convert the above code to fetch actual value of that field instead of me comparing with the Guid?

I would like to change the query to something like "qe2.Criteria.AddCondition("bpf_leadid", ConditionOperator.BeginsWith, "A");" then for each loop will pick the value one by one and delete those records from the BPF entity.

I would like to achieve this using console app and not by going to advanced find and delete the records.

Please let me know for any confusion or questions.

Thank you for your help! :)

*This post is locked for comments

  • Verified answer
    Durgesh Profile Picture
    Durgesh 1,290 on at
    RE: QueryExpression with for each loop to get the list of records from BPF entity

    Aric,

    Appreciate your response and that's what exactly I tried like 2-3 hours back, but instead of name, i am using "CreatedOn" as my condition criteria and it seems to be working fine. Let me add my updated code below:

    QueryExpression qe2 = new QueryExpression("new_bpf_lead");

    LinkEntity createdonLink = qe2.AddLink("lead", "bpf_leadid", "leadid");

    createdonLink.LinkCriteria.AddCondition("createdon", ConditionOperator.OnOrBefore, "2017-01-01");

    EntityCollection ec2 = service.RetrieveMultiple(qe2);

    Console.WriteLine("Retrieved {0} record(s) to delete.", ec2.Entities.Count);

    try

    {

    ec2.Entities.ToList().ForEach(y =>

                   {

                       Guid oldBPFId = y.Id;

                       service.Delete("new_bpf_c95eef904a574633a1af4330310d10df", oldBPFId);

                   });

    }

    catch (Exception e)

               {

                   Console.WriteLine(e);

               }

    Thank you,

    Durgesh Vishen          

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 Moderator on at
    RE: QueryExpression with for each loop to get the list of records from BPF entity

    You should be able to do this with a join on the lead entity and creating a condition on the join either using Query Expression or Fetch Xml.

    Check the following link on how to create the join and the criteria on the Join expression.

    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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans