function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
abcd1234abcd1234 

Inactivate Opportunity whenever account is inactivated

When a parent account is inactivated ( A custom field 'Inactive' , wil be set to true), then all the opportunites for that account & opportunities of the child accounts should be transferred to 'Opportunity Deleted' stage and owner of the opportunites ha s to be changed.

 

When Iam trying to do this. am getting errors like:

1. Apex trigger INACTIVATE_TRIGGER caused an unexpected exception, contact your administrator: INACTIVATE_TRIGGER: System.LimitException: Too many SOQL queries: 101

 

2. self-recursion :

Cannot recursively update itself

 

Can someone help me in this?

 

Thanks

sfdcfoxsfdcfox

I suspect there's a little "bulkification" that needs to be done. Probably something like this:

 

 

trigger trg_Inactivate_Children on Account(after update) {
  // Find all accounts that will trigger in this batch.
  Set<Id> accounts = new Set<Id>();
  for(Account a:Trigger.new)
    if(!a.active__c)
      accounts.add(a.id);

  // Find all children accounts, set them as inactive, and update them.
  // Recursion will happen here, so account hierarchy is limited to at most 16 levels.
  for(Account[] a:[select id from accounts where active__c = true and parentid in :accounts]) {
    for(account aa:a)
      aa.active__c = false;
    update a;
  }

  // Find all opportunities for accounts in this batch not owned by the selected user
  // or do not have the correct stage name. Update their values accordingly.
  for(Opportunity[] o:[select id from opportunity where accountid in :accounts and (stagename <> 'Opportunity Deleted' or ownerid <> '.......')]) {
    for(opportunity oo:o) {
       oo.ownerid = '.........';
       oo.stagename = 'Opportunity Deleted';
    }
    update o;
  }
}

 

 

abcd1234abcd1234

thanx a lot for the quick response