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
Rakesh M 20Rakesh M 20 

Convert Opportunity To Account and Order Batch Class

Hi Folks,
Can Anyone tell me How can achive this Please..

I need to do this "Batch Class" Not in Trigger

My Requirement
--------------------------------------
-> Create new field on Opportunity 
Status (picklist) (Values: New, Closed)

-> This batch class will run on Every 6th day of month
Process only not closed Opportunity.

-> If the Opportunity Type is “New Customer” create new Account and create new Order for the Opportunity 
and create Order Items and update Account reference on Opportunity back
The  Order Items copied from Products that related to  Opportunity 

-> If the Opportunity Type is other than “New Customer” create new Order for the Opportunity and 
create Order Items.Order Items copied from Products that related to  Opportunity

My code
----------------------------------

public class OpportunityToAccountBatchClass implements Database.Batchable<sObject> {
    //Start Method
    public Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query ='select id,AccountId,Name,Type,Pricebook2Id,CloseDate,Status__c from Opportunity where Status__c != \'%Closed%\'';
        return Database.getQueryLocator(query);
    }  
    //Execute Method
    public void execute(Database.BatchableContext BC,List<Opportunity> oppList)
    {
        Map<Opportunity, Account> accountMap = new Map<Opportunity,Account>();
        Map<Opportunity, Order> orderMap = new Map<Opportunity,Order>();
        for(Opportunity opp: oppList){
            if( opp.Type=='New Customer')
            {
                System.debug('Opp Records --> '+oppList);
                //Account Creation
                Account acc = new Account();
                acc.Name = opp.Name;
                accountMap.put(opp,acc);
                //Order Creation
                Order ord = new Order();
                ord.Name=opp.Name;
                ord.OpportunityId=opp.Id;
                ord.Status= 'Draft';
                ord.AccountId=acc.Id;ord.EffectiveDate=opp.CloseDate;
                orderMap.put(opp,ord);
            }
        }
        if(!accountMap.values().isEmpty()){
            insert accountMap.values();
            System.debug('Acount records '+accountMap.values());
        }
        if(!orderMap.values().isEmpty()){
            insert orderMap.values();
        }
        for(Opportunity opp : oppList){
            opp.AccountId = accountMap.get(opp).Id;
        }
        if(!oppList.isEmpty()){
            update oppList;
        }   
    }
    
    //Finish Method
    public void finish(Database.BatchableContext BC)
    {
        
    }    
}


Thanks in Advance
Best Answer chosen by Rakesh M 20
Suraj Tripathi 47Suraj Tripathi 47
Hi Rakesh,

You can take reference from this below code.
public class OpportunityToAccountBatchClasss implements Database.Batchable<sObject> {
    //Start Method
    public Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query ='select id,AccountId,Name,Type,Pricebook2Id,CloseDate,Status__c from Opportunity where Status__c != \'%Closed%\'';
        return Database.getQueryLocator(query);
    }  
    //Execute Method
    public void execute(Database.BatchableContext BC,List<Opportunity> oppList)
    {
        list<opportunityLineItem> oppliList=[select name,opportunityId,PricebookEntryId,UnitPrice,Product2Id,Quantity from opportunityLineItem where opportunityId IN: oppList];
        map<id, List<opportunityLineItem>> oppidVsOppLineItemMap= new map<id, List<opportunityLineItem>>();
        for(opportunityLineItem oppli: oppliList){
            if(!oppidVsOppLineItemMap.containsKey(oppli.opportunityid)){
                List<opportunityLineItem> opporliList= new List<opportunityLineItem>();
                opporliList.add(oppli);
                oppidVsOppLineItemMap.put(oppli.opportunityid,opporliList);
            }
            else{
                List<opportunityLineItem> opporliList= oppidVsOppLineItemMap.get(oppli.opportunityid);
                opporliList.add(oppli);
                oppidVsOppLineItemMap.put(oppli.opportunityid,opporliList);
            }
        }
        Map<Opportunity, Account> accountMap = new Map<Opportunity,Account>();
        Map<Opportunity, Account> accountMap1 = new Map<Opportunity,Account>();
        Map<Opportunity, Order> orderMap = new Map<Opportunity,Order>();
        for(Opportunity opp: oppList){
            if( opp.Type=='New Customer')
            {
                Account acc = new Account();
                acc.Name = opp.Name;
                accountMap.put(opp,acc);
                
            }
            else{
                Account acc = new Account();
                acc.Name = opp.Name;
                accountMap1.put(opp,acc);
            }
        }
        if(!accountMap.values().isEmpty()){
            insert accountMap.values();
        }
        if(!accountMap1.values().isEmpty()){
            insert accountMap1.values();
        }
        for(Opportunity opp: oppList){
            if( opp.Type=='New Customer'){
                Order ord = new Order();
                ord.Name=opp.Name;
                ord.OpportunityId=opp.Id;
                ord.Pricebook2Id=opp.Pricebook2Id;
                ord.Status= 'Draft';
                ord.AccountId=accountMap.get(opp).id;
                ord.EffectiveDate=opp.CloseDate;
                orderMap.put(opp,ord);
            }
            else{
                Order ord = new Order();
                ord.Name=opp.Name;
                ord.OpportunityId=opp.Id;
                ord.Pricebook2Id=opp.Pricebook2Id;
                ord.Status= 'Draft';
                ord.AccountId=accountMap1.get(opp).id;
                ord.EffectiveDate=opp.CloseDate;
                orderMap.put(opp,ord);
            }
        }
        if(!orderMap.values().isEmpty()){
            insert orderMap.values();
        }
        list<orderItem> ordItemList= new List<orderItem>();
        for(order ord: orderMap.values()){
            if(oppidVsOppLineItemMap.containskey(ord.opportunityid)){
                for(opportunitylineitem oppli: oppidVsOppLineItemMap.get(ord.opportunityid)){
                    orderItem ordItem= new ordeRItem();
                    ordItem.OrderId=ord.Id;
                    ordItem.PricebookEntryId=oppli.PricebookEntryId;
                    ordItem.Product2Id=oppli.Product2Id;
                    ordItem.UnitPrice=oppli.UnitPrice;
                    ordItem.Quantity=oppli.Quantity;
                    ordItemList.add(ordItem);
                }
            }
        }
        if(ordItemList.size()>0){
            insert ordItemList;
        }
        list<opportunity> updateOppList= new list<opportunity>();
        for(Opportunity opp : oppList){
            if(accountMap.containskey(opp)){
                opp.AccountId = accountMap.get(opp).Id;
                updateOppList.add(opp);
            }
        }
        if(!updateOppList.isEmpty()){
            update updateOppList;
        }   
    }
    
    //Finish Method
    public void finish(Database.BatchableContext BC)
    {
        
    }    
}
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
 

All Answers

PriyaPriya (Salesforce Developers) 

Hi Rakesh,

Can you please provide what error/data you are getting here ?

Regards,

Priya Ranjan

Suraj Tripathi 47Suraj Tripathi 47
Hi Rakesh,

You can take reference from this below code.
public class OpportunityToAccountBatchClasss implements Database.Batchable<sObject> {
    //Start Method
    public Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query ='select id,AccountId,Name,Type,Pricebook2Id,CloseDate,Status__c from Opportunity where Status__c != \'%Closed%\'';
        return Database.getQueryLocator(query);
    }  
    //Execute Method
    public void execute(Database.BatchableContext BC,List<Opportunity> oppList)
    {
        list<opportunityLineItem> oppliList=[select name,opportunityId,PricebookEntryId,UnitPrice,Product2Id,Quantity from opportunityLineItem where opportunityId IN: oppList];
        map<id, List<opportunityLineItem>> oppidVsOppLineItemMap= new map<id, List<opportunityLineItem>>();
        for(opportunityLineItem oppli: oppliList){
            if(!oppidVsOppLineItemMap.containsKey(oppli.opportunityid)){
                List<opportunityLineItem> opporliList= new List<opportunityLineItem>();
                opporliList.add(oppli);
                oppidVsOppLineItemMap.put(oppli.opportunityid,opporliList);
            }
            else{
                List<opportunityLineItem> opporliList= oppidVsOppLineItemMap.get(oppli.opportunityid);
                opporliList.add(oppli);
                oppidVsOppLineItemMap.put(oppli.opportunityid,opporliList);
            }
        }
        Map<Opportunity, Account> accountMap = new Map<Opportunity,Account>();
        Map<Opportunity, Account> accountMap1 = new Map<Opportunity,Account>();
        Map<Opportunity, Order> orderMap = new Map<Opportunity,Order>();
        for(Opportunity opp: oppList){
            if( opp.Type=='New Customer')
            {
                Account acc = new Account();
                acc.Name = opp.Name;
                accountMap.put(opp,acc);
                
            }
            else{
                Account acc = new Account();
                acc.Name = opp.Name;
                accountMap1.put(opp,acc);
            }
        }
        if(!accountMap.values().isEmpty()){
            insert accountMap.values();
        }
        if(!accountMap1.values().isEmpty()){
            insert accountMap1.values();
        }
        for(Opportunity opp: oppList){
            if( opp.Type=='New Customer'){
                Order ord = new Order();
                ord.Name=opp.Name;
                ord.OpportunityId=opp.Id;
                ord.Pricebook2Id=opp.Pricebook2Id;
                ord.Status= 'Draft';
                ord.AccountId=accountMap.get(opp).id;
                ord.EffectiveDate=opp.CloseDate;
                orderMap.put(opp,ord);
            }
            else{
                Order ord = new Order();
                ord.Name=opp.Name;
                ord.OpportunityId=opp.Id;
                ord.Pricebook2Id=opp.Pricebook2Id;
                ord.Status= 'Draft';
                ord.AccountId=accountMap1.get(opp).id;
                ord.EffectiveDate=opp.CloseDate;
                orderMap.put(opp,ord);
            }
        }
        if(!orderMap.values().isEmpty()){
            insert orderMap.values();
        }
        list<orderItem> ordItemList= new List<orderItem>();
        for(order ord: orderMap.values()){
            if(oppidVsOppLineItemMap.containskey(ord.opportunityid)){
                for(opportunitylineitem oppli: oppidVsOppLineItemMap.get(ord.opportunityid)){
                    orderItem ordItem= new ordeRItem();
                    ordItem.OrderId=ord.Id;
                    ordItem.PricebookEntryId=oppli.PricebookEntryId;
                    ordItem.Product2Id=oppli.Product2Id;
                    ordItem.UnitPrice=oppli.UnitPrice;
                    ordItem.Quantity=oppli.Quantity;
                    ordItemList.add(ordItem);
                }
            }
        }
        if(ordItemList.size()>0){
            insert ordItemList;
        }
        list<opportunity> updateOppList= new list<opportunity>();
        for(Opportunity opp : oppList){
            if(accountMap.containskey(opp)){
                opp.AccountId = accountMap.get(opp).Id;
                updateOppList.add(opp);
            }
        }
        if(!updateOppList.isEmpty()){
            update updateOppList;
        }   
    }
    
    //Finish Method
    public void finish(Database.BatchableContext BC)
    {
        
    }    
}
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
 
This was selected as the best answer
Rakesh M 20Rakesh M 20
Its Working fine...
Thank you so much @Suraj Tripathi 47
Really appreciate it