2

I'm using the Financial Services Cloud package in my org and have Person Accounts enabled. When a user is creating a contact-to-contact relationship, they're selecting the Household Member record type that can't be associated to an Account (which I believe is by design). Since our sharing model is private and we have Contacts set to "controlled by parent", users are not able to see each others contacts that are created since an account isn't being created.

My solution to this is to take that newly created contact and create a Person Account where they could then link the contact related to the person account to the contact for the relationship.

I have accomplished this but the user experience is awful since when they create the contact it gives an error that the record is deleted.

Is there any way around this?

Code for reference:

Trigger CreatePersonAccountForHouseholdContact on Contact (after insert) {
Id recordTypeHHMember = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Household Member').getRecordTypeId();
Id recordTypePersonAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getREcordTypeId();



List <Account> acctList = new List<Account>();
for(Contact c : trigger.new){
    if(c.RecordTypeId == recordTypeHHMember && c.Primary_Household__c != null && c.isPersonAccount == False){
        //Create PersonAccount
        Account acct = new Account();
        //acct.FinServ__PrimaryContact__c = c.Id;
        acct.FirstName = c.FirstName;
        acct.LastName = c.LastName;
        acct.Primary_Household__pc = c.Primary_Household__c;
        acct.Phone = c.Phone;
        acct.RecordTypeId = recordTypePersonAccount;
        
        //acct.OwnerId = c.Primary_Household__r.OwnerId;
        acctList.Add(acct);
        
        insert Acctlist;
        
    }
    

}
//get originating contact Id and delete
for (Contact createdContact : [Select Id from Contact WHERE Id IN:Trigger.new]){
    
    delete createdContact;
}
         

}

1 Answer 1

2

Your trigger needs to do this in two phases. First, in the Before Insert event, create any missing accounts. Next, in the After Insert phase, convert those records to Person Accounts. This works because we can link the account and contact immediately and perform conversion. Your code would look something like this:

trigger q330441 on Contact (before insert, after insert) {
    Id recordTypeHHMember = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Household Member').getRecordTypeId();
    Id recordTypePersonAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
    // This should be a temporary record type created specifically to assist the trigger in conversion.
    Id recordTypeBusinessAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Business Account').getRecordTypeId();
    switch on Trigger.operationType {
        when BEFORE_INSERT {
            Account[] newAccount = new Account[0];
            Contact[] updatedContacts = new Contact[0];
            for(Contact record: Trigger.new) {
                if(record.RecordTypeId == recordTypeHHMember && record.AccountId == null && record.Primary_Household__c != null) {
                    newAccount.add(new Account(Name=record.Name, recordTypeId=recordTypeBusinessAccount));
                    updatedContacts.add(record);
                }
            }
            insert newAccount;
            // Assign Account to Contact
            for(Contact record: updatedContacts) {
                record.AccountId = newAccount.remove(0).Id;
            }
        }
        when AFTER_INSERT {
            // Aggregate query update pattern
            Set<id> accountIds = new Set<Id>();
            for(Contact record: Trigger.new) {
                accountIds.add(record.AccountId);
            }
            Account[] accounts = [SELECT RecordTypeId FROM Account WHERE Id = :accountIds AND RecordTypeId = :recordTypeBusinessAccount];
            for(Account record: accounts) {
                record.RecordTypeId = recordTypePersonAccount;
            }            
            update accounts; // This changes the account to a Person Account            
        }
    }
}

Notice we do not delete the contact, but instead convert it on the fly. I can't test this right now, but do feel free to give this a try.

1
  • After a couple updates to match the information that is in the org this works flawlessly. Thank you so much!!
    – tDactyl
    Commented Jan 6, 2021 at 13:43

You must log in to answer this question.

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