You need to sign in to do that
Don't have an account?
Del Santos
Test class keeps failing
Hi Guys!
Good day. I am creating a test method for a class i just created from a recycled apex code.
I have a requirement of updating the custom lookup field product__c in our opportunity object.
This field gets its value from the opportunity line item.
I've created an apex class, got the code somewhere from the internet, i read and tried a lot, fortunately made it working for our requirement. :)
The problem is i been trying to make a test method for it but keeps failing. I am a new admin and do not know much about coding.
Can someone help me to make a test class please. :) TIA guys!
Heres my apex class:
public class UpdateOLIProdMgr {
public static void updateOppProdMgr(OpportunityLineItem[] oppLineItems)
{
List<Opportunity> opps = new List<Opportunity>();
List<Id> idPriceBookEntry = new List<Id>();
List<Id> idProducts = new List<Id>();
List<String> strFamily = new List<String>();
for(OpportunityLineItem oppLineItem:oppLineItems)
{
idPriceBookEntry.add(oppLineItem.PricebookEntryId);
}
List<PricebookEntry> sObjPriceBookEntries = [Select Product2Id, Product2.Family, Product2.Name from PricebookEntry where Id in :idPriceBookEntry];
for(PricebookEntry objPriceBookEntry:sObjPriceBookEntries)
{
idProducts.add(objPriceBookEntry.Product2Id);
strFamily.add(objPriceBookEntry.Product2.Family);
}
if (sObjPriceBookEntries.size() > 0)
{
Integer intCounter = 0;
for(OpportunityLineItem oppLineItem:oppLineItems)
{
Opportunity opp = new Opportunity(id = oppLineItem.OpportunityId,Product__c = idProducts[intCounter]);
opps.add(opp);
intCounter++;
}
Database.update(opps);
}
}
public static void updateOLIMgr(OpportunityLineItem[] oppLineItems)
{
List<Id> idOpp = new List<Id>();
for(OpportunityLineItem oppLI: oppLineItems)
{
idOpp.add(oppLI.OpportunityId);
}
}
public static void eraseOppProd(OpportunityLineItem[] oppLineItems)
{
List<Opportunity> opps = new List<Opportunity>();
List<Id> idOpp = new List<Id>();
for(OpportunityLineItem oppLI: oppLineItems)
{
idOpp.add(oppLI.OpportunityId);
}
List<Opportunity> sObjOpp = [Select Id, Product__c from Opportunity where Id in :idOpp];
for(Opportunity opp:sObjOpp)
{
Opportunity oppty = new Opportunity(id=opp.id, Product__c = null, X50__c = null);
opps.add(oppty);
}
database.update(opps);
}
}
Good day. I am creating a test method for a class i just created from a recycled apex code.
I have a requirement of updating the custom lookup field product__c in our opportunity object.
This field gets its value from the opportunity line item.
I've created an apex class, got the code somewhere from the internet, i read and tried a lot, fortunately made it working for our requirement. :)
The problem is i been trying to make a test method for it but keeps failing. I am a new admin and do not know much about coding.
Can someone help me to make a test class please. :) TIA guys!
Heres my apex class:
public class UpdateOLIProdMgr {
public static void updateOppProdMgr(OpportunityLineItem[] oppLineItems)
{
List<Opportunity> opps = new List<Opportunity>();
List<Id> idPriceBookEntry = new List<Id>();
List<Id> idProducts = new List<Id>();
List<String> strFamily = new List<String>();
for(OpportunityLineItem oppLineItem:oppLineItems)
{
idPriceBookEntry.add(oppLineItem.PricebookEntryId);
}
List<PricebookEntry> sObjPriceBookEntries = [Select Product2Id, Product2.Family, Product2.Name from PricebookEntry where Id in :idPriceBookEntry];
for(PricebookEntry objPriceBookEntry:sObjPriceBookEntries)
{
idProducts.add(objPriceBookEntry.Product2Id);
strFamily.add(objPriceBookEntry.Product2.Family);
}
if (sObjPriceBookEntries.size() > 0)
{
Integer intCounter = 0;
for(OpportunityLineItem oppLineItem:oppLineItems)
{
Opportunity opp = new Opportunity(id = oppLineItem.OpportunityId,Product__c = idProducts[intCounter]);
opps.add(opp);
intCounter++;
}
Database.update(opps);
}
}
public static void updateOLIMgr(OpportunityLineItem[] oppLineItems)
{
List<Id> idOpp = new List<Id>();
for(OpportunityLineItem oppLI: oppLineItems)
{
idOpp.add(oppLI.OpportunityId);
}
}
public static void eraseOppProd(OpportunityLineItem[] oppLineItems)
{
List<Opportunity> opps = new List<Opportunity>();
List<Id> idOpp = new List<Id>();
for(OpportunityLineItem oppLI: oppLineItems)
{
idOpp.add(oppLI.OpportunityId);
}
List<Opportunity> sObjOpp = [Select Id, Product__c from Opportunity where Id in :idOpp];
for(Opportunity opp:sObjOpp)
{
Opportunity oppty = new Opportunity(id=opp.id, Product__c = null, X50__c = null);
opps.add(oppty);
}
database.update(opps);
}
}
All Answers
This is great!
Thanks a lot! I owe you one. I shall be reviewing this code so i can make one..ty!