Opportunity Line Item

Opportunity Line Item | Salesforce Developer Guide

An opportunity is a pending agreement with a monetary value attached to it. It denotes that if the Opportunity is closed successfully, the $ amount of income will be generated for your firm.

The Products that are associated with an Opportunity are called OpportunityLineItems.

A business can sell a variety of different things. All of these items are referred to as Products. When a product (or a group of Products) is associated with an Opportunity, the records are referred to as "OpportunityLineItem" records.

Because each Product (or OpportunityLineItem) has a $ value connected with it, the $ value attached to an Opportunity originates from the Products associated with that particular opportunity.

Example:- On Opportunity to alert the user when an Opportunity is being closed with no Opportunity line items in it.

dont miss out iconDon't forget to check out: Concept of Virtual and Abstract Modifiers in Apex | Salesforce Developer Guide

Apex Class Code

public static void method1(List<Opportunity> listOfOpportunity){
    //use map for take opportunity id and opportunity record
    List<Opportunity> listOfOpportunity1 = new List<Opportunity>();
    map<Id,Opportunity> NewMapOfOpportunity = new map<Id,Opportunity>();
    // take listof opportunity in new opportunity1
    for(opportunity newOpportunity1 : listOfOpportunity){
        //check wheather opp stage name is closedwon/closed lost
        if(newOpportunity1.StageName =='Closed Won' || newOpportunity1.StageName =='Closed Lost'){
            //put the opp record id and opp record in map
            NewMapOfOpportunity.put(newOpportunity1.id,newOpportunity1);
            listOfOpportunity1.add(newOpportunity1);
        }           
    }
    //check the above map size any opp record having the opp stage as closed won and closed lost 
    if(NewMapOfOpportunity.size()>0){
        //fetch the record of opp line item in OpportunityLineItemlist
        for(List<OpportunityLineItem> listOfOpportunityLineItem : [Select id,name,OpportunityId from OpportunityLineItem where OpportunityId !='' AND OpportunityId In :listOfOpportunity1] ) {
            //check no of product 
            if(listOfOpportunityLineItem.size() < 1){
                //if there is no any product then display a error msg
                listOfOpportunity[0].addError('You can not closed the oppprtunites'); 
            }
        }            
    }        
}

Trigger Code

trigger updateAccountType on Opportunity (after insert,after update) {
    if(!AvoidRecursivecalltrigger.firstCall){
        AvoidRecursivecalltrigger.firstCall=true;
        if(Trigger.isAfter && (Trigger.isinsert || Trigger.isUpdate)){  
            OpportunityAmountUpdatetrigger.method1(Trigger.new);
        }
    }
}

dont miss out iconCheck out another amazing blog by Arpit here: Test Classes In Apex Salesforce | The Developer Guide

Test Class Code

@istest
public class OpportunityAmountUpdatetrigger_Test {
    @testSetup static void testmethod1(){
        List<Account> listOfAccount =DataFactory.createAccountMethod('Test Account',1,'Test Client');
        insert listOfAccount;
        List<Contact> listOfContact =DataFactory.createContactMethod(listOfAccount,1,false,'Test Client1');
        insert listOfContact;
        List<Opportunity> listOfOpportunity =DataFactory.createOpportunityMethod(listOfAccount,1);
        insert listOfOpportunity;
        List<Product2> listOfProduct2 =DataFactory.createProductMethod(1);
        insert listOfProduct2;
        Id pricebookId = Test.getStandardPricebookId();       
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = listOfProduct2[0].Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        List<Pricebook2> listOfPriceBook =DataFactory.createPriceBookMethod(listOfProduct2[0],1);
        insert listOfPriceBook;    
        List<PricebookEntry> listOfPriceBookEntry =DataFactory.createPricebookEntryMethod(listOfPriceBook,1,listOfProduct2);
        insert listOfPriceBookEntry;
        //List<Product2> listOfProduct2 =DataFactory.createProductMethod(listOfPriceBookEntry,1);
        //insert listOfProduct2;
        //  List<Pricebook2> listOfPriceBook =DataFactory.createPriceBookMethod(listOfProduct2,1);
        //insert listOfPriceBook;
        List<OpportunityLineItem> listOfOpportunityLineItem =DataFactory.CreateOpportunityLineItemMethod(listOfOpportunity,1,listOfPriceBookEntry[0]);
        //insert listOfOpportunityLineItem;
        Test.startTest();
        insert listOfOpportunityLineItem;
        Test.stopTest(); 
    }    
    @istest static void testmethod2(){
        List<Opportunity> listOfOpportunity1 =[Select id,name,StageName,CloseDate,Amount from Opportunity limit 1];        
        listOfOpportunity1[0].Amount =10000;
        //   listOfOpportunity1[0].Amount =1000;
        listOfOpportunity1[0].StageName='Closed Won';
        Test.startTest();
        update listOfOpportunity1 ;
        Test.stopTest();
        // listOfOpportunity1[0].Name ='Update test 1';
        // update listOfOpportunity1;
    }
    @istest static void testmethodForNegativeScnario(){
        List<Opportunity> listOfOpportunity2 =[Select id,name,StageName,CloseDate,Amount from Opportunity limit 1]; 
        listOfOpportunity2[0].Amount =10000;
        listOfOpportunity2[0].StageName='Need Analysis';        
        Test.startTest();
        update listOfOpportunity2 ;
        Test.stopTest();
    }
    @istest static void testMethodFromOpportunityLineItem(){
        List<OpportunityLineItem> listOfOpportunityLineItem1 =[Select id,name,OpportunityId from OpportunityLineItem limit 1];
        //
        //  listOfOpportunityLineItem1[0].ListPrice =1000;
        // listOfOpportunityLineItem1.Product2Id =listOfProduct2.id;
        Test.startTest();
        update listOfOpportunityLineItem1;
        Test.stopTest();
    }
}

Responses

Popular Salesforce Blogs