0

i wanted to update a field on contact once i associate a account.

once accountcontactrelation is created i am checking if that relation role is Manager and if yes then only i wanted to update a contact field.

My query is how cani achieve this in before trigger .beacuse if i write after trigger then my trigger is calling twice.Please suggest

and in before insert i am not getting any ACR and if on update I change the ACR then still not getting ACR query

map<id,AccountContactRelation>acrmap=new map<id,AccountContactRelation >();

    for(AccountContactRelation acr: [Select id,ContactId,Role__c from AccountContactRelation where (Role__c='Manager) and contactid in:newconMap.keyset() ]) {
        acrmap.put(acr.contactid,acr);
    }

    for(contact con:newconMap.values()) {
        if(acrmap.containsKey(con.id))
            con.isManager__c = true;   
    }     
1
  • Try using recursive control to avoid trigger fire more than once. Commented Jan 27, 2019 at 4:38

1 Answer 1

0

In the trigger you can check what stage you are in and then only run code when you want it to.

trigger TriggerName on SObjectType (before insert, before update, before delete, after insert, after update, after delete) {

 if (trigger.isBefore){
    if (trigger.isUpdate){
        //run your code here
        //for good practice and readability, call a class that has your code in it
    }
    else if (trigger.isInsert){
    }
    else if (trigger.isDelete){
    }
}
else if (trigger.isAfter){
    if (trigger.isUpdate){
    }
    else if (trigger.isInsert){
    }
    else if (trigger.isDelete){
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .