Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

CRM Plugin: Delete multiple records base on QueryExpression

Posted on by Microsoft Employee

Hi all,

I'm trying to create a plugin to delete some records base on a lookup field.

I'm now using QueryExpression and Filter to query those should be deleted, under the condition such as "lookup filed" Equals "certain GUID".  So, there will be multiple records retrieved as an EntityCollection.

I wanna know how can the program delete once by using the query result.

Btw, if there're not so many records, is it recommended to delete one by one with loop?  How can I take it into consideration.

Thanks,

Yvonne

*This post is locked for comments

  • Suggested answer
    sandeepstw Profile Picture
    sandeepstw 4,601 on at
    RE: CRM Plugin: Delete multiple records base on QueryExpression

    Hi,

    protected void Delete(Guid LookupID)

           {

               string fetchquery = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>

                                     <entity name='new_entityname>

                                       <attribute name='new_id' />                                    

                                        <filter type='and'>

                                         <condition attribute='new_lookupid' operator='eq' value='##ID##' />

                                       </filter>

                                     </entity>

                                   </fetch>";

               fetchquery = fetchquery.Replace("##ID##", LookupID + "");

               EntityCollection results = service.RetrieveMultiple(new FetchExpression(fetchquery));

               foreach (Entity e in results.Entities)

               {

                                   service.Delete(e);

               }

           }

  • C. Jensen Profile Picture
    C. Jensen 384 on at
    RE: CRM Plugin: Delete multiple records base on QueryExpression

    Hi Yvonne

    You could do something like this:

            private void DeleteRelatedRecords(string entityName, string attributeName, object attributeValue)
            {
                var query = new QueryExpression(entityName)
                {
                    PageInfo = new PagingInfo()
                    {
                        Count = 250,
                        PageNumber = 1
                    },
                    ColumnSet = new ColumnSet(false),
                };
                query.Criteria.AddCondition(attributeName, ConditionOperator.Equal, attributeValue);
                while (true)
                {
                    var RecordCollection = Service.RetrieveMultiple(query);
                    if (RecordCollection != null && RecordCollection.Entities.Count > 0)
                    {
                        foreach (var record in RecordCollection.Entities)
                        {
                            Service.Delete(record.LogicalName, record.Id);
                        }
                    }
                    if (RecordCollection.MoreRecords)
                    {
                        query.PageInfo.PageNumber++;
                        query.PageInfo.PagingCookie = RecordCollection.PagingCookie;
                    }
                    else
                    {
                        break;
                    }
                }
            }

    That will loop through all the related records, and delete them.

    Best regards

    Christian Jensen

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: CRM Plugin: Delete multiple records base on QueryExpression

    In addition you can also do BulkDeleteRequest using execute method.

    msdn.microsoft.com/.../hh670605.aspx

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: CRM Plugin: Delete multiple records base on QueryExpression

    Hello,

    If you have several records to delete you can use ExecuteMultipleRequest for this purpose - msdn.microsoft.com/.../jj863604.aspx

    Also if number of records to delete is not huge you can delete it in loop one by one.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans