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
Alejandro Garcia LopezAlejandro Garcia Lopez 

help with my trigger: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger

it works when i update a field in the same object, but when i try to update other field with relationship from other object it says that is a null object, i don't understand that, this is my trigger
trigger VeEstado on Oferta__c (before update) {    
    for (Oferta__c c:Trigger.new)
    {
    integer i=[select count() from oferta__c where FechaCobro__c != null AND Ciudad_busqueda__c =:c.id];
    c.Estado__r.vendidas__c=i;
    integer d=[SELECT count() FROM Oferta__c WHERE TiempoOferta__c >= -1500 AND PrecioVenta__c <= 5000000 
               AND Estatus__c != 'Descartada' AND FechaCobro__c = Null AND FechaPago__c != Null 
               AND ((Etapa__c IN ('Compilación expediente compra','Compra final',
                                  'Escrituras en proceso registro','Compilación expediente cliente')) 
                                OR (Etapa__c IN ('Rehabilitación') AND Invadida__c = true))
                                    AND Ciudad_busqueda__c =:c.id ];
    c.Estado__r.Propiedades_disponibles__c=d;
    }
}

 
Best Answer chosen by Alejandro Garcia Lopez
Amit Singh 1Amit Singh 1
You need to make SOQL qury in Estado__C object and then can update the filed.
Thanks!

All Answers

Amit Singh 1Amit Singh 1
Hello,

Error is due to that you are trying to uodate another "Estado__C" obect field and if I am not it may be lookup/master field if you want to uodate field of Estado__C object then you need to do SOQL on this then you can update the record.

Also, you have used SOQL inside for loop which is not best practise, you can use collection Map, List and set for best practise.
https://developer.salesforce.com/page/Apex_Code_Best_Practices
https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
Let me know if this helps:)
Thanks!
AMit Singh
Alejandro Garcia LopezAlejandro Garcia Lopez
hello, 
yes, i'm trying to update other object field, what do you mean with do soql in that object?
Amit Singh 1Amit Singh 1
You need to make SOQL qury in Estado__C object and then can update the filed.
Thanks!
This was selected as the best answer
Alejandro Garcia LopezAlejandro Garcia Lopez
but, why? can you help me with an example of that? that would be great
Amit Singh 1Amit Singh 1
Ok.

Make sure that Estado__c field for Oferta__c record is not null means check for the null.

Use below code
trigger VeEstado on Oferta__c (before update) {    
    for (Oferta__c c:Trigger.new)
    {
    integer i=[select count() from oferta__c where FechaCobro__c != null AND Ciudad_busqueda__c =:c.id];
    If(Estado__c!=null)c.Estado__r.vendidas__c=i;
    integer d=[SELECT count() FROM Oferta__c WHERE TiempoOferta__c >= -1500 AND PrecioVenta__c <= 5000000 
               AND Estatus__c != 'Descartada' AND FechaCobro__c = Null AND FechaPago__c != Null 
               AND ((Etapa__c IN ('Compilación expediente compra','Compra final',
                                  'Escrituras en proceso registro','Compilación expediente cliente')) 
                                OR (Etapa__c IN ('Rehabilitación') AND Invadida__c = true))
                                    AND Ciudad_busqueda__c =:c.id ];
    If(Estado__c!=null)c.Estado__r.Propiedades_disponibles__c=d;
    }
}
Hope this will resolve the issue :)
Thanks!
Amit
 
Alejandro Garcia LopezAlejandro Garcia Lopez
Estado__c field for Oferta__c record is not null, i tried (c.Estado__c!=null)c.Estado__r.vendidas__c=i; but the same error appear in the same line: Attempt to de-reference a null object Trigger.VeEstado: line 5, column 1: []
Amit Singh 1Amit Singh 1
Then you need to make SOQL query Estado__c like below
Select Id,Propiedades_disponibles__c,vendidas__c FROM Estado__c Where 'your relationship condition here'
Thanks!
 
Alejandro Garcia LopezAlejandro Garcia Lopez
i need to save that query in a list? then update the list or how could i update it? thanks for your help :)
rajat Maheshwari 6rajat Maheshwari 6

Hi Alejandro,

As I see your code, the culprit is "c.Estado__r.vendidas__c", when you see in debug log, It will show null value, 

To tackle such situation, @Amit have suggested the right approach. You need to make query on Estado__c 

Below is code snippet, please use this and let me know if it works :)

trigger VeEstado on Oferta__c (before update) {   

Set<Id> setOfertaId = new Set<Id>();
Set<Id> SetEstadoId = new Set<Id>();

    for (Oferta__c c:Trigger.new)
    {
       setOfertaId.add(c.id);
       if(c.Estado__c!=null)
            SetEstadoId.add(c.Estado__c);
    }


    integer i=[select count() from oferta__c where FechaCobro__c != null AND Ciudad_busqueda__c IN :setOfertaId];

 integer d=[SELECT count() FROM Oferta__c WHERE TiempoOferta__c >= -1500 AND PrecioVenta__c <= 5000000 
               AND Estatus__c != 'Descartada' AND FechaCobro__c = Null AND FechaPago__c != Null 
               AND ((Etapa__c IN ('Compilación expediente compra','Compra final',
                                  'Escrituras en proceso registro','Compilación expediente cliente')) 
                                OR (Etapa__c IN ('Rehabilitación') AND Invadida__c = true))
                                    AND Ciudad_busqueda__c IN:setOfertaId ];

Map<Id,Estado__c> mpEstado = new Map<Id,Estado__c>([Select Id,vendidas__c,Propiedades_disponibles__c from Estado__c where Id IN:SetEstadoId]);

for(Oferta__c c : Trigger.new)
  {
     if(mpEstado!=null && mpEstado.containsKey(c.Estado__c))
       {
          mpEstado.get(c.Estado__c).vendidas__c = i;
         mpEstado.get(c.Estado__c).Propiedades_disponibles__c= d;
          
        }
   }

if(mpEstado!=null && mpEstado.value()!=null)
    update mpEstado.value();

}

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Alejandro Garcia LopezAlejandro Garcia Lopez
Thanks
Rajat Maheshwari :)
rajat Maheshwari 6rajat Maheshwari 6

Hi Alejandro,

Please let me know : - Is it work ?

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

Alejandro Garcia LopezAlejandro Garcia Lopez
hi Rajat, it works with a query in Estados__c you were right :)
trigger VeEstado on Oferta__c (after update) {  

    for (Oferta__c c:Trigger.new)
    {
        if (c.Estado__c!=null){
integer i=[select count() from oferta__c where FechaCobro__c != null AND Estado__c =:c.Estado__c];
Estados__c l=[select id, vendidas__c, Propiedades_Disponibles__c from Estados__c where id=:c.Estado__c];
l.vendidas__c=i;
 
integer d=[SELECT count() FROM Oferta__c WHERE TiempoOferta__c >= -1500 AND PrecioVenta__c <= 5000000 
               AND Estatus__c != 'Descartada' AND FechaCobro__c = Null AND FechaPago__c != Null 
               AND ((Etapa__c IN ('Compilación expediente compra','Compra final',
                                  'Escrituras en proceso registro','Compilación expediente cliente')) 
                                OR (Etapa__c IN ('Rehabilitación') AND Invadida__c = true))
                                    AND Estado__c =:c.Estado__c ];
l.Propiedades_Disponibles__c=d;       
update l; 
    }
}
}