1

I am creating a visualforce page and a custom apex class for a page that allows users to convert opportunities and opportunity line items into service contracts and contract line items.

I've got all the field data into the places I want it, and am at the point where I upsert the the new service contract, but I can't think how to add the contract line items. I suppose I need the service contract's Id, but I would've assumed I can't do that in the code here because I have no way of knowing what Id I am querying for. Can someone clarify the way of doing this? Here is the code I have so far for reference, any other problems you can see let me know too as I'm still learning.

global class ContractCreationController
{
    final Opportunity opp;
    final List<OpportunityLineItem> lineItems;
    ServiceContract sc;
    List<ContractLineItem> contractItems;

    public ContractCreationController()
    {    
        opp = [SELECT   Id,
                        OwnerId, 
                        Name, 
                        AccountId,
                        Account.Name, 
                        Primary_Contact__c, 
                        Account.BillingStreet, 
                        Account.BillingCity, 
                        Account.BillingState,
                        Account.BillingPostalCode,
                        Account.BillingCountry
                        FROM Opportunity WHERE Id = :ApexPages.currentPage().getParameters().get('oid')];

        lineItems = [SELECT Id, 
                            Discount, 
                            Description, 
                            PricebookEntryId,
                            PricebookEntry.Name, 
                            Quantity, 
                            UnitPrice 
                            FROM OpportunityLineItem WHERE OpportunityId = :opp.Id];

        sc = new ServiceContract();
        sc.AccountId = opp.Id;
        sc.OwnerId = opp.OwnerId;
        sc.Name = opp.Account.Name + ' - ' + opp.Name;
        sc.ContactId = opp.Primary_Contact__c;
        sc.BillingStreet = opp.Account.BillingStreet;
        sc.BillingCity = opp.Account.BillingCity;
        sc.BillingState = opp.Account.BillingState;
        sc.BillingCountry = opp.Account.BillingCountry;
        sc.BillingPostalCode = opp.Account.BillingPostalCode;

        contractItems = new List<ContractLineItem>();
        for (OpportunityLineItem items : lineItems)
        {
            ContractLineItem cli = new ContractLineItem();
            cli.Discount = items.Discount;
            cli.Description = items.Description;
            cli.PricebookEntryId = items.PricebookEntryId;
            cli.Quantity = items.Quantity;
            cli.UnitPrice = items.UnitPrice;

            contractItems.add(cli);
        }
    }

    public PageReference save()
    {
        try
        {
            upsert(sc);
            //Not quite sure how to upsert all my contract line items :S

        }
        catch (System.DMLException e)
        {
            ApexPages.addMessages(e);
            return null;
        }
        return (new ApexPages.StandardController(sc)).view();
    }       
}
0

Once you've upserted your service contract, Apex puts the database ID in your object in memory. Once that is done you can use that to populate the value in your line items and insert them too.

Knowing this it may make more sense to first to the service contract upsert, and only then generate your line items.

try
        {
            upsert(sc);
            contractItems = new List<ContractLineItem>();
            for (OpportunityLineItem items : lineItems)
            {
                ContractLineItem cli = new ContractLineItem();
                cli.Discount = items.Discount;
                cli.Description = items.Description;
                cli.PricebookEntryId = items.PricebookEntryId;
                cli.Quantity = items.Quantity;
                cli.UnitPrice = items.UnitPrice;
                cli.ContractId = sc.id; //this wll now be filled

               contractItems.add(cli);
           }
}catch(....etc
  • So it's literally just a case of upsert(sc) and then I can reference sc.Id? I thought about that but it just seemed too easy :P – Adam Apr 7 '14 at 9:44
  • Yes that's how it works – Samuel De Rycke Apr 7 '14 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.