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

Update Contact Account following insert of associated object

Have a custom "Account Contact Role" (ACR) table here, joiner obj between Accounts and Contacts.  Any time an ACR is added, if the Contact used is missing an Account, I'd like to update that contact record with the Account.


I know I'm sufferening from "lazy queryness" here - after 100 different tries and fails, I'm looking for some guidance down the right path - please help !


Best way to "bulk friendly" grab the existing Contact Account values?


trigger ACR on Account_Contact_Role__c (Before Insert) {

    Map<Account_Contact_Role__c, Contact> ACRtoContactMap = new Map<Account_Contact_Role__c, Contact>();

    for (Account_Contact_Role__c ACR : {
        If (ACR.Contact__r.Account == null) { // <== yes, I know...will always be null 
                                              // (please help me to fix!)
            Contact C = new Contact(Id=ACR.Contact__c);
            ACRtoContactMap.put(ACR, C);
        update ACRtoContactMap.values();


Thanks for any asisstance / guidance !

Best Answer chosen by pjaenick.ax736

You'lll need to do an SOQL query for the related Contact record when inserting the Account_Contact_Role__c because the data for related objects isn't immediately available, and you need to check if Contact.AccountId == NULL. The trick will be keeping the query out of your loop(s) :)