You need to sign in to do that
Don't have an account?
Alejandro 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; } }
Thanks!
All Answers
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
yes, i'm trying to update other object field, what do you mean with do soql in that object?
Thanks!
Make sure that Estado__c field for Oferta__c record is not null means check for the null.
Use below code Hope this will resolve the issue :)
Thanks!
Amit
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 :)
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Rajat Maheshwari :)
Hi Alejandro,
Please let me know : - Is it work ?
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com