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
qbrownqbrown 

populating a standard field with a custom field

Hi, i rewrote this post because I realized I wasnt being clear enough. Heres what im trying to do: when a lead is converted the parent account in lead needs to go over to the parent account in accounts. I found quickly that standard and custom fields cant be mapped together, but that a trigger could pull the data on conversion. I found a trigger here that I've attempted to use and I dont get any syntax errors with it, but when a lead is converted the parent account in accounts is left blank. Any help or suggestions would be appreciated, thanks!

heres the trigger:

 

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

 

Best Answer chosen by Admin (Salesforce Developers) 
SurekaSureka

Hi,

 

Try this:

 

trigger leadCheck on Lead (before update) 
{
    Map<Id, Id> convAccs = new Map<Id, Id>();
    List<Account> accsUpd = new List<Account>();
    for(Lead l:Trigger.new) 
    {
        if(l.isConverted)
        {
            convAccs.put(l.ConvertedAccountId, l.Parent_Account__c);
        }
        
    }
    List<Account> accs = [select ParentId,Id from Account where Id in: convAccs.keyset()];
    for(Account a: accs)
    {
        a.parentId = convAccs.get(a.Id);
        accsUpd.add(a);
    }
    update accsUpd;
}

 

 

Thanks

All Answers

SurekaSureka

Hi,

 

Try this:

 

trigger leadCheck on Lead (before update) 
{
    Map<Id, Id> convAccs = new Map<Id, Id>();
    List<Account> accsUpd = new List<Account>();
    for(Lead l:Trigger.new) 
    {
        if(l.isConverted)
        {
            convAccs.put(l.ConvertedAccountId, l.Parent_Account__c);
        }
        
    }
    List<Account> accs = [select ParentId,Id from Account where Id in: convAccs.keyset()];
    for(Account a: accs)
    {
        a.parentId = convAccs.get(a.Id);
        accsUpd.add(a);
    }
    update accsUpd;
}

 

 

Thanks

This was selected as the best answer
qbrownqbrown

thankyou! perfect