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
BsinBsin 

populate hierarchy field parent account on account object

Hi,

I have a requirement to populate "parent account" on Account based on a custom field value. I tried to achieve it through a trigger.
In bulk upsert,if the child account is inserted before the parent account,parent account on child is not getting assigned. 

Accounts data will be upserted to salesforce through a scheduled pentaho job from another application on dailiy basis.

Could some one please advise me how to resolve the issue?

Thanks in advance.

BR,
BB
Best Answer chosen by Bsin
Pradeep SinghPradeep Singh
You can refer below code:-
 
public class batch_ParentAccountPopulate implements database.Batchable<sObject>{    
    Map<string,ID> accMap = new Map<string,ID>();
    string query = '';    
    public batch_ParentAccountPopulate (){
        for(Account acc : [Select id,name,ParentID,Color__c from Account]){
            accMap.put(acc.Color__c,acc.id);
        }        
    }
    
    public Database.QueryLocator start(Database.BatchableContext BCA)
    {
        query = 'SELECT Id,Name,Parent_Color__c FROM Account WHERE LastModifiedDate = TODAY';
        return Database.getQueryLocator(query);
        
    }   
    public void execute(Database.BatchableContext BCB, List<Account> scope)
    {
         for(Account a : scope)
         {
             a.parentId = accMap.get(a.Parent_Color__c);          
         }
         
         update scope;
    }   
    public void finish(Database.BatchableContext BCC)
    {
    
    }
}

------------------------------------------------------------------------
global class AccountupdateSchedule implements Schedulable{ 
 
 global void execute(SchedulableContext sc) {
       Database.executeBatch(new batch_ParentAccountPopulate());
 }
}

Here child records contains the parent color code so that it can be liked to parent records.

If it solves your problem, please mark it as solved or best answer.

All Answers

Gaurish Gopal GoelGaurish Gopal Goel
Please post the code of trigger. Thanks.
Pradeep SinghPradeep Singh
Hi,
You will never able to poplate parent account field if there is no parent available. So , before inserting account you have to insert parent account if you want to do this via trigger.
 
You can write a schedule batch which is scheduled for daily after the data is upserted into the org.Assuming your scheduled pentaho job from another application is scheduled for a fix time.
BsinBsin
Hi pradeep,

Yes Our pentaho job is scheduled at fix time. can you please explian me how the scheduled batch helps here.

Thanks in advance.

BR,
Bhargavi
Pradeep SinghPradeep Singh
How many records are there in Account Object..??
Pradeep SinghPradeep Singh
You can refer below code:-
 
public class batch_ParentAccountPopulate implements database.Batchable<sObject>{    
    Map<string,ID> accMap = new Map<string,ID>();
    string query = '';    
    public batch_ParentAccountPopulate (){
        for(Account acc : [Select id,name,ParentID,Color__c from Account]){
            accMap.put(acc.Color__c,acc.id);
        }        
    }
    
    public Database.QueryLocator start(Database.BatchableContext BCA)
    {
        query = 'SELECT Id,Name,Parent_Color__c FROM Account WHERE LastModifiedDate = TODAY';
        return Database.getQueryLocator(query);
        
    }   
    public void execute(Database.BatchableContext BCB, List<Account> scope)
    {
         for(Account a : scope)
         {
             a.parentId = accMap.get(a.Parent_Color__c);          
         }
         
         update scope;
    }   
    public void finish(Database.BatchableContext BCC)
    {
    
    }
}

------------------------------------------------------------------------
global class AccountupdateSchedule implements Schedulable{ 
 
 global void execute(SchedulableContext sc) {
       Database.executeBatch(new batch_ParentAccountPopulate());
 }
}

Here child records contains the parent color code so that it can be liked to parent records.

If it solves your problem, please mark it as solved or best answer.
This was selected as the best answer
BsinBsin
Thank you very much pradeep.

BR,
BB