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
BhagyaBhagya 

Avoid deletion of opportunities if its child Consultant status is active. (create consultant object with fields-name,DOB,active,oppid(lookup),accountid(lookup))

Hi can someone help me with this trigger?

*Avoid deletion of opportunities if its child Consultant status is active.(create consultant object with fields-name,DOB,active,oppid(lookup),accountid(lookup)).

I've tried this, but I'm getting error in different way.

trigger OppConsultantTrigger on Opportunity (before delete) {
     list<Consultant__c> cns = new list<Consultant__c>();
         cns = [select id,name,status__c from Consultant__c where Opportunityid__c IN : Trigger.OldMap.keySet()];
         for(opportunity o:trigger.old){
             for(Consultant__c c : cns)
             if(o.id== c.Opportunityid__c && c.status__c == 'Active')
             {
                 o.adderror('you annot delete this opportunity');
             }
         }
}
s
AnkaiahAnkaiah (Salesforce Developers) 
Hi Bhagya,

try with below code.
trigger OppConsultantTrigger on Opportunity (before delete) {
     list<Consultant__c> cns = new list<Consultant__c>();
         cns = [select id,name,status__c from Consultant__c where Opportunityid__c IN : Trigger.OldMap.keySet() AND status__c='Active'];
         for(opportunity o:trigger.old){
             if(cns.size()>0)
             {
                 o.adderror('you annot delete this opportunity');
             }
         }
}

If this helps, Please mark it as best answer.

Thanks!!