You need to sign in to do that
Don't have an account?
Khushboo Agarwal 4
how to write test class for the below class ?
Global class cls_createExecutionPlans
{
Webservice static String createExecutions(string RecordId,String Stage)
{
if(UtilityClass.Users_BusinessUnit == ''){
UtilityClass.Users_BusinessUnit = UtilityClass.getLoggedInUsersBusinessUnit();
}
else if(UtilityClass.Users_BusinessUnit == 'Display')
{
String ad_CampaignID;
Ad_Campaign__c objAdCampaign = new Ad_Campaign__c();
Map<ID,Decimal> ProductQuantityMap = new Map<ID,Decimal>();
Map<ID,Decimal> ProductAddedValueMap = new Map<ID,Decimal>();
Map<ID,Decimal> ProductRevenueMap = new Map<ID,Decimal>();
Decimal Quantity,AddedValue,Revenue;
Map<ID,Decimal> QuanityMap = new Map<ID,Decimal>();
Map<ID,Decimal> AddedValueMap = new Map<ID,Decimal>();
Map<ID,Decimal> RevenueMap = new Map<ID,Decimal>();
Set<ID> ProductIDs_Qnty = new Set<ID>();
Set<ID> ProductIDs_AddedValue = new Set<ID>();
Set<ID> ProductIDs_Revenue = new Set<ID>();
list<Execution_Plan__c> execDeleteList = new list<Execution_Plan__c>();
list<Execution_Plan__c> execInsertList = new list<Execution_Plan__c>();
cls_ExecutionPlans ObjExecutionPlans= new cls_ExecutionPlans();
system.debug('Opp Id==>'+RecordId);
system.debug('Opp Stage==>'+Stage);
Quote QuoteStatus = [Select id,Status from Quote where OpportunityID=:RecordId AND IsSyncing=true];
system.debug('Quote Status==>'+QuoteStatus.Status);
if(QuoteStatus!=Null && QuoteStatus.Status=='Approved' && Stage=='Closed Won')
{
Integer existingExecPlan = [Select count() from Ad_Campaign__c where Opportunity__c=:RecordId];
if(existingExecPlan==1)
{
objAdCampaign = [Select Id, Opportunity__c from Ad_Campaign__c where Opportunity__c=:RecordId limit 1];
if(objAdCampaign!=Null)
{
for(Execution_Plan__c e: [Select Id from Execution_Plan__c where Ad_Campaign__c=:objAdCampaign.id])
{
execDeleteList.add(e);
}
if(execDeleteList != null && execDeleteList.size() > 0)
delete execDeleteList;
delete objAdCampaign ;
}
}
if(RecordId!='')
{
try{
Ad_Campaign__c newAd = new Ad_Campaign__c();
newAd.Opportunity__c = RecordId;
newAd.Quote_Generated_Flag_Insert__c =true;
newAd.Quote_Generated_Flag_Delete__c = true;
insert newAd;
ad_CampaignID = newAd.id;
List<OpportunityLineItem> OppLIList = new List<OpportunityLineItem>();
List<Quote> QuoteList = new List<Quote>();
//Links the Quotes to the Ad Campaign created
for(Quote objQuote : [Select id,Ad_Campaign__c from Quote where OpportunityID=:RecordID])
{
Quote Quot = new Quote();
Quot=objQuote;
Quot.Ad_Campaign__c =newAd.Id;
QuoteList.add(Quot);
}
//Main section to create executions from the line items
for(OpportunityLineItem oli: [Select Id, OpportunityId, Quantity, added_value__c,UnitPrice, pricebookentry.product2.Id, TotalPrice,
Start_Date__c, End_Date__c, Description
from OpportunityLineItem
where OpportunityId=:RecordID])
{
//Links the Opportunity line items to the Ad Campaign creted
OpportunityLineItem objOppLI = new OpportunityLineItem();
objOppLI=oli;
objOppLI.Ad_Campaign__c =newAd.Id;
OppLIList.add(objOppLI);
if(oli.Start_Date__c != null && oli.End_Date__c != null)
{
date st = oli.Start_Date__c;
integer totalDays = oli.Start_Date__c.daysBetween(oli.End_Date__c) + 1;
double unitQuantity = (oli.Quantity * 1.0) / totalDays;
double unitSalePrice = (oli.TotalPrice * 1.0) / totalDays;
double unitAddedValue = 0.0;
if(oli.added_value__c != null)
unitAddedValue = (oli.added_value__c * 1.0) / totalDays;
boolean executeLoop = true;
while(executeLoop == true)
{
date en = st.addMonths(1).toStartOfMonth() - 1;
if(en >= oli.End_Date__c)
{
en = oli.End_Date__c;
executeLoop = false;
}
integer numberDays = st.daysBetween(en) + 1;
Execution_Plan__c e = new Execution_Plan__c();
e.Start_Date__c = st;
e.End_Date__c = en;
e.Ad_Campaign__c = newAd.Id;
e.Product_Name__c = oli.pricebookentry.product2.Id;
e.Line_Description__c = oli.Description;
e.Quantity__c = math.floor(unitQuantity * numberDays);
e.Added_Value__c = math.floor(unitAddedValue * numberDays);
Decimal temp_amnt = (unitSalePrice * numberDays);
//e.Amount__c = temp_amnt.setScale(2,roundingMode.DOWN);
e.OpportunityLineItemId__c = oli.Id;
e.Pd_Added_Value__c = (oli.added_value__c);
e.Pd_Quantity__c = math.floor(oli.Quantity);
system.debug('PD==>'+oli.TotalPrice);
system.debug('PD setscal==>'+(oli.TotalPrice).setScale(2));
system.debug('PD roundingMode==>'+(oli.TotalPrice).setScale(2,roundingMode.DOWN));
system.debug('PD roundUp==>'+(oli.TotalPrice).setScale(2,roundingMode.UP));
e.Pd_Total_Price__c = oli.TotalPrice;
system.debug('execToPrce==>'+e.Pd_Total_Price__c);
e.of_Impressions_Running_Tot__c = e.Pd_Quantity__c;
e.Added_Value_Running_Tot__c = e.Pd_Added_Value__c;
e.UnitSalesPrice__c = oli.UnitPrice;
Decimal tP = (oli.Quantity*oli.UnitPrice);
//e.Revenue_Running_Tot__c = (oli.TotalPrice).setScale(2);
system.debug('OLI=>'+oli.TotalPrice);
system.debug('ExPl=>'+ e.Revenue_Running_Tot__c);
if(ProductQuantityMap.containsKey(e.Product_Name__c))
{
Decimal value = ProductQuantityMap.get(e.Product_Name__c)+e.Quantity__c;
ProductQuantityMap.put(e.Product_Name__c,value);
}
else
{
ProductQuantityMap.put(e.Product_Name__c, e.Quantity__c);
}
if(ProductAddedValueMap.containsKey(e.Product_Name__c))
{
Decimal value = ProductAddedValueMap.get(e.Product_Name__c)+e.Added_Value__c;
ProductAddedValueMap.put(e.Product_Name__c,value);
}
else
{
ProductAddedValueMap.put(e.Product_Name__c, e.Added_Value__c);
}
system.debug('ExecutionPlan=>'+e);
execInsertList.add(e);
system.debug(execInsertList);
st = st.addMonths(1).toStartOfMonth();
}
}
}
//This method creates the execution plans
ObjExecutionPlans.createExecutionPlans(execInsertList,ProductQuantityMap,ProductAddedValueMap);
system.debug('Execution List==>'+execInsertList);
system.debug('Oppo LI Update==>'+OppLIList);
//Link Opp LI to AdCampaign
if(!OppLIList.isEmpty()) update OppLIList;
//Link Quote to AdCampaign
if(!QuoteList.isEmpty()) update QuoteList;
}
catch(Exception e)
{
system.debug('Exception Occured==>'+e);
}
}
return ad_CampaignId;
}
else if(QuoteStatus.Status!='Approved' || Stage!='Closed Won')
{
return 'false';
}
else
return null;
}
return null;
}
}
{
Webservice static String createExecutions(string RecordId,String Stage)
{
if(UtilityClass.Users_BusinessUnit == ''){
UtilityClass.Users_BusinessUnit = UtilityClass.getLoggedInUsersBusinessUnit();
}
else if(UtilityClass.Users_BusinessUnit == 'Display')
{
String ad_CampaignID;
Ad_Campaign__c objAdCampaign = new Ad_Campaign__c();
Map<ID,Decimal> ProductQuantityMap = new Map<ID,Decimal>();
Map<ID,Decimal> ProductAddedValueMap = new Map<ID,Decimal>();
Map<ID,Decimal> ProductRevenueMap = new Map<ID,Decimal>();
Decimal Quantity,AddedValue,Revenue;
Map<ID,Decimal> QuanityMap = new Map<ID,Decimal>();
Map<ID,Decimal> AddedValueMap = new Map<ID,Decimal>();
Map<ID,Decimal> RevenueMap = new Map<ID,Decimal>();
Set<ID> ProductIDs_Qnty = new Set<ID>();
Set<ID> ProductIDs_AddedValue = new Set<ID>();
Set<ID> ProductIDs_Revenue = new Set<ID>();
list<Execution_Plan__c> execDeleteList = new list<Execution_Plan__c>();
list<Execution_Plan__c> execInsertList = new list<Execution_Plan__c>();
cls_ExecutionPlans ObjExecutionPlans= new cls_ExecutionPlans();
system.debug('Opp Id==>'+RecordId);
system.debug('Opp Stage==>'+Stage);
Quote QuoteStatus = [Select id,Status from Quote where OpportunityID=:RecordId AND IsSyncing=true];
system.debug('Quote Status==>'+QuoteStatus.Status);
if(QuoteStatus!=Null && QuoteStatus.Status=='Approved' && Stage=='Closed Won')
{
Integer existingExecPlan = [Select count() from Ad_Campaign__c where Opportunity__c=:RecordId];
if(existingExecPlan==1)
{
objAdCampaign = [Select Id, Opportunity__c from Ad_Campaign__c where Opportunity__c=:RecordId limit 1];
if(objAdCampaign!=Null)
{
for(Execution_Plan__c e: [Select Id from Execution_Plan__c where Ad_Campaign__c=:objAdCampaign.id])
{
execDeleteList.add(e);
}
if(execDeleteList != null && execDeleteList.size() > 0)
delete execDeleteList;
delete objAdCampaign ;
}
}
if(RecordId!='')
{
try{
Ad_Campaign__c newAd = new Ad_Campaign__c();
newAd.Opportunity__c = RecordId;
newAd.Quote_Generated_Flag_Insert__c =true;
newAd.Quote_Generated_Flag_Delete__c = true;
insert newAd;
ad_CampaignID = newAd.id;
List<OpportunityLineItem> OppLIList = new List<OpportunityLineItem>();
List<Quote> QuoteList = new List<Quote>();
//Links the Quotes to the Ad Campaign created
for(Quote objQuote : [Select id,Ad_Campaign__c from Quote where OpportunityID=:RecordID])
{
Quote Quot = new Quote();
Quot=objQuote;
Quot.Ad_Campaign__c =newAd.Id;
QuoteList.add(Quot);
}
//Main section to create executions from the line items
for(OpportunityLineItem oli: [Select Id, OpportunityId, Quantity, added_value__c,UnitPrice, pricebookentry.product2.Id, TotalPrice,
Start_Date__c, End_Date__c, Description
from OpportunityLineItem
where OpportunityId=:RecordID])
{
//Links the Opportunity line items to the Ad Campaign creted
OpportunityLineItem objOppLI = new OpportunityLineItem();
objOppLI=oli;
objOppLI.Ad_Campaign__c =newAd.Id;
OppLIList.add(objOppLI);
if(oli.Start_Date__c != null && oli.End_Date__c != null)
{
date st = oli.Start_Date__c;
integer totalDays = oli.Start_Date__c.daysBetween(oli.End_Date__c) + 1;
double unitQuantity = (oli.Quantity * 1.0) / totalDays;
double unitSalePrice = (oli.TotalPrice * 1.0) / totalDays;
double unitAddedValue = 0.0;
if(oli.added_value__c != null)
unitAddedValue = (oli.added_value__c * 1.0) / totalDays;
boolean executeLoop = true;
while(executeLoop == true)
{
date en = st.addMonths(1).toStartOfMonth() - 1;
if(en >= oli.End_Date__c)
{
en = oli.End_Date__c;
executeLoop = false;
}
integer numberDays = st.daysBetween(en) + 1;
Execution_Plan__c e = new Execution_Plan__c();
e.Start_Date__c = st;
e.End_Date__c = en;
e.Ad_Campaign__c = newAd.Id;
e.Product_Name__c = oli.pricebookentry.product2.Id;
e.Line_Description__c = oli.Description;
e.Quantity__c = math.floor(unitQuantity * numberDays);
e.Added_Value__c = math.floor(unitAddedValue * numberDays);
Decimal temp_amnt = (unitSalePrice * numberDays);
//e.Amount__c = temp_amnt.setScale(2,roundingMode.DOWN);
e.OpportunityLineItemId__c = oli.Id;
e.Pd_Added_Value__c = (oli.added_value__c);
e.Pd_Quantity__c = math.floor(oli.Quantity);
system.debug('PD==>'+oli.TotalPrice);
system.debug('PD setscal==>'+(oli.TotalPrice).setScale(2));
system.debug('PD roundingMode==>'+(oli.TotalPrice).setScale(2,roundingMode.DOWN));
system.debug('PD roundUp==>'+(oli.TotalPrice).setScale(2,roundingMode.UP));
e.Pd_Total_Price__c = oli.TotalPrice;
system.debug('execToPrce==>'+e.Pd_Total_Price__c);
e.of_Impressions_Running_Tot__c = e.Pd_Quantity__c;
e.Added_Value_Running_Tot__c = e.Pd_Added_Value__c;
e.UnitSalesPrice__c = oli.UnitPrice;
Decimal tP = (oli.Quantity*oli.UnitPrice);
//e.Revenue_Running_Tot__c = (oli.TotalPrice).setScale(2);
system.debug('OLI=>'+oli.TotalPrice);
system.debug('ExPl=>'+ e.Revenue_Running_Tot__c);
if(ProductQuantityMap.containsKey(e.Product_Name__c))
{
Decimal value = ProductQuantityMap.get(e.Product_Name__c)+e.Quantity__c;
ProductQuantityMap.put(e.Product_Name__c,value);
}
else
{
ProductQuantityMap.put(e.Product_Name__c, e.Quantity__c);
}
if(ProductAddedValueMap.containsKey(e.Product_Name__c))
{
Decimal value = ProductAddedValueMap.get(e.Product_Name__c)+e.Added_Value__c;
ProductAddedValueMap.put(e.Product_Name__c,value);
}
else
{
ProductAddedValueMap.put(e.Product_Name__c, e.Added_Value__c);
}
system.debug('ExecutionPlan=>'+e);
execInsertList.add(e);
system.debug(execInsertList);
st = st.addMonths(1).toStartOfMonth();
}
}
}
//This method creates the execution plans
ObjExecutionPlans.createExecutionPlans(execInsertList,ProductQuantityMap,ProductAddedValueMap);
system.debug('Execution List==>'+execInsertList);
system.debug('Oppo LI Update==>'+OppLIList);
//Link Opp LI to AdCampaign
if(!OppLIList.isEmpty()) update OppLIList;
//Link Quote to AdCampaign
if(!QuoteList.isEmpty()) update QuoteList;
}
catch(Exception e)
{
system.debug('Exception Occured==>'+e);
}
}
return ad_CampaignId;
}
else if(QuoteStatus.Status!='Approved' || Stage!='Closed Won')
{
return 'false';
}
else
return null;
}
return null;
}
}
I would recommend you to go through following and start writing your test class.
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
Just to give you a start on this your class would be like this:
You might get troubles initally and can ask questions as they come.
Thanks
Shashikant