You need to sign in to do that
Don't have an account?
Rakesh M 20
If the Opportunity Type is “New Customer” create new Account and update Account reference on Opportunity back
Hi Folks,
Plese help me to do this in " Batch Class " not trigger.
My code :
public class OpportunityToAccountBatchClass implements Database.Batchable<sObject> {
//Start Method
public Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator('select id,AccountId,Name,Type from Opportunity where Status__c != \'Closed\'');
}
//Execute Method
public void execute(Database.BatchableContext BC,List<Opportunity> oppData)
{
Map<Id, List<Opportunity>> mapOppIdToAccOrd = new Map<Id, List<Opportunity>>();
Set<Id> oppId = new Set<Id>();
List<Account> accList = new List<Account>();
List<Order> orderList = new List<Order>();
List<Opportunity> updateAccRefToOpp = new List<Opportunity>();
for(Opportunity opp : oppData)
{
if(String.isNotBlank(opp.AccountId) && opp.Type=='New Customer') {
if(!mapOppIdToAccOrd.containsKey(opp.AccountId)) {
mapOppIdToAccOrd.put(opp.AccountId, new List<Opportunity>());
}
mapOppIdToAccOrd.get(opp.AccountId).add(opp);
oppId.add(opp.AccountId);
}
if(oppId.size()>0)
{
accList.add(New Account(Name=opp.Name+'-'+'Account'));
}
}
if(accList.size()>0)
{
insert accList;
for(Account acc : accList)
{
for(Opportunity op : oppData)
{
if(!mapOppIdToAccOrd.containsKey(op.AccountId) && op.Type=='New Customer')
{
op.AccountId=acc.Id;
updateAccRefToOpp.add(op);
}
}
}
update updateAccRefToOpp;
}
}
//Finish Method
public void finish(Database.BatchableContext BC)
{
}
}
Thanks in Advance
Plese help me to do this in " Batch Class " not trigger.
My code :
public class OpportunityToAccountBatchClass implements Database.Batchable<sObject> {
//Start Method
public Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator('select id,AccountId,Name,Type from Opportunity where Status__c != \'Closed\'');
}
//Execute Method
public void execute(Database.BatchableContext BC,List<Opportunity> oppData)
{
Map<Id, List<Opportunity>> mapOppIdToAccOrd = new Map<Id, List<Opportunity>>();
Set<Id> oppId = new Set<Id>();
List<Account> accList = new List<Account>();
List<Order> orderList = new List<Order>();
List<Opportunity> updateAccRefToOpp = new List<Opportunity>();
for(Opportunity opp : oppData)
{
if(String.isNotBlank(opp.AccountId) && opp.Type=='New Customer') {
if(!mapOppIdToAccOrd.containsKey(opp.AccountId)) {
mapOppIdToAccOrd.put(opp.AccountId, new List<Opportunity>());
}
mapOppIdToAccOrd.get(opp.AccountId).add(opp);
oppId.add(opp.AccountId);
}
if(oppId.size()>0)
{
accList.add(New Account(Name=opp.Name+'-'+'Account'));
}
}
if(accList.size()>0)
{
insert accList;
for(Account acc : accList)
{
for(Opportunity op : oppData)
{
if(!mapOppIdToAccOrd.containsKey(op.AccountId) && op.Type=='New Customer')
{
op.AccountId=acc.Id;
updateAccRefToOpp.add(op);
}
}
}
update updateAccRefToOpp;
}
}
//Finish Method
public void finish(Database.BatchableContext BC)
{
}
}
Thanks in Advance
Kindly find the solution. If you find your Solution then mark it as the best answer.
Thanks and Regards
Suraj Tripathi.
All Answers
Kindly find the solution. If you find your Solution then mark it as the best answer.
Thanks and Regards
Suraj Tripathi.
Really appreciate it