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
shruthikashruthika 

Urgent :Test class for opportunityLineItem trigger

Hi all,

 

Am very new to the apex code could anyone help me out in writing test class for opportunityLineItem trigger.please give me the solution as soon as possible.

 

Below is the code which i have written..

 

/*###########################################################################
        # The requirement is to inspect all Opportunity Line Items for each updated opportunity
      # If the field Product_Line_F__c  contained one of 4 product line values, it would concatenate an abbreviated code for that product line to Exlusion_Check__c. 
      # It is possible all 4 product lines can be on the same opportunity so Exlusion_Check__c could have all 4 values concatenated. 
      # Each product line code would only appear once no matter how many line items have  the same product line.

########################################################################### */

trigger Opportunity_Product_Line_Check on Opportunity (before update)
{
    String CN_var='';
    String OE_var='';
    String OB_var='';
    String OR_var='';
   
    // Querying for ids and Product Line values on OpportunityLineItem
    List<opportunitylineitem> opppro=[select Id,Product_Line_F__c from OpportunityLineitem where OpportunityId IN :Trigger.newMap.keySet()];
   
    //Iterating loop for every Opportunity
    for(Opportunity opp : Trigger.new)
    {
        //Iterating loop for every OpportunityLineItem
        for(OpportunityLineItem oli : opppro)
        {
            if(oli.Product_Line_F__c=='Connect')        // Comparing Product Line value
            {
                 CN_var = '[CN]';                                // Assigning abbreviated code to the variable
            }
            if(oli.Product_Line_F__c=='OpenEdge')
            {
                  OE_var = '[OE]';
            }
            if(oli.Product_Line_F__c=='ObjectStore')
            {
                  OB_var = '[OB]';
            }
            if(oli.Product_Line_F__c=='Orbix')
            {
                  OR_var = '[OR]';
            }
           
            // Concatenate an abbreviated code
            opp.Exclusion_Check__c = CN_var + OE_var + OB_var + OR_var;
           
            system.debug('*** Filter products checked; applicable code added to filter field ****');
        }
    }
}

 

 

Thanks in advance.....

shruthika...

 





rohitsfdcrohitsfdc

Hi,

one simple way to do that is hard coding the oppurtunity id.

take an oppurtunity, and add add products to it. Make sure those products have all those value in them i.e. Connect, openedge etc.

Take this oppurtunity id and write a test class as shown below:

 

@istest
private class testOpportunity_Product{

static testMethod void testOpportunity(){
Opportunity opp=[ select Id,exclusion_check__c,Description from Opportunity where id='YOUR_OPPURTUNITY_ID'];

opp.Description='123';
update opp;
}
}

 Another way is to create oppurtunity in the test class, create different oppurtunity line items for them and then update that newly created oppurtunity.

 

shruthikashruthika

Thanks for responding.

 

Actually am not understanding the actual flow. The requirement is whenever i add the product ex 'connect' in exclusion check it should populate as  [CN]. i want to know how the product, pricebook and be interlinked as these fields are in opportunity products and write a test class in such a way that whenever i add that product it should populate the abbreviated code.

 

could you help me out in explaning the actual flow between opportunity and opportunity product. Please send me solution as soon as possible.

 

thanks,

shruthika.

 

rohitsfdcrohitsfdc

hi,

all the products that you see in the product list are the records of object Product2.

Wen you select some product and enter the quantity in the oppurtunity, it creates a record in oppurtunityLineItem object or say oppurtunity product.

There is no relation between oppurtunity and products2. but there is a relation between oppurtunity and OppurtunitylineItem.

Hope this helps you.

 

shruthikashruthika

Thanks for making me to understand the relation between opportunity and opportunityLineItem..

if possible could you please help me in writing tests class in other ways apart  from the tets class which you posted before..

 

Thanks

shruthika.

rohitsfdcrohitsfdc

Another method is that instead of  that SOQL query, you create oppurtunity and its lineitems in the test class itself as shown in the following code:

@istest
private class testOpportunity_Product{

static testMethod void testOpportunity(){

PricebookEntry PBE=[select id from PricebookEntry limit 1 ];
Opportunity opp= new Opportunity();
opp.name='testOpp';
opp.closeDate=system.today()+20;
opp.stageName='Qualification';
try{
insert opp;
}
catch(Exception e){
System.debug(e);
}

List<opportunitylineitem> opppro = new opportunitylineitem[]{new opportunitylineitem(UnitPrice=10000.00, Quantity=10, Product_line_F__c='Connect', opportunityid=opp.id, pricebookEntryId=PBE.id),new opportunitylineitem(UnitPrice=10000.00, 	Quantity=10, Product_line_F__c='Orbix', opportunityid=opp.id, pricebookEntryId=PBE.id),new opportunitylineitem(UnitPrice=10000.00, 	Quantity=10, Product_line_F__c='ObjectStore', opportunityid=opp.id, pricebookEntryId=PBE.id),new opportunitylineitem(UnitPrice=10000.00, 	Quantity=10, Product_line_F__c='OpenEdge', pricebookEntryId=PBE.id, opportunityid=opp.id)};
insert opppro;

opp.Description='123';
update opp;
} }

 Please let us know if any issues occurs