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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM Plugin: Delete multiple records base on QueryExpression

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 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.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator 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

  • C. Jensen Profile Picture
    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
    sandeepstw Profile Picture
    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);

               }

           }

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans