• emillar_altia
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 3
    Replies
I've written a trigger but keep getting the same error, I'm unsure what the error is, when I run my test class the message reads: Variable does not exist: productIdToQli

My trigger is below and I've boldened the issue, does anyone know what Im doing wrong? 

trigger QuotetoOppSync on OpportunityLineItem (after insert) {

//Query opportunity line items
    List<OpportunityLineItem> olis= [SELECT Id, PricebookEntry.Product2Id, OpportunityId, Start_date__c, Duration__c,
                             End_date__c FROM
                                    OpportunityLineItem WHERE Id IN: Trigger.new];
    //Get a list of all opp ids:
    List<id> oppids= new List<id>();
    for (opportunityLineItem oli:olis){
        oppIds.add(oli.OpportunityId);
    }
   
    //Query QuoteLineItems by Opportunity ID
    Map<Id, QuotelineItem> producttoQli = new Map<Id, QuoteLineItem>();
    List<QuoteLineItem> qlis=[SELECT Id, PricebookEntry.Product2Id, Start_date__c, Duration__c,
                             End_date__c, Quote.OpportunityId FROM QuoteLineItem WHERE Quote.OpportunityId IN :oppIds];
    if(qlis!=null){
        for(QuoteLineItem qli:qlis){
            productIdToQli.put(qli.PricebookEntry.Product2Id, qli);
        }
    }
//Iterate across quote line items
    for (OpportunityLineItem oli : Olis) {
       
        Quotelineitem qli=productIdToQli.get(oli.PricebookEntry.productId);
        if(qli!=null){
            if(qli.Start_Date__c!=null){ oli.Start_Date__c=qli.Start_Date__c;}
            If(qli.Duration__c!=null) {oli.Duration__c=qli.Duration__c;}
            if(qli.End_Date__c!=null){ oli.End_Date__c=qli.End_Date__c;}
        }
    }
    //Update
    update olis;
}
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);
       
    }
}

I've just started to learn code on my own based on stuff I can find on the internet but was thinking of buying a book for a bit more structure and an advanced insight. Can anyone recommend any good ones? Price isn't a major problem as my work will cover the cost. 

I have a test class in sandbox which has passed, but when I take it into Production I get the below error, I don't understand what the problem is when its passed in Sandbox? 

 

TestCopyOpportunityProductFields.testCopy() Class 47 1

Failure Message: "System.AssertException: Assertion Failed: Expected: 2013-10-22 00:00:00, Actual: null", Failure Stack Trace: "Class.TestCopyOpportunityProductFields.testCopy: line 47, column 1"

 

I have copied in my test class below. 

 

@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, qlis[0].Start_Date__c);
        System.assertEquals(oli.Duration__c, qlis[0].Duration__c);
        
    }
}

 

 

Also, my trigger is saying this. 

 

CopyOpportunityProductFields       Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

 

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;
}

I have created three custom fields for my product line items - 'StartDate', 'Duration' and 'EndDate'. I have then created the same fields in my Quote line items as I need them to appear on my quotes. I am brand new to apex and its taking a lot of reading and patience to figure out where to start. I need to get this sorted my next week or I'll be in trouble with my boss. 

 

What my custom fields do: The start date is self explanatory, the duration is the number of months in one of our Support contracts, the duration is entered after the start date, which then calculates the end date. For example;

Start date 01/01/2014 - Duration 12 (hit save) End Date calculates: 01/01/2015
 
When I hit new quote, and the information from my product line items is copied into my Quote Line Items, I need my custom information to do the same. Does anyone know how I can go about this. Below is what I have started think I am totally way off. Help! 
 
trigger AutopopulateStartDate on QuoteLineItems (after insert, after update) {
//. Create a container for the products that need start dates inserted to them
Set updatedStartDate = new
//If this is an insert, I want the date to be inserted to the correct product
if (trigger.isInsert) {
for (StartDate s : trigger.new) {
productstartdate.add;
 
 
 
 

I have created three custom fields for my product line items - 'StartDate', 'Duration' and 'EndDate'. The Start Date and EndDate are pretty self explanatory but the Duration field calculates the end date by the number of months added to the start date. I have created a new quote process where I am looking to send multiple quotes in one email to one customer. This process has been successful to a point, my problem is that when I create a quote, the information in my custom fields 'Start Date', 'Duration' and 'End Date' are not taken from the Product Line Items in the opportunity over into the Quote Line Items. I have been looking at apex code and have a tiny bit of experience with Java to create a trigger to do this, below is what I have started but I really don't think I'm going in the right direction as I'm looking at too many examples. Does anyone have a spare moment to point me in the right direction? 

 

trigger AutopopulateStartDate on QuoteLineItems (after insert, after update) {

 

//. Create a container for the products that need start dates inserted to them

 

Set <QuoteLineItems> updatedStartDate = new <QuoteLineItems> 


//If this is an insert, I want the date to be inserted to the correct product

 

if (trigger.isInsert) {

 

for (StartDate s : trigger.new) {

 

productstartdate.add;

 

I have a test class in sandbox which has passed, but when I take it into Production I get the below error, I don't understand what the problem is when its passed in Sandbox? 

 

TestCopyOpportunityProductFields.testCopy() Class 47 1

Failure Message: "System.AssertException: Assertion Failed: Expected: 2013-10-22 00:00:00, Actual: null", Failure Stack Trace: "Class.TestCopyOpportunityProductFields.testCopy: line 47, column 1"

 

I have copied in my test class below. 

 

@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, qlis[0].Start_Date__c);
        System.assertEquals(oli.Duration__c, qlis[0].Duration__c);
        
    }
}

 

 

Also, my trigger is saying this. 

 

CopyOpportunityProductFields       Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

 

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;
}

I have created three custom fields for my product line items - 'StartDate', 'Duration' and 'EndDate'. I have then created the same fields in my Quote line items as I need them to appear on my quotes. I am brand new to apex and its taking a lot of reading and patience to figure out where to start. I need to get this sorted my next week or I'll be in trouble with my boss. 

 

What my custom fields do: The start date is self explanatory, the duration is the number of months in one of our Support contracts, the duration is entered after the start date, which then calculates the end date. For example;

Start date 01/01/2014 - Duration 12 (hit save) End Date calculates: 01/01/2015
 
When I hit new quote, and the information from my product line items is copied into my Quote Line Items, I need my custom information to do the same. Does anyone know how I can go about this. Below is what I have started think I am totally way off. Help! 
 
trigger AutopopulateStartDate on QuoteLineItems (after insert, after update) {
//. Create a container for the products that need start dates inserted to them
Set updatedStartDate = new
//If this is an insert, I want the date to be inserted to the correct product
if (trigger.isInsert) {
for (StartDate s : trigger.new) {
productstartdate.add;