+ Start a Discussion
RelaxItsJustCodeRelaxItsJustCode 

Will give major kudos for help. This is probably something simple for someone more exp with apex

I have a custom object called "Contract_Line_Item__c" when someone enters in a product id I need to look it up and pull the UnitPrice from pricebookentry with

pricebook2id that is a constant and the product2id entered into the object by the user before save.  My code is just choking on my...  Having a hell of a day with brain overload...  Anyway I will give kudos to anyone who can probably solve this easily...  Here is the code.....

 

public with sharing class MaintContractLineItemCreation 
{
   public static void CreateContractLine(Map<Id,Contract_Line_Item__c> oldMap, Map<Id,Contract_Line_Item__c> newMap)
   {
    List<Contract_Line_Item__c> ContractLineItemsToUpdate = new List<Contract_Line_Item__c>();
    List<Id> CLIIds = new List<Id>();

    For(Contract_Line_Item__c cli : newMap.Values())
    {
        CLIIds.add(cli.id);
    }
        
    
    List<Contract_Line_Item__c> LineItemsToUpdate = new List<Contract_Line_Item__c>();

    
    for(Contract_Line_Item__c li :[SELECT ID, (SELECT UnitPrice FROM PricebookEntries where pricebook2id = '01s3000000001JS'), 
                (SELECT Product_LU__c, List_Price__c FROM Contract_Line_Items__r where id in :CLIIds) FROM Product2])

    {
        
            LineItemsToUpdate.List_Price__c =  li.UnitPrice;

            LineItemsToUpdate.add(li);
                
    }
    Update(LineItemsToUpdate);
   }
}

 Thanks again,

Steve

Best Answer chosen by Admin (Salesforce Developers) 
RelaxItsJustCodeRelaxItsJustCode

Souvik9086, That you for trying to help but I got this now..  I appriciate the fact you were willing to help.  Had a brain fart kind of day.

 

public with sharing class MaintContractLineItemCreation 
{    
    
    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>();
    
    for(PriceBookEntry pbe1 :[Select id, UnitPrice 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])
       {
            cli.List_Price__c = pbe1.UnitPrice;
            ContractLinesToUpdate.add(cli);
       }
    update(ContractLinesToUpdate);    
    }
}}

 

All Answers

souvik9086souvik9086

Can you explain the relationship between Pricebookentry and ContractLineitrems?

 

for(Product2 li :[SELECT ID, (SELECT UnitPrice FROM PricebookEntries where pricebook2id = '01s3000000001JS'),
(SELECT Product_LU__c, List_Price__c FROM Contract_Line_Items__r where id in :CLIIds) FROM Product2])

{
//You have to assign the unitprice to listprice but for whom to whom? Have to understand that relation
//LineItemsToUpdate.List_Price__c = li.UnitPrice;

//LineItemsToUpdate.add(li);

}

 

Thanks

Jake GmerekJake Gmerek

Ok you are all over the place here.  A few questions:

 

1.  How is this list:

 

List<Contract_Line_Item__c> ContractLineItemsToUpdate = new List<Contract_Line_Item__c>();

 

different than this list:

 

List<Contract_Line_Item__c> LineItemsToUpdate = new List<Contract_Line_Item__c>();

 

2. This line makes no sense:

 

for(Contract_Line_Item__c li :[SELECT ID, (SELECT UnitPrice FROM PricebookEntries where pricebook2id = '01s3000000001JS'),
(SELECT Product_LU__c, List_Price__c FROM Contract_Line_Items__r where id in :CLIIds) FROM Product2])

 

You are trying to query a list of Product2 objects and iterate through them as Contract_Line_Item__c objects.  What are you really trying to do here?

 

3.  This line should not even complie:

 

LineItemsToUpdate.List_Price__c =  li.UnitPrice;

 

LineItemsToUpdate is a list and therefore there is no List_Price__c field for you to assign, also there is the problem with the li variable stated above and depending on your answer there we may have to change something.  No question here, just need to understand what you are trying to do.

 

4. From what I understand you are trying to update your object, Contract_Line_Item__c, before the record is saved.  I am assuming that this is getting called by a before insert, before update trigger which means that you should not need this line either:

 

Update(LineItemsToUpdate);

 

But it is hard to tell because your query is messed up.

 

5.  Finally you have hardcoded a pricebookentry id in your subquery which tells me that every List price that you save is going to be the same value, is this correct?

 

Give me a little more information and I will help you solve your issue.

 

RelaxItsJustCodeRelaxItsJustCode

Souvik9086, That you for trying to help but I got this now..  I appriciate the fact you were willing to help.  Had a brain fart kind of day.

 

public with sharing class MaintContractLineItemCreation 
{    
    
    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>();
    
    for(PriceBookEntry pbe1 :[Select id, UnitPrice 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])
       {
            cli.List_Price__c = pbe1.UnitPrice;
            ContractLinesToUpdate.add(cli);
       }
    update(ContractLinesToUpdate);    
    }
}}

 

This was selected as the best answer
bob_buzzardbob_buzzard

Should your question be changed to "will give major kudos for solution"?  Seems like you received some help, even though you figured out the solution yourself, but I can't see any kudos awarded.