I need help creating a method that queries the AccountContactRelation object and throws an error when an Opp is created without having an AccountContactRelation between its Primary Contact and Account. I have created the method below and works successfully, but the code is not bulkified, it is just taking one record at the time instead the whole trigger.new. I have created 2 maps relating Opp with Account and Opp with Contact, but the result are not accurate since I have to query ACRs that are related to the Account and Contact for each Opportunity.
This method is a 'Before Insert' event
public static void checkVestingScope(Opportunity opp){
List<AccountContactRelation> acr = New List<AccountContactRelation>([Select Id from AccountContactRelation where AccountId =:opp.AccountId and ContactId =:opp.Borrower_Contact__c]);
if(acr.size() == 0){
//check for unit test
if(Test.isRunningTest()){
String memo = 'Error runs here';
System.Debug(memo);
}else{
//throw vesting error
throw new applicationException('Error! Vesting Account (account name) and Primary Guarator (borrower contact) must have a relationship in order to save.');
}
}
}