You need to sign in to do that
Don't have an account?
KB Kim
Need help for test code for OLI trigger
This code auto-creates OLI when a user create a new opportunity and selet a product name from the custome picklist (product__C).
I need to deploy this to our production instance, but I don't know how to write test code for this..
Please, help me.. I need to deploy this as soon as possible.
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(!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());
}
}
}
}
Test code that has only 58% coverage.
@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 need to deploy this to our production instance, but I don't know how to write test code for this..
Please, help me.. I need to deploy this as soon as possible.
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(!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());
}
}
}
}
Test code that has only 58% coverage.
@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;
}
}
KunalSharma
Please use the above and see if it works for you. Basically you will have to cover all the 'if' conditions present in your code.