On update of a Parent Object (Recipe Sheet) to be set to "Add to Inventory" I need to add Child Records (Recipe Sheet Items) based on another table in Salesforce tied to the Product table (Product Items), then I need to update the "Add to Inventory" field to False.

I am currently receiving this error: "There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was

Apex trigger CreateRecipeItems2 caused an unexpected exception, contact your administrator: CreateRecipeItems2: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CreateRecipeItems2: line 46, column 1.

Referring to this row:

ritem2.Quantity__c = pilist2.External_Wall_Mount_Vaulted_Ceiling__c*RQuantity;

What am I doing wrong? Here is the code:

trigger CreateRecipeItems2 on Recipe_Sheet__c (after Update) {
    List<Id> recShts = new List<Id>();
    List<Id> recPrds = new List<Id>();

    List<Recipe_Sheet__c> RecipeList = new List<Recipe_Sheet__c>();
    List<Product_Item__c> ProductItemlist = new List<Product_Item__c>();
    List<Product2> ProductList = new List<Product2>();
    List<Recipe_Sheet_Item__c> RecipeItemList = new List<Recipe_Sheet_Item__c>();

    for(Recipe_Sheet__c rec : trigger.new){
        if(rec.Add_To_Inventory__c == true){
            recShts.add(rec.Id); //Stores the Id of the Recipe_Sheet__c in the list
            recPrds.add(rec.Product__c); //Stores the Id of the Product on the Recipe_Sheet__c in the list

    ProductList = [select Id,Name,Do_Not_Discount__c,Record_Type_Id__c 
        from Product2 where Id IN: recPrds];
    ProductItemList = [select Id,Product__c,Quantity__c,Item__c,Length_in_inches__c,
        from Product_Item__c where Product__c IN: recPrds ];
    Recipelist  = [Select Id,Opportunity__c,Product__c,Quantity__c,Add_To_Inventory__c,
        from Recipe_Sheet__c where Id IN: recShts];        

    for(Recipe_Sheet__c rs : Recipelist ){

    Double RQuantity = rs.Quantity__c;

    if (rQuantity == null)
        rQuantity = 0;   

    for(Product_Item__c pilist :ProductItemlist ){
        if(rs.For_Vaulted_Ceiling__c == 'No'&& rs.Wall_Mount_Stem__c == 'No'){
            Recipe_Sheet_Item__c ritem = new Recipe_Sheet_Item__c();
            ritem.Recipe_Sheet__c = Recipelist[0].Id;
            ritem.Item__c = pilist.Item__c;
            ritem.Quantity__c = pilist.Quantity__c*RQuantity ;

    for(Product_Item__c pilist2 :ProductItemlist ){
        if(rs.For_Vaulted_Ceiling__c == 'Yes'|| rs.Wall_Mount_Stem__c == 'Yes'){
            Recipe_Sheet_Item__c ritem2 = new Recipe_Sheet_Item__c();
            ritem2.Recipe_Sheet__c = Recipelist[0].Id;
            ritem2.Item__c = pilist2.Item__c;
            ritem2.Quantity__c = pilist2.External_Wall_Mount_Vaulted_Ceiling__c*RQuantity;

    if (!RecipeItemList.isEmpty()){
        insert RecipeItemList ;

    for(Recipe_Sheet__c RS1 :Recipelist ){

    update Recipelist;

2 Answers 2


You are trying to multiple a null value by something:

ritem2.Quantity__c = pilist2.External_Wall_Mount_Vaulted_Ceiling__c*RQuantity;

Either pilist2.External_Wall_Mount_Vaulted_Ceiling__c or RQuantity is null. Make sure they have a value before multiplying.

You said RQuantity is null. Add in a check:

if (rQuantity == null)
  rQuantity = 0;

EDIT: Also the following(this doesn't really hurt anything besides adding unnecessary lines):

Double Rquantity;
RQuantity = rec.quantity__c;

Can be changed to:

DoubRQuantity = rec.Quantity__c;

There is no reason to use 2 lines for it.

  • For some reason it thinks RQuantity does not have a value because if I remove that piece I do not get this error. Am I not passing the value into this variable correctly? As the Recipe Sheet Quantity is populated on my record.
    – Grace
    Jul 8, 2014 at 18:08
  • rec.quantity__c probably doesn't have a value. Use a debug here to confirm. If it doesn't have a value, assign it to 0.
    – dphil
    Jul 8, 2014 at 18:09
  • Edited my answer.
    – dphil
    Jul 8, 2014 at 18:14
  • How do I assign it to 0?
    – Grace
    Jul 8, 2014 at 18:32
  • I show how in my answer.
    – dphil
    Jul 8, 2014 at 19:31

Check to see if both fields used in the assignment have a value.

  • I must not be assigning the Recipe_Sheet__c.Quantity__c correctly because if I remove Rquantity I do not get an error. Any thoughts here?
    – Grace
    Jul 8, 2014 at 18:09
  • You could set a default value of 0 on that field or mark is as required or check if it has a value before performing the calculation.
    – Jenny B
    Jul 8, 2014 at 18:13
  • How do I seet the default value? Also the record I am testing with absolutely has a value in the quantity field as it set to required so I am not sure why it wouldn't be pulling it through.
    – Grace
    Jul 8, 2014 at 18:51
  • Could be the other field, have you done a system.Debug on both fields to the right of the assignment?
    – Jenny B
    Jul 8, 2014 at 19:01
  • I am fairly new at apex. What is the best way to run a system.Debug?
    – Grace
    Jul 8, 2014 at 19:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .