You need to sign in to do that
Don't have an account?
Rakesh 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
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
You can take reference from this below code. 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
Hi Rakesh,
Can you please provide what error/data you are getting here ?
Regards,
Priya Ranjan
You can take reference from this below code. 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.
Thank you so much @Suraj Tripathi 47
Really appreciate it