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];
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.