• Lights
  • NEWBIE
  • 30 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 7
    Replies
Hello Friends,

I'm getting the following error because the trigger doesn't have enough coverage. How do I accomplish that? Thank you!
The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
 
trigger Product00001 on OpportunityLineItem( after insert, after update )
{
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    Set<Id> opptyIds = new Set<Id>();
 
    for( OpportunityLineItem optLineItem: trigger.new )
    {
        if( optLineItem.ProductCode == '00001a' )
        {
            opptyIds.add( optLineItem.OpportunityId );
        }
    }
 
    if( opptyIds.size() > 0 )
    {
        //retrieve the values based on Product list
List<OpportunityLineItem> lstOpptyLineItems = [ SELECT Opportunity.Pricebook2Id, OpportunityId, Name, ProductCode,
                                                        PricebookEntryId, Quantity, UnitPrice
                                                        FROM OpportunityLineItem
                                                        WHERE OpportunityId IN: opptyIds order by ProductCode ];
 
        Map<Id, List<OpportunityLineItem>> mapOpptyLineItem = new Map<Id, List<OpportunityLineItem>>();
        for( OpportunityLineItem item : lstOpptyLineItems )
        {
            List<OpportunityLineItem> oliList1 = new List<OpportunityLineItem>();
            if( mapOpptyLineItem.containsKey( item.OpportunityId ))
            {
                oliList1 = mapOpptyLineItem.get( item.OpportunityId );
            }
            
            oliList1.add( item );
            mapOpptyLineItem.put( item.OpportunityId, oliList1 );
        }
 
 
        //retrieve PriceBookEntry of the Product B, this is most important
        PricebookEntry pbeProduct2 = [ SELECT Id, Pricebook2Id, UnitPrice, Name, Percentage_Entry__c
                                        FROM PricebookEntry
                                        WHERE Name ='Product 00001'
                                        AND Pricebook2Id  IN (SELECT Id FROM PriceBook2 WHERE Id ='01sA00000004lbRIAQ')
                                        LIMIT 1 ];
 
         if( trigger.isInsert )
         {
             for( Id oppId : mapOpptyLineItem.keySet() )
             {
                 for( OpportunityLineItem item : mapOpptyLineItem.get( oppId ))
                 {
                     if( item.ProductCode == '00001a' )
                     {
                         oliList.add( new OpportunityLineItem(
                                            OpportunityId = oppId,
                                            PricebookEntryId = pbeProduct2.Id,
                                            Quantity = item.Quantity,
                                            UnitPrice = item.UnitPrice * pbeProduct2.Percentage_Entry__c * 0.01 )
                                          );
                     }
                 }
             }
             
             if( oliList.size() > 0 )
                 insert oliList;
         }
         else if( trigger.isUpdate )
         {
             for( Id oppId : mapOpptyLineItem.keySet() )
             {
                 OpportunityLineItem itemProductA = new OpportunityLineItem();
                 for( OpportunityLineItem item : mapOpptyLineItem.get( oppId ))
                 {
                     if( item.ProductCode == '00001a' )
                     {
                         itemProductA = item;
                         break;
                     }
                 }

                 for( OpportunityLineItem item : mapOpptyLineItem.get( oppId ))
                 {
                     if( item.ProductCode == '00001b' )
                     {
                         oliList.add( new OpportunityLineItem( Id = item.Id,
                                            OpportunityId = oppId,
                                            PricebookEntryId = pbeProduct2.Id,
                                            Quantity = itemProductA.Quantity,
                                            UnitPrice = itemProductA.UnitPrice * pbeProduct2.Percentage_Entry__c * 0.01 )
                                          );
                     }
                 }
             }
             
             if( oliList.size() > 0 )
                 update oliList;
         }
    }
}

 
  • December 14, 2017
  • Like
  • 0
I have the following apex trigger and I need a test class. Can you guys check what I got so far?

Apex Trigger
trigger NOVEMBER2 on OpportunityLineItem( after insert, after update )
{
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    Set<Id> opptyIds = new Set<Id>();
 
    for( OpportunityLineItem optLineItem: trigger.new )
    {
        if( optLineItem.ProductCode == '00002a' )
        {
            opptyIds.add( optLineItem.OpportunityId );
        }
    }
 
    if( opptyIds.size() > 0 )
    {
        //retrieve the values based on Product list
        List<OpportunityLineItem> lstOpptyLineItems = [ SELECT Opportunity.Pricebook2Id, OpportunityId, Name, ProductCode,
                                                        PricebookEntryId, Quantity, UnitPrice
                                                        FROM OpportunityLineItem
                                                        WHERE OpportunityId IN: opptyIds order by ProductCode ];
 
        Map<Id, List<OpportunityLineItem>> mapOpptyLineItem = new Map<Id, List<OpportunityLineItem>>();
        for( OpportunityLineItem item : lstOpptyLineItems )
        {
            List<OpportunityLineItem> oliList1 = new List<OpportunityLineItem>();
            if( mapOpptyLineItem.containsKey( item.OpportunityId ))
            {
                oliList1 = mapOpptyLineItem.get( item.OpportunityId );
            }
            
            oliList1.add( item );
            mapOpptyLineItem.put( item.OpportunityId, oliList1 );
        }
 
 
        //retrieve PriceBookEntry of the Product B, this is most important
        PricebookEntry pbeProduct2 = [ SELECT Id, Pricebook2Id, UnitPrice, Name, Pass_Through_Percentage_Entry__c
                                        FROM PricebookEntry
                                        WHERE Name ='Car Oil Fee'
                                        AND Pricebook2Id  IN (SELECT Id FROM PriceBook2 WHERE Id ='01sA00000004lbRIAQ')
                                        LIMIT 1 ];
 
         if( trigger.isInsert )
         {
             for( Id oppId : mapOpptyLineItem.keySet() )
             {
                 for( OpportunityLineItem item : mapOpptyLineItem.get( oppId ))
                 {
                     if( item.ProductCode == '00002a' )
                     {
                         oliList.add( new OpportunityLineItem(
                                            OpportunityId = oppId,
                                            PricebookEntryId = pbeProduct2.Id,
                                            Quantity = item.Quantity,
                                            UnitPrice = item.UnitPrice * pbeProduct2.Pass_Through_Percentage_Entry__c * 0.01 )
                                          );
                     }
                 }
             }
             
             if( oliList.size() > 0 )
                 insert oliList;
         }
         else if( trigger.isUpdate )
         {
             for( Id oppId : mapOpptyLineItem.keySet() )
             {
                 OpportunityLineItem itemProductA = new OpportunityLineItem();
                 for( OpportunityLineItem item : mapOpptyLineItem.get( oppId ))
                 {
                     if( item.ProductCode == '00002a' )
                     {
                         itemProductA = item;
                         continue;
                     }
                     if( item.ProductCode == '00002b' )
                     {
                         oliList.add( new OpportunityLineItem( Id = item.Id,
                                            OpportunityId = oppId,
                                            PricebookEntryId = pbeProduct2.Id,
                                            Quantity = itemProductA.Quantity,
                                            UnitPrice = itemProductA.UnitPrice * pbeProduct2.Pass_Through_Percentage_Entry__c * 0.01 )
                                          );
                     }
                 }
             }
             
             if( oliList.size() > 0 )
                 update oliList;
         }
    }
}
Test Class:
@isTest
private class OpportunityLineItemTrigger_Test{
  static testMethod void test_OpportunityLineItemTrigger(){
   test.startTest();
    OpportunityLineItem opportunitylineitem_Obj = new OpportunityLineItem(OpportunityId = opportunity_Obj[0].id, PricebookEntryId = pricebookentry_Obj[0].id, Quantity = 9, UnitPrice = 13, Package_Delivery_Date__c = false, Batch_Readiness__c = false);
    Insert opportunitylineitem_Obj; 
   test.stopTest();
  }

  static testMethod void test_UseCase2(){
   test.startTest();
    OpportunityLineItem opportunitylineitem_Obj = new OpportunityLineItem(OpportunityId = opportunity_Obj[0].id, PricebookEntryId = pricebookentry_Obj[0].id, Quantity = 9, UnitPrice = 13, Package_Delivery_Date__c = false, Batch_Readiness__c = false);
    opportunitylineitem_Obj.ProductCode='UseCase1-0';
    Insert opportunitylineitem_Obj; 
    test.stopTest();
}
}


 
  • November 22, 2017
  • Like
  • 0
Not a duplicate question guys. Different aspect of the code.

Goal of the code:
Auto add product B once product A is added to the opportunity

Issue:
Adding product B to a different opportunity and not to the opportunity that product A is being added to.

Any idea why? or what part of the code that I should focus on to test different solutions?

Code:
trigger NOVEMBER1 on OpportunityLineItem (after insert, after update)
    {
        List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    
        List<String> lstProductCodes = new List<String>();
     
        for(OpportunityLineItem optLineItem: Trigger.new)
        {
            if(optLineItem.ProductCode == '00002a')
            {
                lstProductCodes.add(optLineItem.ProductCode);
            }
        }
     
        if(lstProductCodes.size()>0)
        {
            System.debug('lstProductCodes=' + lstProductCodes);
     
            //retrieve the values based on Product list
            List<OpportunityLineItem> lstOpptyLineItems = [SELECT Opportunity.Pricebook2Id, Name, ProductCode , PricebookEntryId, Quantity, UnitPrice
                                                            FROM    OpportunityLineItem
                                                            WHERE ProductCode IN:lstProductCodes];
     
            //create a map which contains Product Name and OpportunityLineItem
            Map<String, OpportunityLineItem> mapOpptyLineItem = new Map<String, OpportunityLineItem>();
            for(OpportunityLineItem item:lstOpptyLineItems)
            {
                mapOpptyLineItem.put(item.ProductCode, item);
            }
     
     
            Id pbkId = lstOpptyLineItems[0].Opportunity.Pricebook2Id;
     
            //retrieve PriceBookEntry of the Product B, this is most important
            PricebookEntry pbeProduct2 = [SELECT Id, Pricebook2Id, UnitPrice, Name, Fee_Percentage_Entry__c 
                                            FROM PricebookEntry
                                            WHERE Name ='Car Oil Fee'
                                            AND Pricebook2Id  IN (SELECT Id FROM PriceBook2 WHERE Id ='01sA00000004lbRIAQ')
                                            LIMIT 1];
     
     
            //retrieve Product A item from the map.        
            OpportunityLineItem itemProductA = mapOpptyLineItem.get('00002a');
            System.debug('itemProductA= ' + itemProductA);
     
            if(itemProductA != null)
            {
                if(Trigger.isInsert)
                {
                //now assign Product A items as required, you can retrieve the amount from Product A
                oliList.add(new OpportunityLineItem(
                    OpportunityId = itemProductA.OpportunityId,
                    PricebookEntryId = pbeProduct2.Id,
                    Quantity = itemProductA.Quantity,
                    UnitPrice = itemProductA.UnitPrice * pbeProduct2.Fee_Percentage_Entry__c * 0.01 )
                  );
                System.debug('oliList=' + oliList);
                insert oliList;
            }
            else if (Trigger.isUpdate)
                {
                    //if you need to update PriceBookEntry of Product B
                    pbeProduct2.UnitPrice = itemProductA.UnitPrice * pbeProduct2.Fee_Percentage_Entry__c * 0.01 ;
                    update pbeProduct2;
     
                    //if you need to update OpportunityLineItem of Product B
                    OpportunityLineItem optLIProductB = [SELECT OpportunityId, Opportunity.Pricebook2Id,
                                                            Name, ProductCode , PricebookEntryId,
                                                            Quantity, UnitPrice
                                                            FROM    OpportunityLineItem
                                                            WHERE ProductCode = '00002b'];
     
                    optLIProductB.Quantity = mapOpptyLineItem.get('00002a').Quantity;
                    optLIProductB.UnitPrice = mapOpptyLineItem.get('00002a').UnitPrice * pbeProduct2.Fee_Percentage_Entry__c * 0.01 ;
     
                    update optLIProductB;
                }
        }
    }
    }

 
  • November 14, 2017
  • Like
  • 0

Hello friends.

I'm having issue with this trigger in the Opportunity Products. It works fine in the Developer Sandbox, but it doesn't work in the Partial Sandbox or Full Sandbox... so I doubt that it will work in the production. Can you guys help me out? I tried everything...

I have the following trigger:

trigger AutoAddPF00001 on OpportunityLineItem (after insert, after update)
{
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();

    List<String> lstProductCodes = new List<String>();

    for(OpportunityLineItem optLineItem: Trigger.new)
    {
        if(optLineItem.ProductCode == '00001a')
        {
            lstProductCodes.add(optLineItem.ProductCode);
        }
    }

    if(lstProductCodes.size()>0)
    {
        System.debug('lstProductCodes=' + lstProductCodes);

        //retrieve the values based on Product list
        List<OpportunityLineItem> lstOpptyLineItems = [SELECT OpportunityId, Opportunity.Pricebook2Id, Name, ProductCode , PricebookEntryId, Quantity, UnitPrice
                                                        FROM    OpportunityLineItem
                                                        WHERE ProductCode IN:lstProductCodes];

        //create a map which contains Product Name and OpportunityLineItem
        Map<String, OpportunityLineItem> mapOpptyLineItem = new Map<String, OpportunityLineItem>();
        for(OpportunityLineItem item:lstOpptyLineItems)
        {
            mapOpptyLineItem.put(item.ProductCode, item);
        }


        Id pbkId = lstOpptyLineItems[0].Opportunity.Pricebook2Id;

        //retrieve PriceBookEntry of the Product B, this is most important
        PricebookEntry pbeProduct2 = [SELECT Id, Pricebook2Id, UnitPrice, Name, Product_Fee_Percentage__c 
                                        FROM PricebookEntry
                                        WHERE Name ='Product Fee'
                                        AND Pricebook2Id  IN (SELECT Id FROM PriceBook2 WHERE Id =:pbkId) LIMIT 1];

        //retrieve Product A item from the map.        
        OpportunityLineItem itemProductA = mapOpptyLineItem.get('00001a');
        System.debug('itemProductA= ' + itemProductA);

        if(itemProductA != null)
        {
            if(Trigger.isInsert)
            {
            //now assign Product A items as required, you can retrieve the amount from Product A
            oliList.add(new OpportunityLineItem(
                OpportunityId = itemProductA.OpportunityId,
                PricebookEntryId = pbeProduct2.Id,
                Quantity = itemProductA.Quantity,
                UnitPrice = itemProductA.UnitPrice * pbeProduct2.Product_Fee_Percentage__c * 0.01 )
              );
            System.debug('oliList=' + oliList);
            insert oliList;
        }
        else if (Trigger.isUpdate)
            {
                //if you need to update PriceBookEntry of Product B
                pbeProduct2.UnitPrice = itemProductA.UnitPrice * pbeProduct2.Product_Fee_Percentage__c * 0.01 ;
                update pbeProduct2;

                //if you need to update OpportunityLineItem of Product B
                OpportunityLineItem optLIProductB = [SELECT OpportunityId, Opportunity.Pricebook2Id,
                                                        Name, ProductCode , PricebookEntryId,
                                                        Quantity, UnitPrice
                                                        FROM    OpportunityLineItem
                                                        WHERE ProductCode = '00001b'];

                optLIProductB.Quantity = mapOpptyLineItem.get('00001a').Quantity;
                optLIProductB.UnitPrice = mapOpptyLineItem.get('00001a').UnitPrice * pbeProduct2.Product_Fee_Percentage__c * 0.01 ;

                update optLIProductB;
            }
    }
}
}

I'm getting the following error:
Apex trigger AutoAddPF00001 caused an unexpected exception, contact your administrator: AutoAddPF00001: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.AutoAddPF00001: line 35, column 1

Line 35 is:
PricebookEntry pbeProduct2 = [SELECT Id, Pricebook2Id, UnitPrice, Name, Product_Fee_Percentage__c

I tried the solution given by the article below, but it causes more issues.
https://help.salesforce.com/articleView?id=000159853&type=1

So I used the following code with some help:​
List<PricebookEntry> pbeProduct2=new List<PricebookEntry>();
 pbeProduct2 = [SELECT Id, Pricebook2Id, UnitPrice, Name, Product_Fee_Percentage__c 
                            FROM PricebookEntry
                            WHERE Name ='Product Fee'
                            AND Pricebook2Id  IN (SELECT Id FROM PriceBook2 WHERE Id =:pbkId) LIMIT 1];
System.debug('pbeProduct2 '+pbeProduct2);

I get the following error if I try the solution given in the link above:
Error: Compile Error: Variable does not exist: Id at line 53 column 48

Line 53 is:
PricebookEntryId = pbeProduct2.Id,

  • October 11, 2017
  • Like
  • 0
Hello Friends,

I'm getting the following error because the trigger doesn't have enough coverage. How do I accomplish that? Thank you!
The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
 
trigger Product00001 on OpportunityLineItem( after insert, after update )
{
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    Set<Id> opptyIds = new Set<Id>();
 
    for( OpportunityLineItem optLineItem: trigger.new )
    {
        if( optLineItem.ProductCode == '00001a' )
        {
            opptyIds.add( optLineItem.OpportunityId );
        }
    }
 
    if( opptyIds.size() > 0 )
    {
        //retrieve the values based on Product list
List<OpportunityLineItem> lstOpptyLineItems = [ SELECT Opportunity.Pricebook2Id, OpportunityId, Name, ProductCode,
                                                        PricebookEntryId, Quantity, UnitPrice
                                                        FROM OpportunityLineItem
                                                        WHERE OpportunityId IN: opptyIds order by ProductCode ];
 
        Map<Id, List<OpportunityLineItem>> mapOpptyLineItem = new Map<Id, List<OpportunityLineItem>>();
        for( OpportunityLineItem item : lstOpptyLineItems )
        {
            List<OpportunityLineItem> oliList1 = new List<OpportunityLineItem>();
            if( mapOpptyLineItem.containsKey( item.OpportunityId ))
            {
                oliList1 = mapOpptyLineItem.get( item.OpportunityId );
            }
            
            oliList1.add( item );
            mapOpptyLineItem.put( item.OpportunityId, oliList1 );
        }
 
 
        //retrieve PriceBookEntry of the Product B, this is most important
        PricebookEntry pbeProduct2 = [ SELECT Id, Pricebook2Id, UnitPrice, Name, Percentage_Entry__c
                                        FROM PricebookEntry
                                        WHERE Name ='Product 00001'
                                        AND Pricebook2Id  IN (SELECT Id FROM PriceBook2 WHERE Id ='01sA00000004lbRIAQ')
                                        LIMIT 1 ];
 
         if( trigger.isInsert )
         {
             for( Id oppId : mapOpptyLineItem.keySet() )
             {
                 for( OpportunityLineItem item : mapOpptyLineItem.get( oppId ))
                 {
                     if( item.ProductCode == '00001a' )
                     {
                         oliList.add( new OpportunityLineItem(
                                            OpportunityId = oppId,
                                            PricebookEntryId = pbeProduct2.Id,
                                            Quantity = item.Quantity,
                                            UnitPrice = item.UnitPrice * pbeProduct2.Percentage_Entry__c * 0.01 )
                                          );
                     }
                 }
             }
             
             if( oliList.size() > 0 )
                 insert oliList;
         }
         else if( trigger.isUpdate )
         {
             for( Id oppId : mapOpptyLineItem.keySet() )
             {
                 OpportunityLineItem itemProductA = new OpportunityLineItem();
                 for( OpportunityLineItem item : mapOpptyLineItem.get( oppId ))
                 {
                     if( item.ProductCode == '00001a' )
                     {
                         itemProductA = item;
                         break;
                     }
                 }

                 for( OpportunityLineItem item : mapOpptyLineItem.get( oppId ))
                 {
                     if( item.ProductCode == '00001b' )
                     {
                         oliList.add( new OpportunityLineItem( Id = item.Id,
                                            OpportunityId = oppId,
                                            PricebookEntryId = pbeProduct2.Id,
                                            Quantity = itemProductA.Quantity,
                                            UnitPrice = itemProductA.UnitPrice * pbeProduct2.Percentage_Entry__c * 0.01 )
                                          );
                     }
                 }
             }
             
             if( oliList.size() > 0 )
                 update oliList;
         }
    }
}

 
  • December 14, 2017
  • Like
  • 0
Not a duplicate question guys. Different aspect of the code.

Goal of the code:
Auto add product B once product A is added to the opportunity

Issue:
Adding product B to a different opportunity and not to the opportunity that product A is being added to.

Any idea why? or what part of the code that I should focus on to test different solutions?

Code:
trigger NOVEMBER1 on OpportunityLineItem (after insert, after update)
    {
        List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    
        List<String> lstProductCodes = new List<String>();
     
        for(OpportunityLineItem optLineItem: Trigger.new)
        {
            if(optLineItem.ProductCode == '00002a')
            {
                lstProductCodes.add(optLineItem.ProductCode);
            }
        }
     
        if(lstProductCodes.size()>0)
        {
            System.debug('lstProductCodes=' + lstProductCodes);
     
            //retrieve the values based on Product list
            List<OpportunityLineItem> lstOpptyLineItems = [SELECT Opportunity.Pricebook2Id, Name, ProductCode , PricebookEntryId, Quantity, UnitPrice
                                                            FROM    OpportunityLineItem
                                                            WHERE ProductCode IN:lstProductCodes];
     
            //create a map which contains Product Name and OpportunityLineItem
            Map<String, OpportunityLineItem> mapOpptyLineItem = new Map<String, OpportunityLineItem>();
            for(OpportunityLineItem item:lstOpptyLineItems)
            {
                mapOpptyLineItem.put(item.ProductCode, item);
            }
     
     
            Id pbkId = lstOpptyLineItems[0].Opportunity.Pricebook2Id;
     
            //retrieve PriceBookEntry of the Product B, this is most important
            PricebookEntry pbeProduct2 = [SELECT Id, Pricebook2Id, UnitPrice, Name, Fee_Percentage_Entry__c 
                                            FROM PricebookEntry
                                            WHERE Name ='Car Oil Fee'
                                            AND Pricebook2Id  IN (SELECT Id FROM PriceBook2 WHERE Id ='01sA00000004lbRIAQ')
                                            LIMIT 1];
     
     
            //retrieve Product A item from the map.        
            OpportunityLineItem itemProductA = mapOpptyLineItem.get('00002a');
            System.debug('itemProductA= ' + itemProductA);
     
            if(itemProductA != null)
            {
                if(Trigger.isInsert)
                {
                //now assign Product A items as required, you can retrieve the amount from Product A
                oliList.add(new OpportunityLineItem(
                    OpportunityId = itemProductA.OpportunityId,
                    PricebookEntryId = pbeProduct2.Id,
                    Quantity = itemProductA.Quantity,
                    UnitPrice = itemProductA.UnitPrice * pbeProduct2.Fee_Percentage_Entry__c * 0.01 )
                  );
                System.debug('oliList=' + oliList);
                insert oliList;
            }
            else if (Trigger.isUpdate)
                {
                    //if you need to update PriceBookEntry of Product B
                    pbeProduct2.UnitPrice = itemProductA.UnitPrice * pbeProduct2.Fee_Percentage_Entry__c * 0.01 ;
                    update pbeProduct2;
     
                    //if you need to update OpportunityLineItem of Product B
                    OpportunityLineItem optLIProductB = [SELECT OpportunityId, Opportunity.Pricebook2Id,
                                                            Name, ProductCode , PricebookEntryId,
                                                            Quantity, UnitPrice
                                                            FROM    OpportunityLineItem
                                                            WHERE ProductCode = '00002b'];
     
                    optLIProductB.Quantity = mapOpptyLineItem.get('00002a').Quantity;
                    optLIProductB.UnitPrice = mapOpptyLineItem.get('00002a').UnitPrice * pbeProduct2.Fee_Percentage_Entry__c * 0.01 ;
     
                    update optLIProductB;
                }
        }
    }
    }

 
  • November 14, 2017
  • Like
  • 0

Hello friends.

I'm having issue with this trigger in the Opportunity Products. It works fine in the Developer Sandbox, but it doesn't work in the Partial Sandbox or Full Sandbox... so I doubt that it will work in the production. Can you guys help me out? I tried everything...

I have the following trigger:

trigger AutoAddPF00001 on OpportunityLineItem (after insert, after update)
{
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();

    List<String> lstProductCodes = new List<String>();

    for(OpportunityLineItem optLineItem: Trigger.new)
    {
        if(optLineItem.ProductCode == '00001a')
        {
            lstProductCodes.add(optLineItem.ProductCode);
        }
    }

    if(lstProductCodes.size()>0)
    {
        System.debug('lstProductCodes=' + lstProductCodes);

        //retrieve the values based on Product list
        List<OpportunityLineItem> lstOpptyLineItems = [SELECT OpportunityId, Opportunity.Pricebook2Id, Name, ProductCode , PricebookEntryId, Quantity, UnitPrice
                                                        FROM    OpportunityLineItem
                                                        WHERE ProductCode IN:lstProductCodes];

        //create a map which contains Product Name and OpportunityLineItem
        Map<String, OpportunityLineItem> mapOpptyLineItem = new Map<String, OpportunityLineItem>();
        for(OpportunityLineItem item:lstOpptyLineItems)
        {
            mapOpptyLineItem.put(item.ProductCode, item);
        }


        Id pbkId = lstOpptyLineItems[0].Opportunity.Pricebook2Id;

        //retrieve PriceBookEntry of the Product B, this is most important
        PricebookEntry pbeProduct2 = [SELECT Id, Pricebook2Id, UnitPrice, Name, Product_Fee_Percentage__c 
                                        FROM PricebookEntry
                                        WHERE Name ='Product Fee'
                                        AND Pricebook2Id  IN (SELECT Id FROM PriceBook2 WHERE Id =:pbkId) LIMIT 1];

        //retrieve Product A item from the map.        
        OpportunityLineItem itemProductA = mapOpptyLineItem.get('00001a');
        System.debug('itemProductA= ' + itemProductA);

        if(itemProductA != null)
        {
            if(Trigger.isInsert)
            {
            //now assign Product A items as required, you can retrieve the amount from Product A
            oliList.add(new OpportunityLineItem(
                OpportunityId = itemProductA.OpportunityId,
                PricebookEntryId = pbeProduct2.Id,
                Quantity = itemProductA.Quantity,
                UnitPrice = itemProductA.UnitPrice * pbeProduct2.Product_Fee_Percentage__c * 0.01 )
              );
            System.debug('oliList=' + oliList);
            insert oliList;
        }
        else if (Trigger.isUpdate)
            {
                //if you need to update PriceBookEntry of Product B
                pbeProduct2.UnitPrice = itemProductA.UnitPrice * pbeProduct2.Product_Fee_Percentage__c * 0.01 ;
                update pbeProduct2;

                //if you need to update OpportunityLineItem of Product B
                OpportunityLineItem optLIProductB = [SELECT OpportunityId, Opportunity.Pricebook2Id,
                                                        Name, ProductCode , PricebookEntryId,
                                                        Quantity, UnitPrice
                                                        FROM    OpportunityLineItem
                                                        WHERE ProductCode = '00001b'];

                optLIProductB.Quantity = mapOpptyLineItem.get('00001a').Quantity;
                optLIProductB.UnitPrice = mapOpptyLineItem.get('00001a').UnitPrice * pbeProduct2.Product_Fee_Percentage__c * 0.01 ;

                update optLIProductB;
            }
    }
}
}

I'm getting the following error:
Apex trigger AutoAddPF00001 caused an unexpected exception, contact your administrator: AutoAddPF00001: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.AutoAddPF00001: line 35, column 1

Line 35 is:
PricebookEntry pbeProduct2 = [SELECT Id, Pricebook2Id, UnitPrice, Name, Product_Fee_Percentage__c

I tried the solution given by the article below, but it causes more issues.
https://help.salesforce.com/articleView?id=000159853&type=1

So I used the following code with some help:​
List<PricebookEntry> pbeProduct2=new List<PricebookEntry>();
 pbeProduct2 = [SELECT Id, Pricebook2Id, UnitPrice, Name, Product_Fee_Percentage__c 
                            FROM PricebookEntry
                            WHERE Name ='Product Fee'
                            AND Pricebook2Id  IN (SELECT Id FROM PriceBook2 WHERE Id =:pbkId) LIMIT 1];
System.debug('pbeProduct2 '+pbeProduct2);

I get the following error if I try the solution given in the link above:
Error: Compile Error: Variable does not exist: Id at line 53 column 48

Line 53 is:
PricebookEntryId = pbeProduct2.Id,

  • October 11, 2017
  • Like
  • 0