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
emillar_altiaemillar_altia 

Syncing data from Quote Line Items to Opportunity Line Items - Stopped working...

I had a lot of help writing a trigger a long time ago that took the Start Date, Duration and End Date data from the Opportunity Line Items into the Quote Line Items, and Vice Versa. 

I thought this was working but after some testing it appears to no longer be doing it's job. The person who helped me write the trigger is no longer with the company and my job role has changed so my memory of writing one isn't very good. 

This is the Trigger: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
trigger CopyOpportunityProductFields on QuoteLineItem (after insert) {

    // Query quote line items
    List<QuoteLineItem> qlis = [SELECT Id, PricebookEntry.Product2Id, Quote.OpportunityId FROM QuoteLineItem WHERE Id IN :Trigger.new];

    // Query opp line items
    Map<Id, OpportunityLineItem> productIdToOli = new Map<Id, OpportunityLineItem>();
    List<OpportunityLineItem> olis = [SELECT Id, PricebookEntry.Product2Id, Start_Date__c, Duration__c, End_Date__c FROM OpportunityLineItem WHERE OpportunityId = :qlis[0].Quote.OpportunityId];
    if (olis != null) {
        for (OpportunityLineItem oli : olis) {
            productIdToOli.put(oli.PricebookEntry.Product2Id, oli);
        }
    }

    // Iterate across quote line items
    for (QuoteLineItem qli : qlis) {
        OpportunityLineItem oli = productIdToOli.get(qli.PricebookEntry.Product2Id);  
        if (oli != null) {
            if (oli.Start_Date__c != null) { qli.Start_Date__c = oli.Start_Date__c; }
            if (oli.Duration__c   != null) { qli.Duration__c   = oli.Duration__c ;  }
        }
    }
   
    // Update
    update qlis;
}

And this is the Test Class: 

@isTest(SeeAllData=true)
public class TestCopyOpportunityProductFields    {
    static testMethod void testCopy() {
   
        Product2 p = new Product2();
        p.Name = 'SFDC99 Rocks';
        p.Deferral_Percentage__c = 0;
        insert p;
       
        Pricebook2 pb = [SELECT Id FROM Pricebook2 where isStandard = true];
        PricebookEntry pbe = new PricebookEntry();
        pbe.Product2Id   = p.Id;
        pbe.UnitPrice    = 99;
        pbe.Pricebook2Id = pb.Id;
        pbe.IsActive     = true;
        insert pbe;
       
        Opportunity o = new Opportunity();
        o.CloseDate = Date.today() + 30;
        o.StageName = 'New';
        o.Name      = 'Follow me in Twitter';
        insert o;
       
        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId    = o.Id;
        oli.PricebookEntryId = pbe.Id;
        oli.Quantity         = 99;
        oli.UnitPrice        = 1;
        oli.Start_Date__c    = Date.today();
        oli.Duration__c      = 5;
        insert oli;
       
        Quote q = new Quote();
        q.OpportunityId = o.Id;
        q.Name          = '@dvdkliu';
        q.Pricebook2Id  = pb.Id;
        insert q;
       
        QuoteLineItem qli = new QuoteLineItem();
        qli.PricebookEntryId = pbe.Id;
        qli.QuoteId          = q.Id;
        qli.Quantity         = 99;
        qli.UnitPrice        = 1;
        insert qli;
       
        List<QuoteLineItem> qlis = [SELECT Id, Start_Date__c, Duration__c FROM QuoteLineItem WHERE Id = :qli.Id];
        //System.assertEquals(oli.Start_Date__c, qli[0].Start_Date__c);
        //System.assertEquals(oli.Duration__c, qli[0].Duration__c);
       
    }
}
Peter_sfdcPeter_sfdc
You say it "appears to be no longer doing its job", can you describe more clearly what outcome you are getting. Does it error, do you not see all of the data you expect? What exactly is the problem. 

One thing I notice from this code is that no where is the end date copied as you suggest it is intended to do. 

Another thing is that the System.assert...calls are commented out, suggesting that the tests for your code were failing and thus were disabled. 

Seriously, though, some more details as to what exactly is happening would help. Is there an error. It would be nice to see that error. I don't see anything massively wrong with the code apart from no copying of end date. 

Does it work some of the time, and then not others? If so, under which circumstances does it succeed/fail.