0

I'm trying to create a relationship between Person Account and Contact. I'm doing it in Apex and using AccountContactRelation(ACR) object. I'm taking information from the Order and want to assign it to the ACR ContactId and AccountId, but not sure how it is done. Also, I want to be sure that the ACR does not already exist.

Contact firstSigner = configureContactAsSigner(eventData.salesContract.additionalContractSigners[0], ord.Business_Account__c);
ord.Contract_signer_1__r = new Contact(Unique_Email__c = firstSigner.Unique_Email__c);
dw.contacts.add(firstSigner);

List<AccountContactRelation> acrList = [SELECT AccountId, ContactId FROM AccountContactRelation];
                    
for (AccountContactRelation acrRel : acrList) {
    if (acrRel.AccountId != ord.Business_Account__c && acrRel.ContactId != ord.Contract_signer_1__r) {
        AccountContactRelation acr = new AccountContactRelation();
        acr.AccountId = ord.Business_Account__c;
        acr.ContactId = ord.Contract_signer_1__r.Unique_Email__c;
        dw.acr.add(acr);
    }
}
1

Your query

   List<AccountContactRelation> acrList = [SELECT AccountId, ContactId FROM AccountContactRelation];

is pulling ALL the records of the AccountContactRelation table, like literally all of them in the org. You need to filter this query based on the order business account and signer.

At the moment, you are getting every single record, and looping through all of them and checking if it matches the order details. This is very inefficient and as I said, you should filter the query using the same conditions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.