You need to sign in to do that
Don't have an account?
nitin sharma 366
Error:Null Pointer Exception so please suggests
Hi All,
Objects which are a part of my trigger.
Opportunity,Opportuity line Item, Price book entry and Product object
I have a custom field cost_price__c on the product object and quantity on the opportunity line item....I am simply multiplying quantity field with with cost_price__c which is on the product table and updating a field on the Opportunity obect known as Total_price_of_all_the_products__c.
However,I am getting an error
caused by: System.NullPointerException: Attempt to de-reference a null object
Below given line is causing an error.Sombody please help
Opportunity abc=m.get(opportunitylineitemRecords.opportunityid);
trigger UpdateOppWithPriceAndQty on OpportunityLineItem (After Insert)
{
Opportunity opp;
set<id>Ids=new set<id>();
set<id>Prodid=new set<id>();
Map<id,opportunitylineitem>oppMap=new Map<id,opportunitylineitem>();
for(opportunitylineitem f:trigger.new)
{
oppmap.put(f.opportunityid,null);
prodid.add(f.product2id);
}
Map<id,opportunity>m=new map<id,opportunity>([Select id,Total_price_of_all_the_products__c from opportunity where id in:oppmap.keyset()]);
list<pricebookentry>ProdOpplineItemRec=[Select id,product2id,Product2.Cost_price__c,(select opportunityid,quantity from opportunitylineitems) from pricebookentry where product2id in:prodid];
list<opportunity>updateOpp=new List<opportunity>();
for(pricebookentry pri:ProdOpplineItemRec)
{
for(opportunitylineitem opportunitylineitemRecords :pri.opportunitylineitems)
{
if(pri.product2.cost_price__c!=null)
{
Opportunity abc=m.get(opportunitylineitemRecords.opportunityid);
abc.Total_price_of_all_the_products__c+=(pri.product2.cost_price__c*opportunitylineitemRecords.Quantity);
//system.debug('record has the following values'+abc.Total_price_of_all_the_products__c);
//abc.Total_price_of_all_the_products__c=(pri.product2.cost_price__c*opportunitylineitemRecords.Quantity);
//updateOpp.add(abc);
}
}
//updateOpp.add(abc);
}
if(updateOpp.size()>0)
{
try
{
//update updateOpp;
}
Catch(DmlException de)
{
System.debug('Exception has been raised as error message has been created');
}
}
}
Objects which are a part of my trigger.
Opportunity,Opportuity line Item, Price book entry and Product object
I have a custom field cost_price__c on the product object and quantity on the opportunity line item....I am simply multiplying quantity field with with cost_price__c which is on the product table and updating a field on the Opportunity obect known as Total_price_of_all_the_products__c.
However,I am getting an error
caused by: System.NullPointerException: Attempt to de-reference a null object
Below given line is causing an error.Sombody please help
Opportunity abc=m.get(opportunitylineitemRecords.opportunityid);
trigger UpdateOppWithPriceAndQty on OpportunityLineItem (After Insert)
{
Opportunity opp;
set<id>Ids=new set<id>();
set<id>Prodid=new set<id>();
Map<id,opportunitylineitem>oppMap=new Map<id,opportunitylineitem>();
for(opportunitylineitem f:trigger.new)
{
oppmap.put(f.opportunityid,null);
prodid.add(f.product2id);
}
Map<id,opportunity>m=new map<id,opportunity>([Select id,Total_price_of_all_the_products__c from opportunity where id in:oppmap.keyset()]);
list<pricebookentry>ProdOpplineItemRec=[Select id,product2id,Product2.Cost_price__c,(select opportunityid,quantity from opportunitylineitems) from pricebookentry where product2id in:prodid];
list<opportunity>updateOpp=new List<opportunity>();
for(pricebookentry pri:ProdOpplineItemRec)
{
for(opportunitylineitem opportunitylineitemRecords :pri.opportunitylineitems)
{
if(pri.product2.cost_price__c!=null)
{
Opportunity abc=m.get(opportunitylineitemRecords.opportunityid);
abc.Total_price_of_all_the_products__c+=(pri.product2.cost_price__c*opportunitylineitemRecords.Quantity);
//system.debug('record has the following values'+abc.Total_price_of_all_the_products__c);
//abc.Total_price_of_all_the_products__c=(pri.product2.cost_price__c*opportunitylineitemRecords.Quantity);
//updateOpp.add(abc);
}
}
//updateOpp.add(abc);
}
if(updateOpp.size()>0)
{
try
{
//update updateOpp;
}
Catch(DmlException de)
{
System.debug('Exception has been raised as error message has been created');
}
}
}
If we consider your query, your result set could look like this
Basically, if you have a pricebook entry for the product and that product is not in Opportunity Line items, the the Oppty Line items will be empty.
So when you do the line
Opportunity abc=m.get(opportunitylineitemRecords.opportunityid);
the element opportunitylineitemRecords.opportunityid is null / empty, hence the error
Do a IsNull test before calling the line and see how you go.
Regards
Andrew
I tried but still the same erorr...Not 100 percent sure if I did asking what you were asking.
I have data in the
trigger UpdateOppWithPriceAndQty on OpportunityLineItem (After Insert)
{
Opportunity opp;
Opportunity abc;
set<id>Ids=new set<id>();
set<id>Prodid=new set<id>();
Map<id,opportunitylineitem>oppMap=new Map<id,opportunitylineitem>();
for(opportunitylineitem f:trigger.new)
{
oppmap.put(f.opportunityid,null);
prodid.add(f.product2id);
}
Map<id,opportunity>m=new map<id,opportunity>([Select id,Total_price_of_all_the_products__c from opportunity where id in:oppmap.keyset()]);
list<pricebookentry>ProdOpplineItemRec=[Select id,product2id,Product2.Cost_price__c,(select opportunityid,quantity from opportunitylineitems) from pricebookentry where product2id in:prodid];
list<opportunity>updateOpp=new List<opportunity>();
for(pricebookentry pri:ProdOpplineItemRec)
{
for(opportunitylineitem opportunitylineitemRecords :pri.opportunitylineitems)
{
if(pri.product2.cost_price__c!=null)
{
if(opportunitylineitemRecords!=null)
{
abc=m.get(opportunitylineitemRecords.opportunityid);
system.debug('The value in the abc is'+abc);
}
if(abc.Total_price_of_all_the_products__c!=null)
{
abc.Total_price_of_all_the_products__c+=(pri.product2.cost_price__c*opportunitylineitemRecords.Quantity);
}
//abc.Total_price_of_all_the_products__c+=(pri.product2.cost_price__c*opportunitylineitemRecords.Quantity);
//system.debug('record has the following values'+abc.Total_price_of_all_the_products__c);
//abc.Total_price_of_all_the_products__c=(pri.product2.cost_price__c*opportunitylineitemRecords.Quantity);
//updateOpp.add(abc);
}
}
//updateOpp.add(abc);
}
if(updateOpp.size()>0)
{
try
{
//update updateOpp;
}
Catch(DmlException de)
{
System.debug('Exception has been raised as error message has been created');
}
}
}
Regards
Andrew
P.S. when posting code, its easier to read if you use the code snippet/sample tool