function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Account Trigger to populate parent account not working correctly

I have a trigger on the Account object that is supposed to populate the Parent Account during Lead Conversion.  The trigger works fine for that purpose.  When a lead is converted, the Parent Account populates.


However, when someone creates an account (without going through the lead process) and populates the parent account, the parent account goes away when that person hits save.  That person has to then update the account again with the parent account information to get the parent account to stay populated.


I thought my trigger would only affect accounts created from lead conversion.  I've copied the code below.  Any ideas how I can correct this issue?



trigger populateParentAccount on Account (before Insert){
  List<Lead> convertedLeads=[SELECT Id, ConvertedAccountID, Parent_Account__c
    FROM Lead WHERE IsConverted=True AND ConvertedAccountId IN];
      Map<ID,ID> acctParentMap=new Map<ID,ID>();
        for (lead l: convertedleads){
                for (account{
                    if (acctParentMap.containsKey(a.Id)){
                              }  }}



Thanks so much!!



 for (account{
                    if (acctParentMap.containsKey(a.Id) && a.ParentId  == null){
                              }  }}


Assign a ParentId to account only if its null else leave it as it is.


Trigger is written in wrong way, What i have given is a fix for your problem.

Abhilash Mishra 13Abhilash Mishra 13
I dont understand this line, as trigger is on before insert. 
List<Lead> convertedLeads=[SELECT Id, ConvertedAccountID, Parent_Account__c
 FROM Lead WHERE IsConverted=True AND ConvertedAccountId IN];
 How we are gonna filter leads on the basis of Account ids when account is not inserted yet. (as trigger is on before insert) ?

in my org its giving null values.
Matthew LohMatthew Loh
Hey @Abhilash if you're still looking to automate the Parent Account field when a lead is converted from account stage, I found a way to do this using Process Builder.

The downside is that you must have a restricted picklist in Lead Stage and all the options in that picklist must already exist as Accounts.

What you do is you then create a formula field on Lead Stage that returns values based on the the Lead Parent picklist I mentioned above.

The formula would be like
IF(TEXT(Picklist__c)="option 1", "Account ID 1", 

IF(TEXT(Picklist__c)="option 2", "Account ID 2", 



The Account ID is the individual string after

It's usually 15 or 18 characters. You can see it by going to those existing accounts and copying that character ID. This is why the picklist values must all be existing accounts or else the formula ID field would not know what to populate.

So once you've made this formula field, you create a hidden text field in Account Stage.

Then, create another picklist in Account Stage with the exact picklist values as the one at Lead Stage.

Then Map the fields as follows:

lead picklist --> account picklist
lead formula field --> account text field to carry account ID (make it a hidden field at Account stage)

Then go to process builder and create a process with the Criteria:

Account text field to carry account ID <> null

Action: Update records

Update Parent account to reference that same text field with the Account ID in it.

The reason you use the account ID <> null condition is so that it only applies for those coming from Lead stage.

So a user can still create an Account skipping the Lead conversion stage, but they'd have to manually fill in the Parent Account.

Hope that helps! :) 
Abhilash Mishra 13Abhilash Mishra 13
Hey Mathew,
Thanks. It was actually Barandi's requirement. I was just wondering about that Trigger code written with reference of question. because in my org it was never able to give converted Account Id value as it was on Before insert.