My goal :
-
When creating new contact and we assign it to specific account , the Account should have specific role
[![enter image description here][1]][1] -
when creating new Relationship , the new Account should be part of the existing Account hierarchy and have different role
Here's my trigger :
public with sharing class C3S_TRG100_AccountContactHandler extends C3S_TRG00_TriggerHandler {
private static final String CLASS_NAME = C3S_TRG100_AccountContactHandler.class.getName();
public C3S_TRG100_AccountContactHandler(){}
public override void beforeInsert(){
C3S_TRG100_AccountContactHandler.setAccountContactRelation(null,Trigger.New);
}
public override void beforeUpdate(){
C3S_TRG100_AccountContactHandler.setAccountContactRelation((Map<Id, AccountContactRelation>) Trigger.oldMap, Trigger.new);
}
public static void setAccountContactRelation(Map<Id, AccountContactRelation> oldMapAccCont,List<AccountContactRelation> accscons) {
for (AccountContactRelation accr: accscons) {
List<Id> accountIds = New List<Id>();
if(oldMapAccCont == null && accr == null){
accr.Roles == 'PGM';
accountIds.add(accr.Id);
}else {
AccountContactRelation oldAccCont = oldMapAccCont.get(accr.Id);
if(oldAccCont.Roles == 'PGM'){
accr.addError('Theres already an account with this Role');
}
}
}List<Account> AccList = [Select Parent.Parent.Parent.Parent.Parent.ParentId, Parent.Parent.Parent.Parent.ParentId, Parent.Parent.Parent.ParentId, Parent.Parent.ParentId, Parent.ParentId, ParentId From Account where Id In :accountIds ]
}
}
I'm stuck on how to do it , should it be in BeforeUpdate ? Should i use Map instead of List ?
[1]: https://i.stack.imgur.com/NYR8w.png
Best Answer
Solved, here's the trigger how it should look like :