Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Delete Plugin child from parent entity

Posted on by Microsoft Employee

Hello,

I need delete Plugin code when parent record delete child record should also be delete

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Delete Plugin child from parent entity

    Could you share the code that you have been tried ?

  • Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Delete Plugin child from parent entity

    you want to delete all the contacts when account is deleted?

    or

    You want to delete only primary contact?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Delete Plugin child from parent entity

    thanks for suggestion but i need plugin code when account is delete related contact also  delete in account entity have contact lookup primary contact from there i get guid of contact but its not going to delete error showing given key is not exit.............................

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Delete Plugin child from parent entity

    This is example code how i update the parent records when child records is deleted. You can change the "Update" message with "Delete".

    public void Execute(IServiceProvider serviceprovider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceprovider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceprovider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = servicefactory.CreateOrganizationService(context.UserId);
    
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
                {
    		//If Delete, you should use "entityReference". Not Entity
    
                    EntityReference entityreference = (EntityReference)context.InputParameters["Target"];
    
                    if (context.MessageName == "Delete")
                    {
    
                        Entity ent = service.Retrieve(entityreference.LogicalName, entityreference.Id, new ColumnSet("new_productname"));
    		    
    		    // Product Name is a Look Up Field That Contain the name of product which i want to update the quantity.
    		    var productname = ent.GetAttributeValue<EntityReference>("new_productname");
    
                        if (productname != null)
                        {
    			// Getting quantity field from child entity
                            Entity product = service.Retrieve(productname.LogicalName, productname.Id, new ColumnSet("new_jumlahunit"));
                            
    			// Update the quantity on child entity
                            var number = product.GetAttributeValue<Decimal>("new_jumlahunit");
                            var sisaunit = number + 1;                        
                            product.Attributes["new_jumlahunit"] = sisaunit;
                            service.Update(product);
    
                        }
    
                      
    
                    }
    
                }


  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Delete Plugin child from parent entity

    You want to delete the child records on certain conditions or delete all the time?

    If you want to do it all the time, then you can set the relationship behavior as such.

    Please refer the following:

    https://community.dynamics.com/crm/b/crminogic/archive/2009/12/28/crm-relationship-behavior

    msdn.microsoft.com/.../gg309412.aspx

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