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
Sudhir_MeruSudhir_Meru 

Not able to Update Amount field in OpportunityLineItem using Trigger

Hi, 

  There is a custom object with name NSP__c When every object is either inserted or updated with values. I am firing a trigger to update 
Amount field in OpportunityLineItem Problem is value is not getting updated. I am not sure what is the issue. 

I tried creating a test field in opportunity to test this value is getting updated. Please suggest me how to fix this issue. 

trigger NSPActiveAmount on NSP__c (after insert,after update)
{
  List <Id> OpportunityIds = new List<Id>();

    for(NSP__c N : trigger.new)
    {
      OpportunityIds.add(N.Opportunity__c);
    }

  List <Opportunity> opiRecords = [SELECT test__c FROM Opportunity
                                             WHERE Id = :OpportunityIds];
 
   List <OpportunityLineItem> oliRecords = [SELECT UnitPrice FROM OpportunityLineItem
                                             WHERE OpportunityId = :OpportunityIds];
                                            
    List <NSP_Details__c>  NSPDetails = [ SELECT  Distributor_Requested_Unit_Price__c
                                          FROM NSP_Details__c
                                          WHERE NSP__c in ( SELECT ID FROM NSP__c
                                                            WHERE opportunity__c  = :OpportunityIds) ];
  
    for (Opportunity opp :opiRecords)
     {    
      for (OpportunityLineItem oppItem :oliRecords )
       { 
       for (NSP_Details__c NSP_Details :NSPDetails)
        {       
        opp.test__c = NSP_Details.Distributor_Requested_Unit_Price__c;
        oppItem.UnitPrice =  NSP_Details.Distributor_Requested_Unit_Price__c;                    
        }
      } 
   } 
  Update  opiRecords;
  Update oliRecords;                          
}

Thanks

Sudhir

Best Answer chosen by Sudhir_Meru
Sudhir_MeruSudhir_Meru
Hi Souvik, 

  Thanks for you reply I tried your method but still this is not working in my trigger to update. Please suggest

trigger NSPActiveAmount on NSP__c (after insert,after update)
{
  List <Id> OpportunityIds = new List<Id>();
  List <NSP__c> NSPnewrecords = new List<NSP__c>();
  List <NSP_Details__c> NSPDetails = new List<NSP_Details__c>();

    for(NSP__c N : trigger.new)
    {
      OpportunityIds.add(N.Opportunity__c);
    }

   List <Opportunity> opiRecords = [SELECT test__c FROM Opportunity
                                             WHERE Id = :OpportunityIds];
 
   List <OpportunityLineItem> oliRecords = [SELECT UnitPrice FROM OpportunityLineItem
                                             WHERE OpportunityId = :OpportunityIds];
                                            
/*  List <NSP__c> NSP =  [ SELECT Total_Net_Amount_To_Meru__c
                          FROM NSP__c
                          WHERE opportunity__c  = :OpportunityIds]; */
                         
     NSPDetails = [ SELECT Id, Related_Opportunity_Product__c,
                    Distributor_Requested_Unit_Price__c, Product_Name__c
                    FROM NSP_Details__c
                    WHERE NSP__c in ( SELECT ID FROM NSP__c WHERE opportunity__c  = :OpportunityIds) ];
  
    for (Opportunity opp :opiRecords)
     {  
      for (OpportunityLineItem oppItem :oliRecords )
       {
       for (NSP_Details__c NSP_Details :NSPDetails)
        {     
        opp.test__c = NSP_Details.Distributor_Requested_Unit_Price__c;
        if(oppItem.UnitPrice != NULL) {
        oppItem.UnitPrice =  NSP_Details.Distributor_Requested_Unit_Price__c;  
        }
        if(oppItem.TotalPrice != NULL) {
        oppItem.TotalPrice =  NSP_Details.Distributor_Requested_Unit_Price__c; 
        }              
        }
      }
   }
  Update  opiRecords;
  Update oliRecords;                                            
                                            
                    
}

All Answers

Mariappan PerumalMariappan Perumal
Hi ,

How did you make sure the 3 list are not null . It looks little complicated because of 3 level of nested for loop statements.

Try to use System.debug statements inside of every loop statements.

Instead of making iterated over each opportunity with every opportunity line item . Make opportunity and opportunitylineitem as inner relational query to make opportunity with the opportunity line item just with the particular opportunity [avoid some unwanted iteration ].

Thanks
Mariappan Perumal

vishal@forcevishal@force
Your code seems fine. Are you sure the field " NSP_Details.Distributor_Requested_Unit_Price__c" has some value?
souvik9086souvik9086
Amount field in Opportunity object is not updatable. It is automatically populated by the summation of all the opportunitylineitems 
(Sales Price * Quantity)
Sudhir_MeruSudhir_Meru

Hi All Thanks for your reply 

  I am not updating Amount field in Opportunity. If you see my code I am updating the Unit Price in OpportunityLineItem which will reflect the changes in Amount field in Opportunity. 

 Please suggest me how to do this.

Thanks

Sudhir

souvik9086souvik9086
Please make sure that totalprice field is not populated. Here is the quote from salesforce about this field

The unit price for the opportunity line item. In the Salesforce user interface, this field’s value is calculated by dividing the total price of the opportunity line item by the quantity listed for that line item. Label is Sales Price.
This field or TotalPrice is required. You can’t specify both.
If you specify Discount and Quantity, this field or TotalPrice is required.
Sudhir_MeruSudhir_Meru

Do you mean I have to update even the TotalPrice?  What would be the best way to update Please advice

  I need to update opportunity amount when custom object NSP_Details is updated 

Thanks

Sudhir

souvik9086souvik9086
See here it is mentioned that you can have value in one of the field among the two. Not both.
So you have to check whether the field is not NULL, then which field is not null, you populate that. LIKE

for (Opportunity opp :opiRecords)
     {   
      for (OpportunityLineItem oppItem :oliRecords )
       {
       for (NSP_Details__c NSP_Details :NSPDetails)
        {      
        opp.test__c = NSP_Details.Distributor_Requested_Unit_Price__c;
        if(oppItem.UnitPrice != NULL) {
        oppItem.UnitPrice =  NSP_Details.Distributor_Requested_Unit_Price__c;   
        }
        if(oppItem.TotalPrice != NULL) {
        oppItem.TotalPrice =  NSP_Details.Distributor_Requested_Unit_Price__c;  
        }               
        }
      }
   }

Also fetch TotalPrice in the above query of OppLineItem
Sudhir_MeruSudhir_Meru
Hi Souvik, 

  Thanks for you reply I tried your method but still this is not working in my trigger to update. Please suggest

trigger NSPActiveAmount on NSP__c (after insert,after update)
{
  List <Id> OpportunityIds = new List<Id>();
  List <NSP__c> NSPnewrecords = new List<NSP__c>();
  List <NSP_Details__c> NSPDetails = new List<NSP_Details__c>();

    for(NSP__c N : trigger.new)
    {
      OpportunityIds.add(N.Opportunity__c);
    }

   List <Opportunity> opiRecords = [SELECT test__c FROM Opportunity
                                             WHERE Id = :OpportunityIds];
 
   List <OpportunityLineItem> oliRecords = [SELECT UnitPrice FROM OpportunityLineItem
                                             WHERE OpportunityId = :OpportunityIds];
                                            
/*  List <NSP__c> NSP =  [ SELECT Total_Net_Amount_To_Meru__c
                          FROM NSP__c
                          WHERE opportunity__c  = :OpportunityIds]; */
                         
     NSPDetails = [ SELECT Id, Related_Opportunity_Product__c,
                    Distributor_Requested_Unit_Price__c, Product_Name__c
                    FROM NSP_Details__c
                    WHERE NSP__c in ( SELECT ID FROM NSP__c WHERE opportunity__c  = :OpportunityIds) ];
  
    for (Opportunity opp :opiRecords)
     {  
      for (OpportunityLineItem oppItem :oliRecords )
       {
       for (NSP_Details__c NSP_Details :NSPDetails)
        {     
        opp.test__c = NSP_Details.Distributor_Requested_Unit_Price__c;
        if(oppItem.UnitPrice != NULL) {
        oppItem.UnitPrice =  NSP_Details.Distributor_Requested_Unit_Price__c;  
        }
        if(oppItem.TotalPrice != NULL) {
        oppItem.TotalPrice =  NSP_Details.Distributor_Requested_Unit_Price__c; 
        }              
        }
      }
   }
  Update  opiRecords;
  Update oliRecords;                                            
                                            
                    
}
This was selected as the best answer