You need to sign in to do that
Don't have an account?
KB Kim
Need help for test code for auto-creating OLI trigger
I have this code to auto-create OLI when create a new opportunity and selet a product name from the custome picklist (product__C).
I don't know how to write test code... plz help me..
trigger AutoCreateProduct on Opportunity (after insert) {
List<OpportunityLineItem> OpportunityLineItems = new List<OpportunityLineItem>();
for (Opportunity newOpportunity: Trigger.New) {
if(newOpportunity.product__C.equalsIgnoreCase('CRA')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001a8iVAAQ',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
} if (newOpportunity.product__C.equalsIgnoreCase('GALV')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001a8iQAAQ',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('2TFS')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBo7AAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('DRTP')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoRAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('SECONDARY TIN')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoqAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('DRBP')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoMAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('GLHR')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBobAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('1TFS')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBo2AAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('ETP')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoWAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('SECONDARY SHEET')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBolAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('SRTP')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBp0AAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('CRFH')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoCAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('CRHS')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoHAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('HRP')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBogAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('STS')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBp5AAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('SRBP')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBovAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
}
if(!OpportunityLineItems.isEmpty()){
Database.SaveResult[] srList = Database.insert(OpportunityLineItems, false);
// Iterate through each returned result
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
// Operation was successful, so get the ID of the record that was processed
System.debug('Successfully inserted Line Item. Oppty Prodcut ID: ' + sr.getId());
}
else {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Opportunity Product fields that affected this error: ' + err.getFields());
}
}
}
}
@IsTest (seealldata=true)
public class AutoCreateProduct{
public static testmethod void myunittest(){
//create a dummy account
Account a = new Account ();
a.Name = 'Test Asset Account';
insert a;
//create a dummy opportunity
Opportunity o = new Opportunity();
o.AccountId = a.Id;
o.Name = 'test Oppty';
o.StageName = 'opportunity';
o.CloseDate = system.today();
o.product__c = 'cra';
o.volume__c = 10;
insert o;
}
}
I don't know how to write test code... plz help me..
trigger AutoCreateProduct on Opportunity (after insert) {
List<OpportunityLineItem> OpportunityLineItems = new List<OpportunityLineItem>();
for (Opportunity newOpportunity: Trigger.New) {
if(newOpportunity.product__C.equalsIgnoreCase('CRA')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001a8iVAAQ',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
} if (newOpportunity.product__C.equalsIgnoreCase('GALV')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001a8iQAAQ',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('2TFS')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBo7AAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('DRTP')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoRAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('SECONDARY TIN')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoqAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('DRBP')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoMAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('GLHR')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBobAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('1TFS')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBo2AAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('ETP')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoWAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('SECONDARY SHEET')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBolAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('SRTP')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBp0AAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('CRFH')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoCAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('CRHS')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoHAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('HRP')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBogAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('STS')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBp5AAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
if (newOpportunity.product__C.equalsIgnoreCase('SRBP')){
OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBovAAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C));
}
}
if(!OpportunityLineItems.isEmpty()){
Database.SaveResult[] srList = Database.insert(OpportunityLineItems, false);
// Iterate through each returned result
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
// Operation was successful, so get the ID of the record that was processed
System.debug('Successfully inserted Line Item. Oppty Prodcut ID: ' + sr.getId());
}
else {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Opportunity Product fields that affected this error: ' + err.getFields());
}
}
}
}
@IsTest (seealldata=true)
public class AutoCreateProduct{
public static testmethod void myunittest(){
//create a dummy account
Account a = new Account ();
a.Name = 'Test Asset Account';
insert a;
//create a dummy opportunity
Opportunity o = new Opportunity();
o.AccountId = a.Id;
o.Name = 'test Oppty';
o.StageName = 'opportunity';
o.CloseDate = system.today();
o.product__c = 'cra';
o.volume__c = 10;
insert o;
}
}