TransWikia.com

Contact being automatically created with an account

Salesforce Asked by fooiey on November 15, 2021

This is my first time working on Salesforce, and I have inherited some code. Basically, when an Account of a certain record type is created, it looks like there is some code/process set up to always create a Contact associated with that account. For example here is a code snippet from an Apex class that is triggered when someone fills out a form.

Account newIndivAccount = new Account();
newIndivAccount.RecordTypeId = [select Id from RecordType where Name = 'Individual' AND SobjectType = 'Account'].Id;
newIndivAccount.Name = TC.FirstName__c + ' ' + TC.LastName__c;
newIndivAccount.Organization_Name__c = 'MyOrg';
insert newIndivAccount;
            
Contact NewContact = [SELECT Id from Contact WHERE AccountId = :newIndivAccount.Id][0];

So there has to be a contact automatically created for the last line to make sense.
The issue is that I think there is some asynchronous operation going on, as sometimes the last line throws a ListException error with list exception out of bounds. So I believe that the code waits for the newIndivAccount to be created but that triggers some other process to make the Contact which is not synchronous with the main code.

So my first question is, how can I find out where the code/process is that is creating the Contact? I have a suspicion it might be one of a few triggers that have relevant names, but the code is (hidden), and from my understanding, its a managed package so I cannot access it. It might also be some Salesforce feature I’m unaware of, so if you know please enlighten me.

My second question is how can I implement some kind of wait? Since I don’t know what process is getting triggered, I figured maybe I could just code in a short wait. I would like to maybe wait 2 seconds or so if the query returns 0 Contacts, and throw an exception once that duration passes and there is still no Contact. However, I did a search, and it doesn’t seem like waiting on threads is allowed in Salesforce. If you have any other suggestions on how to fix this problem, I’m all ears.

Thanks.

One Answer

First, asynchronous code will not start until the transaction that requested it ends; waiting a few seconds would not allow an asynchronous process to start (with the single exception of Platform Events, which could be possible, but unlikely).

Second, Salesforce doesn't have any default behavior that creates a contact when an account is created, so it is definitely custom code/process/etc.

Third, if you need to debug installed code, you'll have to contact the providers to see if they can help you, or refer to the documentation. Aside from that, if you have a Sandbox, you can create one, start uninstalling packages, and keep going until the behavior goes away. Or, you can create a Scratch Org, and install each package one at a time until the behavior appears. There's several ways to figure this out.

Unfortunately, without more information, it'll be hard to tell you exactly what's going on.

Answered by sfdcfox on November 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP