+ Start a Discussion
Shaikh RayyanShaikh Rayyan 

i want to create a batch class that will append accounty name to related opportunity

Best Answer chosen by Shaikh Rayyan
CharuDuttCharuDutt
Hii Shaikh
Try Below Code
global class BatchUpdateaccnameonOpp implements Database.Batchable <sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext bc) {
        String query = 'Select Id ,Name,(Select Id,Name,AccountId from Opportunities) from Account';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc,List<Account> batch) {
       for(Account Acc : batch){
        for(Opportunity Opp : Acc.Opportunities){
        if(Acc.Id = opp.AccountId){
         opp.Name = Acc.Name +' _ '+ opp.Name;
         }
        }
       }
       
       if(!batch.IsEmpty()){
       update batch;
       }
    }
    
    global void finish(Database.BatchableContext bc) {
        //Do Nothing.
    }
}
Please Mark it As Best Asnwer If It Helps
Thank You!

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hio Shaikh,

try with below code.
global class BatchUpdateaccnameonOpp implements Database.Batchable <sObject> {

    List<opportunity> opplist = new List<opportunity>();
    
    global Database.QueryLocator start(Database.BatchableContext bc) {
        String query = 'select id,Name,Accountid from opportunity where accountid!=Null';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc,List<opportunity> batch) {
        system.debug('batch=='+batch);
        
        //Map<id,string> accmap = new map<id,string>();
        set<id> accids = new set<id>();
        for (opportunity a : batch) {
               accids.add(a.accountid);
            }
        
        Map<id,account> accmap = new map<id,account>([select id,Name from account where id=:accids]);
        for(opportunity opp:batch){
            if(accmap.containskey(opp.accountid)){               
                opp.name = accmap.get(opp.accountid).Name +'_'+opp.Name;
               opplist.add(opp);
                
            }
               
            }
        update opplist;
        
        system.debug('opplist=='+opplist);
    }
    
    global void finish(Database.BatchableContext bc) {
        //Do Nothing.
    }
}

Run the below command in dev console to run the batch apex.
BatchUpdateaccnameonOpp b = new BatchUpdateaccnameonOpp();
database.executeBatch(b);

If this helps, Please mark it as best answer.

Thanks!!
Harshit Kumar 27Harshit Kumar 27
Hi Shaikh Rayyan,

Please find below the solution to your problem using Handler Class -> 

//LeadTrigger
------------------
trigger LeadTrigger on Lead (before insert){
    if(Trigger.isBefore){
        if(Trigger.isInsert){
            LeadTriggerHandler.main(Trigger.new);
        }
    }
}

//LeadTriggerHandler
----------------------------
public class LeadTriggerHandler{
    public static void main(List<Lead> leadList){
        for(Lead lead : leadList){
            if(lead.Rating == 'Hot' && lead.Phone == NULL){
                lead.Phone.addError('Phone number is mandatory');
            }
        }
    }
}

If this solves your problem then please mark this as the best answer.

Thanks and Regards
Harshit Kumar
Harshit Kumar 27Harshit Kumar 27
Kindly ignore the above reply as it was meant to be posted somewhere else..
CharuDuttCharuDutt
Hii Shaikh
Try Below Code
global class BatchUpdateaccnameonOpp implements Database.Batchable <sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext bc) {
        String query = 'Select Id ,Name,(Select Id,Name,AccountId from Opportunities) from Account';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc,List<Account> batch) {
       for(Account Acc : batch){
        for(Opportunity Opp : Acc.Opportunities){
        if(Acc.Id = opp.AccountId){
         opp.Name = Acc.Name +' _ '+ opp.Name;
         }
        }
       }
       
       if(!batch.IsEmpty()){
       update batch;
       }
    }
    
    global void finish(Database.BatchableContext bc) {
        //Do Nothing.
    }
}
Please Mark it As Best Asnwer If It Helps
Thank You!
This was selected as the best answer