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.



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.

  • Okay, thanks. I figured there wouldn't be a simple answer. I guess I'll take your advice and start uninstalling things in the sandbox. Is there any way where I can see from what provider the custom code came from? Where would I begin to look for the documentation?
    – fooiey
    Jul 22 '20 at 1:09
  • @fooiey The apps should have product information in the setup screen under Installed Packages.
    – sfdcfox
    Jul 22 '20 at 1:53

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.