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)

Update parent record after child records are deleted

(0) ShareShare
ReportReport
Posted on by

Hiii guys, i have 2 entities: Product and order. There is product quantity field in product entity. Whenever order record created, the product quantity will be reduced based on the selected product.

Im able to reduced product quantity when order record is created. Now i want to increase product quantity value whenever order records are deleted. 

This what i have done so far:

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)
            {

                EntityReference entityreference = (EntityReference)context.InputParameters["Target"];
               

                if (context.MessageName == "Delete")
                {

                    Entity ent = service.Retrieve(entityreference.LogicalName, entityreference.Id, new ColumnSet(true));

                    var number = ent.GetAttributeValue<Decimal>("new_jumlahunit");
                    var sisaunit = number + 1;


                    ent.Attributes["new_jumlahunit"] = sisaunit;

                    service.Update(ent);

                }

            }

        }


i register this plugin on : pre-operation. But i got this error message: "The specified field does not exist in Microsoft Dynamics CRM"


*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at
    RE: Update parent record after child records are deleted

    Hi Saraswati,

     

    You can try to adding tracing logic{https://msdn.microsoft.com/en-us/library/gg328574.aspx} to your code to find out the issue... line 10 of your code seems incomplete... could you have a look and check if it is right?

  • Mahendar Pal Profile Picture
    45,095 on at
    RE: Update parent record after child records are deleted

    You need to write plugin on order pre delete and need to get order id, which will help you to get product id from order and then you can update that product.

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Update parent record after child records are deleted

    Hi,

    It looks like you have specified an attribute or entity name wrongly in the code. 

    If you look at the 11th line, there is a typo.

    1. EntityReference entityreference = (EntityReference)context.InputParameters["Target"];
    2. entityreference. = "new_productname";

    Please correct it and try. I think this will solve your problem.

  • Community Member Profile Picture
    on at
    RE: Update parent record after child records are deleted

    Thanks ratheesh, i forgot to delete it after i edit the code. but that not the problem here

  • Community Member Profile Picture
    on at
    RE: Update parent record after child records are deleted

    thanks guido, i got a clue from your second answer. That was i mean by a hole within my code. I think i never get parent entity to be updated.

    Would you mind to show me example code ?

  • Community Member Profile Picture
    on at
    RE: Update parent record after child records are deleted

    Thanks guido, it help me much

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
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans