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
Marco Pollastri 1Marco Pollastri 1 

Trigger trying to get Account information

Hi, this is the trigger I have developed, I need to get accounts information, I need to reach a field called Type on the accont.


trigger Error_Opp on Opportunity (before insert) {
  for( Opportunity oppsupplier: trigger.new ){
       if(oppsupplier.Opp_Count__c == 1 && oppsupplier.type == 'Supplier')
        {
                 oppsupplier.addError('This is a Supplier Account, it can have only one Opportunity.');
       }
   }
}


Thanks 
Best Answer chosen by Marco Pollastri 1
Vijay NagellaVijay Nagella
Hi Marco,

Please find the Working code.
 
trigger Error_Opp  on Opportunity (before insert) {
    
	Set<Id> accid = new Set<Id>();
    
    for(Opportunity opp : Trigger.new){
        // Check if the Opportunity has an Account
        if(opp.AccountId != Null){
			accid.add(opp.AccountId);
        }
    }
    
    Map<Id,Account> accMap = new Map<Id,Account>([SELECT Id,Name,Type FROM Account WHERE Id IN :accid]);
    
    for(Opportunity oppsupplier : Trigger.new){
        // Check if the Opportunity Count is 1
        // and the Related Account Type is Supplier
        if(oppsupplier.Opp_Count__c == 1 && accMap.get(oppsupplier.AccountId).Type == 'Supplier'){
			oppsupplier.addError('This is a Supplier Account, it can have only one Opportunity.');
        }
    }
    

}

Please Mark it As Best Answer  If  the solution helps you.

Thanks,
Vijay Nagella,
Solution Architect,
Steadfast Consultancy Services

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Marco,
You can take reference from this below code.
trigger Error_Opp on Opportunity (before insert) {
  for( Opportunity oppsupplier: trigger.new ){
       if(oppsupplier.Opp_Count__c == 1 && oppsupplier.Account.Type == 'Supplier')
        {
                 oppsupplier.addError('This is a Supplier Account, it can have only one Opportunity.');
       }
   }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
Vijay NagellaVijay Nagella
Hi Marco,

Please find the Working code.
 
trigger Error_Opp  on Opportunity (before insert) {
    
	Set<Id> accid = new Set<Id>();
    
    for(Opportunity opp : Trigger.new){
        // Check if the Opportunity has an Account
        if(opp.AccountId != Null){
			accid.add(opp.AccountId);
        }
    }
    
    Map<Id,Account> accMap = new Map<Id,Account>([SELECT Id,Name,Type FROM Account WHERE Id IN :accid]);
    
    for(Opportunity oppsupplier : Trigger.new){
        // Check if the Opportunity Count is 1
        // and the Related Account Type is Supplier
        if(oppsupplier.Opp_Count__c == 1 && accMap.get(oppsupplier.AccountId).Type == 'Supplier'){
			oppsupplier.addError('This is a Supplier Account, it can have only one Opportunity.');
        }
    }
    

}

Please Mark it As Best Answer  If  the solution helps you.

Thanks,
Vijay Nagella,
Solution Architect,
Steadfast Consultancy Services
This was selected as the best answer
Marco Pollastri 1Marco Pollastri 1
Thanks @Vijay Nagella!