Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Delete Record referential relationship between two entities

Posted on by Microsoft Employee

How to delete referential relationship between two entities in C# console application

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Delete Record referential relationship between two entities

    Hi,

    i want to delete record which have referential relationship. To delete parent and child record from CRM entity

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: Delete Record referential relationship between two entities

    Hi DMat,

    Try using below code.

    #region Execute Multiple with Results
    ExecuteTransactionRequest TransactionRequestDelete = null;

    // Create an ExecuteTransactionRequest object.
    TransactionRequestDelete = new ExecuteTransactionRequest()
    {
    // Create an empty organization request collection.
    Requests = new OrganizationRequestCollection(),
    //ReturnResponses = true
    };
    #endregion

    EntityReference connection = new EntityReference() { LogicalName = "LogicalName", Id = Guid };

    DeleteRequest deleteRequest = new DeleteRequest { Target = connection };

    TransactionRequestDelete.Requests.Add(deleteRequest);

    try

    {

    // Execute all the requests in the request collection using a single web method call.

    var responseForCreateRecords = (ExecuteTransactionResponse)service.Execute(TransactionRequestDelete);

    }

    catch (FaultException<OrganizationServiceFault> ex)

    {

    if (ex.Detail.ErrorDetails.Contains("MaxBatchSize"))

    {

    int maxBatchSize = Convert.ToInt32(ex.Detail.ErrorDetails["MaxBatchSize"]);

    if (maxBatchSize < TransactionRequestDelete.Requests.Count)

    {

    // Here you could reduce the size of your request collection and re-submit the ExecuteTransaction request.

    // For this sample, that only issues a few requests per batch, we will just print out some info. However,

    // this code will never be executed because the default max batch size is 1000.

    Console.WriteLine("The input request collection contains %0 requests, which exceeds the maximum allowed (%1)",

    TransactionRequestDelete.Requests.Count, maxBatchSize);

    }

    }

    Console.WriteLine("\t\t\t\t\t\t =================================");

    Console.WriteLine("\t\t\t\t\t\t\t Exception : " + "Create request failed for the project{0} and the reason being: {1}",

    ((ExecuteTransactionFault)(ex.Detail)).FaultedRequestIndex + 1, ex.Detail.Message);

    throw;

    }

    Thanks,

    Shahbaaz

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans