I have the following apex code to create an Opportunity from a Service Contract:

public class serviceContractOpportunities 

         public PageReference createOpportunities() 
            Map<Id, Opportunity> opps = new Map<Id, Opportunity>();
            for(ServiceContract sc: [SELECT ID, Legal_Entity__c, ServiceContract.Contact.Id, Name, AccountId, EndDate FROM ServiceContract WHERE EndDate = This_quarter AND Terminated__c = False])
                opps.put(sc.Id, new Opportunity(
                    Name = sc.Name,
                    AccountId = sc.AccountId,
                    Order_Type__c = 'AMS',
                    Contact__c = sc.Contact.Id,
                    StageName = 'Value Proposition',
                    CloseDate = sc.EndDate,
                    Related_Service_Contract_del__c  = sc.Id,
                    Legal_Entity__c = sc.Legal_Entity__c,
                    System_Type__c = 'Maintenance Renewal',
                    Type = 'Existing Business',
                    LeadSource = 'Maintenance'

            insert opps.values();

            OpportunityLineItem[] lines = new OpportunityLineItem[0];

            for(ContractLineItem cli: [select ServiceContract.Name, Pricebookentry.Name, ServiceContractId, Quantity, TotalPrice, PricebookEntryId FROM ContractLineItem WHERE ServiceContractId = :opps.keyset()]) 
                lines.add(new OpportunityLineItem(
                OpportunityId = opps.get(cli.ServiceContractId).Id,
                Quantity = cli.Quantity,
                TotalPrice = cli.TotalPrice,
                Description = 'Annual Maintenance ' +  cli.Pricebookentry.Name,
                PriceBookEntryId = cli.PriceBookEntryId
            insert lines;
            return null; 

Which works great. However, the process here is a bit different. Since an opportunity is invoiced through another system, it needs to look in a certain way in order to be uploaded as a CSV file into our financial system.

In the end, all products in the opportunity should be of product code 'ZY-SLA'. In addition, three extra lines need to be added to the opportunity, also with product code 'ZY-SLA', and different descriptions.

A complete opportunity should look as following:


Where the first three are the aforementioned descriptions, and the last four lines are the products from the ServiceContract.

How can I add the three dummy lines, and how can I set all the products to 'ZY-SLA'?

  • How you are receiving those three dummy line items? Feb 13, 2017 at 19:08
  • What do you mean exactly? The three dummy line items should be in all opportunities. Feb 13, 2017 at 19:09

1 Answer 1


You can use this code to insert dummy line items.

Pass the Opportunity Id and Set of product ids to this method.

Feel free to change according to your need, I have given an approach to insert OpportunityLineItems.

private void insertOpportunityLineItems(Id opportunityId, Set<Id> productIdSet)
    List<OpportunityLineItem> listInsertOpptyLineItem = new List<OpportunityLineItem>();
    Map<Id,PriceBookEntry> prodPriceBookMap = new Map<Id,PriceBookEntry>();

    Id pricebookId = null;

    //retrieve the Id from Standard price book.
    List<Pricebook2> lstPriceBook = [   SELECT Id 
                                        FROM Pricebook2 
                                        WHERE isStandard = true 
                                        LIMIT 1];
    //retrieve the standard price book id
        pricebookId = Test.getStandardPricebookId();
    else if (lstPriceBook.size()>0)

    //retrieve the price book entries of the products
    Map<Id,PriceBookEntry> priceBookEntryMap = new Map<Id,PriceBookEntry>([ SELECT  Id, 
                                                FROM    PriceBookEntry 
                                                        Product2Id in:productIdSet
                                                AND     Pricebook2Id =:pricebookId]);

    //create list of line items
    for(PriceBookEntry pbk:priceBookEntryMap.values())
        OpportunityLineItem opptyLineItem = new OpportunityLineItem();
        opptyLineItem.OpportunityId = opportunityId;
        opptyLineItem.PricebookEntryId = pbk.Id;                

    //finally perform insert operation
    insert listInsertOpptyLineItem;         
  • Thank you for your answer Santanu! I wasn't expecting so much code to be honest. What I perhaps failed to mention was that ZY-SLA is already a Product in our Pricebook? I will try to understand the code (quite new to APEX) and see if I can make it work, thanks! Feb 13, 2017 at 20:05
  • I have given a approach to create lineItems, feel free to change according to your need Feb 13, 2017 at 20:30
  • I have updated my code, to make it concise Feb 13, 2017 at 20:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .