+ Start a Discussion
RelaxItsJustCodeRelaxItsJustCode 

Need some help with a custom contract line item object's product selection. Kudos available!

I have 2 loops that are causing some small problems.

 

In the code below you will note that I have two loops that hit the PriceBookEntry table (pbe1).  The first loop searches through out main production pricebook.  The second loop, loops through the PriceBookEntry table (pbe2) searching to find the productid in the standard pricebook.

 

The  PROBLEM:

 

When the first loop finds the productId I am looking for and grabs the listprice out of the main pricebook, it sometimes continues to loop through the second loop through the standard and then the update has two duplicate id's and blows to high hell and back.

 

Anyone that contributes will get kudos.

 

The person that gets me closest to the solution will get solution credit as well.

 

Here's the code:

 

public with sharing class MaintContractLineItemCreation 
{    
    Public static String ProductLineState = 'Not Found';
    Public static void ContractLineItemInsert(Map<Id,Contract_Line_Item__c> oldMap, Map<Id,Contract_Line_Item__c> newMap)
    {
    
    List<Id> CLIIds = new List<Id>();
    map<Id,String> CLIMap = new map <Id,String>(); 
    
    List<Id> Prodid = new List<Id>();
    
    List<id> PBE = new List<Id>();
    
    for (Contract_Line_Item__c c : newMap.Values())
    {   
        if(c.Product_LU__c != null)
        {      
             CLIIds.add(c.Id);
        }
    }
    
    for (Contract_Line_Item__c Prod : newMap.Values())
    {   
        if(Prod.Product_LU__c != null)
        {      
             Prodid.add(Prod.Product_LU__c);
        }
    }
    
    List<Contract_Line_Item__c> ContractLinesToUpdate = new List<Contract_Line_Item__c>();
    List<Contract_Line_Item__c> ContractLinesToUpdate1 = new List<Contract_Line_Item__c>();
    
    for(PriceBookEntry pbe1 :[Select id, UnitPrice, Pricebook2Id from PriceBookEntry where (Pricebook2id = '01s3000000001JS') and Product2id in :Prodid])
    
    {
                 
           
           for (Contract_Line_Item__c cli :[Select id, List_Price__c from Contract_Line_Item__c where id in :CLIIds])
           {
             ProductLineState = 'Not Found';
             if(pbe1.Pricebook2Id == '01s3000000001JS' && ProductLineState != 'Found' && pbe1.UnitPrice != null)
             {  
           
                cli.List_Price__c = pbe1.UnitPrice;
                ContractLinesToUpdate.add(cli);
                ProductLineState = 'Found'; 
                update(ContractLinesToUpdate);
             }
             }
    for(PriceBookEntry pbe2 :[Select id, UnitPrice, Pricebook2Id from PriceBookEntry where (Pricebook2id = '01s3000000004T2AAI') and Product2id in :Prodid])
    
    {
    for (Contract_Line_Item__c cli :[Select id, List_Price__c from Contract_Line_Item__c where List_Price__c = null and id in :CLIIds])
           {
             if(pbe2.Pricebook2Id == '01s3000000004T2AAI' && cli.List_Price__c == null &&  pbe1.UnitPrice != null)
                  {
                    cli.List_Price__c = pbe2.UnitPrice;
                    ContractLinesToUpdate.add(cli);
                    ProductLineState = 'Found'; 
                     update(ContractLinesToUpdate);
            }      }                 
           }  }
           
         }
        }

 Thank you,

Steve Laycock