You need to sign in to do that
Don't have an account?
Avinash dhanke 24
Test class For @invocable method
Hi All
I am trying to create apex test class for invocable method but i am not getting 75 plus code coverage. the following are my apex code
global with sharing class BoaOpportunityClone {
@InvocableMethod
global static List<Apex_Result> Opportunityreplicate(List<string> oppId){
Savepoint sp = Database.setSavepoint();
String opportunityId = '';
String oppRecType = '';
String accName = '';
List<Apex_Result> cloneStat = new List<Apex_Result>();
Apex_Result ar = new Apex_Result();
try{
String [] oppDetails = oppId[0].split('-');
System.debug('oppRecType' + oppDetails[1]);
System.debug('order' + oppDetails.size());
Opportunity oldOpp = [SELECT Id, RecordType.DeveloperName FROM Opportunity WHERE Id=: oppDetails[0]];
//purchase to purchase
if(oldOpp.RecordType.DeveloperName == 'Purchase_Order' && oppDetails[1] == 'Purchase Order'){ // change condition
opportunityId = oppDetails[0];
oppRecType = oppDetails[1];
accName = oppDetails[2];
System.debug('purchase');
Opportunity opp1 = [SELECT AccountId,Id,Amount,BOA_Order__c,Internal_Note__c,CurrencyIsoCode,city__c,CloseDate,Country__c,customer_order_number__c,default_Location__c,Description,ExpectedRevenue,Lead_Time__c,
Name,On_hold__c,Order_Date__c,Order_Placed_By__c,Order_Source__c,OwnerId,Parent_Order__c,Payment_type__c,postal_code__c,Probability,RecordTypeId,reference__c,
StageName,state__c,Street__c,subtotal__c,tax_total__c,TotalOpportunityQuantity,Type FROM Opportunity Where Id=:opportunityId ];
Account acc = [SELECT override_GST_rate__c,Remaining_Credit_Balance__c,Sea_Freight_Lead_Time__c,Air_Freight_Lead_Time__c FROM Account WHERE Id =: opp1.AccountId];
Decimal daysToAddToCloseDate = 0;
if(opp1.Lead_Time__c == 'Sea Freight Lead Time'){
daysToAddToCloseDate = acc.Sea_Freight_Lead_Time__c == null ? 0 : acc.Sea_Freight_Lead_Time__c;
}
else if(opp1.Lead_Time__c == 'Air Freight Lead Time'){
daysToAddToCloseDate = acc.Air_Freight_Lead_Time__c == null ? 0 : acc.Air_Freight_Lead_Time__c;
}
ObjectAutoNumber__c t1 = ObjectAutoNumber__c.getInstance('Opportunity');
t1.Purchase_Order_No__c += decimal.valueof('1');
update t1;
Opportunity Oppt1 = new Opportunity();
Oppt1.Name=string.valueof(math.round(t1.Purchase_Order_No__c));
Oppt1.OwnerId = opp1.OwnerId;
Oppt1.AccountId = opp1.AccountId;
Oppt1.Order_Date__c = date.today();
Date cd = opp1.CloseDate;
cd = cd.addDays(Integer.valueOf(daysToAddToCloseDate));
Oppt1.CloseDate = cd;
Oppt1.Lead_Time__c = opp1.Lead_Time__c;
Oppt1.default_Location__c = opp1.default_Location__c;
// Oppt1.tax_total__c = 0.00;
// Oppt1.Amount = opp1.Amount;
// Oppt1.subtotal__c = 0.00;
Oppt1.StageName = 'Prospecting';
Oppt1.Parent_Order__c = opp1.Id;
Oppt1.CurrencyIsoCode = opp1.CurrencyIsoCode;
Oppt1.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Purchase_Order').getRecordTypeId();
insert Oppt1;
Account accId = [select id from account where name=:accName];
if(accId.id !=null)
{
oppt1.AccountId = accId.id;
}
update oppt1; // Working
// changes
List<Id> prodId = new List<Id>();
Map<Id,Stock_Supplier__c> stockSuppl = new Map<Id,Stock_Supplier__c>();
List<OpportunityLineItem> oliList = [Select Product2Id,PricebookEntryId,UnitPrice,Quantity,Discount,OpportunityId,Description FROM OpportunityLineItem WHERE OpportunityId =: opp1.Id];
for(OpportunityLineItem oli : oliList){
prodId.add(oli.Product2Id);
}
System.debug('oliList ' + oliList);
// Stock supplier records based on supplier and stock selected
for(Stock_Supplier__c m : [SELECT Id, Discount_Percentage__c, Pack_Price__c, Supplier_Code__c, Stock_Item__c, Stock_Item__r.EOQ__c FROM Stock_Supplier__c WHERE Stock_Item__c IN : prodId AND Supplier_Name__c =: accId.Id]){
if(m != null){
stockSuppl.put(m.Stock_Item__c,m);
}
}
System.debug('stockSuppl' + stockSuppl);
List<OpportunityLineItem> newOli = new List<OpportunityLineItem>();
//if(stockSuppl.size() > 0){
for(OpportunityLineItem e : oliList){
OpportunityLineItem m = new OpportunityLineItem();
if(stockSuppl.get(e.product2Id) != null){
m.PricebookEntryId = e.PricebookEntryId;
m.UnitPrice = stockSuppl.get(e.product2Id).Pack_Price__c;
m.Quantity = Decimal.valueOf((stockSuppl.get(e.product2Id).Stock_Item__r.EOQ__c == null) ? String.valueOf(e.Quantity) : String.valueOf(stockSuppl.get(e.product2Id).Stock_Item__r.EOQ__c));
m.Discount = stockSuppl.get(e.product2Id).Discount_Percentage__c;
m.Description = e.Description == null ? '' : e.Description;
m.OpportunityId = Oppt1.Id;
}
else{
m.PricebookEntryId = e.PricebookEntryId;
m.UnitPrice = e.UnitPrice;
m.Quantity = e.Quantity;
m.Discount = e.Discount == null ? 0 : e.Discount;
m.Description = e.Description == null ? '' : e.Description;
m.OpportunityId = Oppt1.Id;
}
newOli.add(m);
}
//}
System.debug('newoliList ' + newOli);
insert newOli;
Account acct = [SELECT override_GST_rate__c,Remaining_Credit_Balance__c FROM Account WHERE Id =: oppt1.AccountId];
Decimal TotalVal = 0.0;
Decimal taxPercent = 0.0;
Decimal taxAmt = 0.0;
Decimal totalAmountWithTax = 0.0;
if(String.isNotBlank(acct.override_GST_rate__c) && acct.override_GST_rate__c != 'Auto Select Rate'){
String gstRate = acct.override_GST_rate__c;
String[] gr = gstRate.split('%');
taxPercent = Decimal.valueOf(gr[0]);
}
System.debug('m' + newOli);
for(OpportunityLineItem r : newOli){
Decimal prodTotal = (Double.valueOf(r.UnitPrice) * Double.valueOf(r.Quantity)) - ((Double.valueOf(r.UnitPrice) * Double.valueOf(r.quantity)) * (Double.valueOf(r.Discount)/100));
TotalVal += prodTotal;
taxAmt = TotalVal * (taxPercent/100);
totalAmountWithTax = TotalVal + taxAmt;
oppt1.subtotal__c = TotalVal;
oppt1.tax_total__c = taxAmt;
update oppt1;
}
ar.ReturnCode = '1';
ar.cloneOppId = Oppt1.Id;
cloneStat.add(ar);
return cloneStat;
}
catch(Exception e){
Database.rollback(sp);
ar.ReturnCode = '0';
ar.cloneOppId = opportunityId;
cloneStat.add(ar);
ErrorLogController.createErrorLog(e.getTypeName(),e.getMessage(),'BoaOpportunityClone.Opportunityreplicate',e.getLineNumber());
return cloneStat;
}
following are my test class
@istest
public class BoacloneTest {
testMethod static void BoacloneTest(){
Account acc = new Account();
acc.Name= 'test';
acc.Base_Price__c='retail';
acc.Region__c = 'Auckland';
acc.Phone = '78965412';
acc.Email__c= 'abc@gmail.com';
insert acc;
Opportunity opp =new Opportunity();
Opp.Name=string.valueof('1');
opp.AccountId = acc.Id;
opp.Order_Date__c= date.today();
opp.CloseDate = date.today();
opp.Lead_Time__c = 'Sea Freight Lead Time';
opp.StageName = 'Prospecting';
Opp.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Purchase_Order').getRecordTypeId();
insert opp;
Product2 newProduct = new Product2();
newProduct.Name = 'newProduct';
newProduct.ProductCode = 'VSC24B100';
newProduct.IsActive = true;
insert newProduct;
Id PricebookId = test.getStandardPricebookId();
PricebookEntry newPBE = new PricebookEntry();
newPBE.Pricebook2Id = PricebookId;
newPBE.Product2Id = newProduct.Id;
newPBE.UnitPrice = 600;
newPBE.IsActive = True;
insert newPBE;
OpportunityLineItem newOppLineItem = new OpportunityLineItem();
newOppLineItem.OpportunityId = opp.Id;
newOpplineItem.PricebookEntryId = newPBE.Id;
newOpplineItem.Quantity = 1;
newOpplineItem.TotalPrice = newPBE.UnitPrice;
insert newOppLineItem;
/*
Stock_Supplier__c stock = new Stock_Supplier__c();
// stock.Id= newProduct.Id;
stock.Supplier_Name__c = 'test';
stock.Supplier_Code__c = 'VS4B100';
insert stock;
*/
Pricing_Policy__c pp = new Pricing_Policy__c();
pp.Name = 'test2';
pp.Start_Date__c = date.today();
pp.Start_Date__c = date.newInstance(2018, 09, 10);
pp.IS_Active__c = True;
insert pp;
Pricing_Policy_Rules__c pricerule = new Pricing_Policy_Rules__c();
pricerule.Pricing_Policy__c = pp.Id;
pricerule.Start_Date__c = date.today();
pricerule.Stop_Date__c = date.newInstance(2019, 08, 11);
insert pricerule;
Pricing_Policy_with_Customer__c ppwcustom = new Pricing_Policy_with_Customer__c();
ppwcustom.Name = 'test3';
ppwcustom.Customers__c = acc.Id;
ppwcustom.Pricing_Policy__c = pp.Id;
insert ppwcustom;
}
@istest private static void opportunityReplicateTest(){
Account acc = new Account();
acc.Name= 'test';
acc.Base_Price__c='retail';
acc.Region__c = 'Auckland';
acc.Phone = '78965412';
acc.Email__c= 'abc@gmail.com';
insert acc;
Opportunity opp =new Opportunity();
opp.Name=string.valueof('1');
opp.AccountId = acc.Id;
opp.Order_Date__c= date.today();
opp.CloseDate = date.today();
opp.Lead_Time__c = 'Sea Freight Lead Time';
opp.StageName = 'Prospecting';
opp.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Purchase_Order').getRecordTypeId();
insert opp;
List<String> oppt= new List<String>();
List<Opportunity> oppList = [SELECT Account.Name,Id,RecordTypeId FROM Opportunity Where id=:opp.Id];
for(Opportunity o : oppList){
oppt.add(o.Id);
oppt.add(o.Account.Name);
oppt.add(o.RecordTypeId);
}
BoaOpportunityClone.Opportunityreplicate(oppt);
}
}
please give mi solution
I am trying to create apex test class for invocable method but i am not getting 75 plus code coverage. the following are my apex code
global with sharing class BoaOpportunityClone {
@InvocableMethod
global static List<Apex_Result> Opportunityreplicate(List<string> oppId){
Savepoint sp = Database.setSavepoint();
String opportunityId = '';
String oppRecType = '';
String accName = '';
List<Apex_Result> cloneStat = new List<Apex_Result>();
Apex_Result ar = new Apex_Result();
try{
String [] oppDetails = oppId[0].split('-');
System.debug('oppRecType' + oppDetails[1]);
System.debug('order' + oppDetails.size());
Opportunity oldOpp = [SELECT Id, RecordType.DeveloperName FROM Opportunity WHERE Id=: oppDetails[0]];
//purchase to purchase
if(oldOpp.RecordType.DeveloperName == 'Purchase_Order' && oppDetails[1] == 'Purchase Order'){ // change condition
opportunityId = oppDetails[0];
oppRecType = oppDetails[1];
accName = oppDetails[2];
System.debug('purchase');
Opportunity opp1 = [SELECT AccountId,Id,Amount,BOA_Order__c,Internal_Note__c,CurrencyIsoCode,city__c,CloseDate,Country__c,customer_order_number__c,default_Location__c,Description,ExpectedRevenue,Lead_Time__c,
Name,On_hold__c,Order_Date__c,Order_Placed_By__c,Order_Source__c,OwnerId,Parent_Order__c,Payment_type__c,postal_code__c,Probability,RecordTypeId,reference__c,
StageName,state__c,Street__c,subtotal__c,tax_total__c,TotalOpportunityQuantity,Type FROM Opportunity Where Id=:opportunityId ];
Account acc = [SELECT override_GST_rate__c,Remaining_Credit_Balance__c,Sea_Freight_Lead_Time__c,Air_Freight_Lead_Time__c FROM Account WHERE Id =: opp1.AccountId];
Decimal daysToAddToCloseDate = 0;
if(opp1.Lead_Time__c == 'Sea Freight Lead Time'){
daysToAddToCloseDate = acc.Sea_Freight_Lead_Time__c == null ? 0 : acc.Sea_Freight_Lead_Time__c;
}
else if(opp1.Lead_Time__c == 'Air Freight Lead Time'){
daysToAddToCloseDate = acc.Air_Freight_Lead_Time__c == null ? 0 : acc.Air_Freight_Lead_Time__c;
}
ObjectAutoNumber__c t1 = ObjectAutoNumber__c.getInstance('Opportunity');
t1.Purchase_Order_No__c += decimal.valueof('1');
update t1;
Opportunity Oppt1 = new Opportunity();
Oppt1.Name=string.valueof(math.round(t1.Purchase_Order_No__c));
Oppt1.OwnerId = opp1.OwnerId;
Oppt1.AccountId = opp1.AccountId;
Oppt1.Order_Date__c = date.today();
Date cd = opp1.CloseDate;
cd = cd.addDays(Integer.valueOf(daysToAddToCloseDate));
Oppt1.CloseDate = cd;
Oppt1.Lead_Time__c = opp1.Lead_Time__c;
Oppt1.default_Location__c = opp1.default_Location__c;
// Oppt1.tax_total__c = 0.00;
// Oppt1.Amount = opp1.Amount;
// Oppt1.subtotal__c = 0.00;
Oppt1.StageName = 'Prospecting';
Oppt1.Parent_Order__c = opp1.Id;
Oppt1.CurrencyIsoCode = opp1.CurrencyIsoCode;
Oppt1.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Purchase_Order').getRecordTypeId();
insert Oppt1;
Account accId = [select id from account where name=:accName];
if(accId.id !=null)
{
oppt1.AccountId = accId.id;
}
update oppt1; // Working
// changes
List<Id> prodId = new List<Id>();
Map<Id,Stock_Supplier__c> stockSuppl = new Map<Id,Stock_Supplier__c>();
List<OpportunityLineItem> oliList = [Select Product2Id,PricebookEntryId,UnitPrice,Quantity,Discount,OpportunityId,Description FROM OpportunityLineItem WHERE OpportunityId =: opp1.Id];
for(OpportunityLineItem oli : oliList){
prodId.add(oli.Product2Id);
}
System.debug('oliList ' + oliList);
// Stock supplier records based on supplier and stock selected
for(Stock_Supplier__c m : [SELECT Id, Discount_Percentage__c, Pack_Price__c, Supplier_Code__c, Stock_Item__c, Stock_Item__r.EOQ__c FROM Stock_Supplier__c WHERE Stock_Item__c IN : prodId AND Supplier_Name__c =: accId.Id]){
if(m != null){
stockSuppl.put(m.Stock_Item__c,m);
}
}
System.debug('stockSuppl' + stockSuppl);
List<OpportunityLineItem> newOli = new List<OpportunityLineItem>();
//if(stockSuppl.size() > 0){
for(OpportunityLineItem e : oliList){
OpportunityLineItem m = new OpportunityLineItem();
if(stockSuppl.get(e.product2Id) != null){
m.PricebookEntryId = e.PricebookEntryId;
m.UnitPrice = stockSuppl.get(e.product2Id).Pack_Price__c;
m.Quantity = Decimal.valueOf((stockSuppl.get(e.product2Id).Stock_Item__r.EOQ__c == null) ? String.valueOf(e.Quantity) : String.valueOf(stockSuppl.get(e.product2Id).Stock_Item__r.EOQ__c));
m.Discount = stockSuppl.get(e.product2Id).Discount_Percentage__c;
m.Description = e.Description == null ? '' : e.Description;
m.OpportunityId = Oppt1.Id;
}
else{
m.PricebookEntryId = e.PricebookEntryId;
m.UnitPrice = e.UnitPrice;
m.Quantity = e.Quantity;
m.Discount = e.Discount == null ? 0 : e.Discount;
m.Description = e.Description == null ? '' : e.Description;
m.OpportunityId = Oppt1.Id;
}
newOli.add(m);
}
//}
System.debug('newoliList ' + newOli);
insert newOli;
Account acct = [SELECT override_GST_rate__c,Remaining_Credit_Balance__c FROM Account WHERE Id =: oppt1.AccountId];
Decimal TotalVal = 0.0;
Decimal taxPercent = 0.0;
Decimal taxAmt = 0.0;
Decimal totalAmountWithTax = 0.0;
if(String.isNotBlank(acct.override_GST_rate__c) && acct.override_GST_rate__c != 'Auto Select Rate'){
String gstRate = acct.override_GST_rate__c;
String[] gr = gstRate.split('%');
taxPercent = Decimal.valueOf(gr[0]);
}
System.debug('m' + newOli);
for(OpportunityLineItem r : newOli){
Decimal prodTotal = (Double.valueOf(r.UnitPrice) * Double.valueOf(r.Quantity)) - ((Double.valueOf(r.UnitPrice) * Double.valueOf(r.quantity)) * (Double.valueOf(r.Discount)/100));
TotalVal += prodTotal;
taxAmt = TotalVal * (taxPercent/100);
totalAmountWithTax = TotalVal + taxAmt;
oppt1.subtotal__c = TotalVal;
oppt1.tax_total__c = taxAmt;
update oppt1;
}
ar.ReturnCode = '1';
ar.cloneOppId = Oppt1.Id;
cloneStat.add(ar);
return cloneStat;
}
catch(Exception e){
Database.rollback(sp);
ar.ReturnCode = '0';
ar.cloneOppId = opportunityId;
cloneStat.add(ar);
ErrorLogController.createErrorLog(e.getTypeName(),e.getMessage(),'BoaOpportunityClone.Opportunityreplicate',e.getLineNumber());
return cloneStat;
}
following are my test class
@istest
public class BoacloneTest {
testMethod static void BoacloneTest(){
Account acc = new Account();
acc.Name= 'test';
acc.Base_Price__c='retail';
acc.Region__c = 'Auckland';
acc.Phone = '78965412';
acc.Email__c= 'abc@gmail.com';
insert acc;
Opportunity opp =new Opportunity();
Opp.Name=string.valueof('1');
opp.AccountId = acc.Id;
opp.Order_Date__c= date.today();
opp.CloseDate = date.today();
opp.Lead_Time__c = 'Sea Freight Lead Time';
opp.StageName = 'Prospecting';
Opp.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Purchase_Order').getRecordTypeId();
insert opp;
Product2 newProduct = new Product2();
newProduct.Name = 'newProduct';
newProduct.ProductCode = 'VSC24B100';
newProduct.IsActive = true;
insert newProduct;
Id PricebookId = test.getStandardPricebookId();
PricebookEntry newPBE = new PricebookEntry();
newPBE.Pricebook2Id = PricebookId;
newPBE.Product2Id = newProduct.Id;
newPBE.UnitPrice = 600;
newPBE.IsActive = True;
insert newPBE;
OpportunityLineItem newOppLineItem = new OpportunityLineItem();
newOppLineItem.OpportunityId = opp.Id;
newOpplineItem.PricebookEntryId = newPBE.Id;
newOpplineItem.Quantity = 1;
newOpplineItem.TotalPrice = newPBE.UnitPrice;
insert newOppLineItem;
/*
Stock_Supplier__c stock = new Stock_Supplier__c();
// stock.Id= newProduct.Id;
stock.Supplier_Name__c = 'test';
stock.Supplier_Code__c = 'VS4B100';
insert stock;
*/
Pricing_Policy__c pp = new Pricing_Policy__c();
pp.Name = 'test2';
pp.Start_Date__c = date.today();
pp.Start_Date__c = date.newInstance(2018, 09, 10);
pp.IS_Active__c = True;
insert pp;
Pricing_Policy_Rules__c pricerule = new Pricing_Policy_Rules__c();
pricerule.Pricing_Policy__c = pp.Id;
pricerule.Start_Date__c = date.today();
pricerule.Stop_Date__c = date.newInstance(2019, 08, 11);
insert pricerule;
Pricing_Policy_with_Customer__c ppwcustom = new Pricing_Policy_with_Customer__c();
ppwcustom.Name = 'test3';
ppwcustom.Customers__c = acc.Id;
ppwcustom.Pricing_Policy__c = pp.Id;
insert ppwcustom;
}
@istest private static void opportunityReplicateTest(){
Account acc = new Account();
acc.Name= 'test';
acc.Base_Price__c='retail';
acc.Region__c = 'Auckland';
acc.Phone = '78965412';
acc.Email__c= 'abc@gmail.com';
insert acc;
Opportunity opp =new Opportunity();
opp.Name=string.valueof('1');
opp.AccountId = acc.Id;
opp.Order_Date__c= date.today();
opp.CloseDate = date.today();
opp.Lead_Time__c = 'Sea Freight Lead Time';
opp.StageName = 'Prospecting';
opp.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Purchase_Order').getRecordTypeId();
insert opp;
List<String> oppt= new List<String>();
List<Opportunity> oppList = [SELECT Account.Name,Id,RecordTypeId FROM Opportunity Where id=:opp.Id];
for(Opportunity o : oppList){
oppt.add(o.Id);
oppt.add(o.Account.Name);
oppt.add(o.RecordTypeId);
}
BoaOpportunityClone.Opportunityreplicate(oppt);
}
}
please give mi solution