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
Cindy C 10Cindy C 10 

Help with trigger to prevent delete related list record based on checkbox on opportunity object

I am very new to writing triggers and would appreciate any and all help.  I have searched the forums to no avail, I am unable to find any information related to this particular scenario.  I do not want to go the roll up summary field route.

I have a custom object (related list) on which I would like to prevent the deletion of a record if checkbox=true on the related Opportunity record.
Best Answer chosen by Cindy C 10
v varaprasadv varaprasad
Hi Cindy,

Please check the samplee code below : 
Here account is a parent in the account I have created DeleteOpp__c checkbox field if it is true we cant delete its related opportunity.
 
trigger DeleteChildRecord on Opportunity (before Delete) {
    
    set<id> accIds = new set<id>();
    for(Opportunity op: trigger.old){
        if(op.accountid != null){
            accIds.add(op.accountid);
        }
        
    }
   
  list<account> lstAccs = [select id,DeleteOpp__c from account where id in : accIds and DeleteOpp__c = True];
    
    for(Opportunity op: trigger.old){
        if(lstAccs.size()>0){
            op.adderror('You cant delete opp because in account DeleteOpp is true');
        }
    }
    
    
}


Hope this helps.
Please let me know in case of any other assistance.
If the above information is informative please mark it as a best answer.


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

All Answers

v varaprasadv varaprasad
Hi Cindy,

Please check the samplee code below : 
Here account is a parent in the account I have created DeleteOpp__c checkbox field if it is true we cant delete its related opportunity.
 
trigger DeleteChildRecord on Opportunity (before Delete) {
    
    set<id> accIds = new set<id>();
    for(Opportunity op: trigger.old){
        if(op.accountid != null){
            accIds.add(op.accountid);
        }
        
    }
   
  list<account> lstAccs = [select id,DeleteOpp__c from account where id in : accIds and DeleteOpp__c = True];
    
    for(Opportunity op: trigger.old){
        if(lstAccs.size()>0){
            op.adderror('You cant delete opp because in account DeleteOpp is true');
        }
    }
    
    
}


Hope this helps.
Please let me know in case of any other assistance.
If the above information is informative please mark it as a best answer.


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
This was selected as the best answer
Cindy C 10Cindy C 10
Hi Varaprasad,
 
I have tried your code but am getting errors (Error: Compile Error: Variable does not exist: oppid at line 5 column 15). 

To clarify, Opportunity is the parent and my custom object (Origination Fee Allocation is the child). 
 
Trigger FeeSplitDeletePrevent on Origination_Fee_Allocation__c (before delete) {
    
    set<id> oppIds = new set<id>();
    for(Origination_Fee_Allocation__c fs: trigger.old){
        if(fs.oppid != null){
            Origination_Fee_Allocation__cIds.add(fs.oppid);
        }
        
    }
   
  list<opportunity> lstopps = [select id,Accounting_Review_Completed__c from opportunity where id in : oppIds and Accounting_Review_Completed__c = True];
    
    for(Origination_Fee_Allocation__c fs: trigger.old){
        if(lstOpps.size()>0){
            fs.adderror('You cant delete this fee split as it has already been reviewed by Accounting');
        }
    }
    }

 
v varaprasadv varaprasad
Hi Cindy,

In the place fs.oppid here you need to use fs.Your opportunity lookup field API name.
In your object Origination_Fee_Allocation__c  check opportunity API name add the same thing to your code.


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Cindy C 10Cindy C 10
Hi I'm sorry to be a pain, as I said I'm totally new to this :).  Can you explain this... In your object Origination_Fee_Allocation__c  check opportunity API name add the same thing to your code. 
v varaprasadv varaprasad
First Open object Origination_Fee_Allocation__c
In fields check opportunity API name (Ex : opportunity__C)
Same API Name you need to use in code. 

if(fs.oppid != null){
Origination_Fee_Allocation__cIds.add(fs.oppid);
 }

In place of  oppid  you need to use opportunity API name (;Ex : opportunity__C)


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Cindy C 10Cindy C 10
Okay yeah I did that but it still doesn't like it, so I'm missing something in setting the variable properly?..Error: Compile Error: Variable does not exist: Origination_Fee_Allocation__cIds at line 6 column 13
Trigger FeeSplitDeletePrevent on Origination_Fee_Allocation__c (before delete) {
   
    set<id> oppIds = new set<id>();
    for(Origination_Fee_Allocation__c fs: trigger.old){
        if(fs.Opportunity__c != null){
            Origination_Fee_Allocation__cIds.add(fs.Opportunity__c);      }
       
    }
  
  list<opportunity> lstopps = [select id,Accounting_Review_Completed__c from opportunity where id in : oppIds and Accounting_Review_Completed__c = True];
   
    for(Origination_Fee_Allocation__c fs: trigger.old){
        if(lstOpps.size()>0){
            fs.adderror('You cant delete this fee split as it has already been reviewed by Accounting');
        }
    }
    }


 
v varaprasadv varaprasad
 Origination_Fee_Allocation__cIds.add(fs.Opportunity__c);  change this to oppIds.add(fs.Opportunity__c); 
Cindy C 10Cindy C 10
I just tested and it works perfectly.  Thanks so much!!