function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Oron MizrachiOron Mizrachi 

apex triger test

hi i have an apex trigger code that i dont know how to buuild a test for.

the code:


trigger PrimaryProduct on Opportunity (before update) {

list<opportunity> lt = trigger.new;

list<opportunityLineItem> opli = new list<opportunityLineItem>([select id ,product2.name, opportunityId,UnitPrice,Quantity from opportunitylineitem where opportunityId =: trigger.new[0].id]);

string productName='';
decimal salesprice='';
integer Quantity='';

for(opportunityLineItem o : opli){

 productName = o.product2.name;
  salesprice=o.UnitPrice;
   Quantity =o.Quantity;
}

for(Opportunity opp : trigger.new){

 opp.Opportunity_Prim_Product_Name__c = productName;
 opp.product_qunt__c=Quantity;
 opp.product_salesprice__c=salesprice;

}


}

can some one please help me building a test for it .

thanx
Best Answer chosen by Oron Mizrachi
ManojjenaManojjena
Hi Oron,
Try with below cod eit will help !
 
@isTest
public class TestPrimaryContactRole{
  public static testMethod void unitTest(){
          Product2 pr = new Product2();
            pr.Name='Lenovo X86';
            pr.isActive=true;
            insert pr;
            
            // Insert Pricebook
            PriceBook2 customPriceBook = new PriceBook2();
            customPriceBook.Name='Custom Pricebook';
            customPriceBook.IsActive=true;
            insert customPriceBook;
            
            // Query Standard and Custom Price Books
            Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
            Id stdPriceBookRec = Test.getStandardPricebookId();
            
            // Create Standard PriceBookEntry
            PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
            stdPriceBookEntry.Product2Id=pr.Id;
            stdPriceBookEntry.Pricebook2Id=stdPriceBookRec;
            stdPriceBookEntry.UnitPrice=2000;
            stdPriceBookEntry.IsActive=true;
            insert stdPriceBookEntry;
            
            // Create Custom PriceBookEntry
            PriceBookEntry customPriceBookEntry = new PriceBookEntry();
            customPriceBookEntry.Product2Id=pr.Id;
            customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
            customPriceBookEntry.UnitPrice=5000;
            customPriceBookEntry.IsActive=true;
            insert customPriceBookEntry;
            
            // Create Opportunity
            Opportunity opp = new Opportunity();
            opp.Name = 'Test';
            opp.CloseDate= System.Today();
            opp.StageName='Prospecting';
            insert opp;
            opp=[SELECT id,StageName FROM Opportunity WHERE Id=:opp.Id];
            System.assertEquals(opp.stageName,'Prospecting');
            
            // Add product and Pricebook to the particular opportunity using OpportunityLineItem 
            OpportunityLineItem oppLineItem = new OpportunityLineItem();
            oppLineItem.OpportunityId = opp.Id;
            oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
            oppLineItem.UnitPrice = 7000;
            oppLineItem.Quantity = 5;
            insert oppLineItem;
          Test.startTest();
            update opp;
        Test.stopTest();
  
  }
}

I will suggest one thing ,your trigger is bulkified so it will not work for bulk update .

Let m eknow if it helps .

All Answers

ManojjenaManojjena
Hi Oron,
Try with below cod eit will help !
 
@isTest
public class TestPrimaryContactRole{
  public static testMethod void unitTest(){
          Product2 pr = new Product2();
            pr.Name='Lenovo X86';
            pr.isActive=true;
            insert pr;
            
            // Insert Pricebook
            PriceBook2 customPriceBook = new PriceBook2();
            customPriceBook.Name='Custom Pricebook';
            customPriceBook.IsActive=true;
            insert customPriceBook;
            
            // Query Standard and Custom Price Books
            Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
            Id stdPriceBookRec = Test.getStandardPricebookId();
            
            // Create Standard PriceBookEntry
            PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
            stdPriceBookEntry.Product2Id=pr.Id;
            stdPriceBookEntry.Pricebook2Id=stdPriceBookRec;
            stdPriceBookEntry.UnitPrice=2000;
            stdPriceBookEntry.IsActive=true;
            insert stdPriceBookEntry;
            
            // Create Custom PriceBookEntry
            PriceBookEntry customPriceBookEntry = new PriceBookEntry();
            customPriceBookEntry.Product2Id=pr.Id;
            customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
            customPriceBookEntry.UnitPrice=5000;
            customPriceBookEntry.IsActive=true;
            insert customPriceBookEntry;
            
            // Create Opportunity
            Opportunity opp = new Opportunity();
            opp.Name = 'Test';
            opp.CloseDate= System.Today();
            opp.StageName='Prospecting';
            insert opp;
            opp=[SELECT id,StageName FROM Opportunity WHERE Id=:opp.Id];
            System.assertEquals(opp.stageName,'Prospecting');
            
            // Add product and Pricebook to the particular opportunity using OpportunityLineItem 
            OpportunityLineItem oppLineItem = new OpportunityLineItem();
            oppLineItem.OpportunityId = opp.Id;
            oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
            oppLineItem.UnitPrice = 7000;
            oppLineItem.Quantity = 5;
            insert oppLineItem;
          Test.startTest();
            update opp;
        Test.stopTest();
  
  }
}

I will suggest one thing ,your trigger is bulkified so it will not work for bulk update .

Let m eknow if it helps .
This was selected as the best answer
Oron MizrachiOron Mizrachi
Manoj thank you,

i have a problem with the code its self - error: Compile Error: Illegal assignment from String to Decimal at line 8 column 9

Can you help with that?

thank you


 
ManojjenaManojjena
Hi Oron,

You have initialize with null try to chage like
decimal salesprice=0;
integer Quantity=0;
 
ManojjenaManojjena
Hi Oron,try with below it will help ,
 
trigger PrimaryProduct on Opportunity (before update) {
   Map<Id,opportunitylineitem> oppMap=new Map<Id,opportunitylineitem>();
   for(Opportunity opp : [SELECT id,(SELECT id ,product2.name, opportunityId,UnitPrice,Quantity from opportunitylineitems)FROM Opportunity WHERE id IN:Trigger.new ]){
     if(opp.opportunitylineitems.size() > 0)
     oppMap.put(opp.Id,opp.opportunitylineitems[0]);
   }
   for(Opportunity opp: Trigger.new){
        opp.Opportunity_Prim_Product_Name__c = oppMap.get(opp.Id).product2.name;
        opp.product_qunt__c=oppMap.get(opp.Id).Quantity;
        opp.product_salesprice__c=oppMap.get(opp.Id).UnitPrice;
   }
}
Let me know if it helps .

Thanks 
Manoj
 
Oron MizrachiOron Mizrachi
he Manoj, the code worked fine on opportunities that already has a product related, but when im trying to edit a non product related opp (and not needing to use the trigger ) im getting an error msg - Error:Apex trigger PrimaryProduct caused an unexpected exception, contact your administrator: PrimaryProduct: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.PrimaryProduct: line 8, column 1.

can you help me code a line that will cancel the trigger if the oppertunity dont have a product  related?

thanx again.

oron