You need to sign in to do that
Don't have an account?

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
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
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
(Sales Price * Quantity)
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
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.
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
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
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;
}