You need to sign in to do that
Don't have an account?
Fintan Murphy
Apex NPE error
Hi, My apex code is giving the following error; translationStatusUpdate: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.translationStatusUpdate: line 28, column 1
Here is the code snippet;
In general what is the best way to update the field on a parent object based on a child object? The Translation_Status__c field on Translation__c is a Master-Detail relationship.
Here is the code snippet;
trigger translationStatusUpdate on Translation__c (after insert, after update) { for(Translation__c trans:Trigger.new){ if(trans.Translation_Status__c != null && trans.Case__c != null){ //Case[] transCase = [SELECT Id, Translation_Status__c, CaseNumber FROM Case WHERE Id = :trans.Case__r.Id LIMIT 1]; //String CaseId = transCase.get(Id); if(trans.Translation_Status__c == 'Completed'){ Boolean isComplete = TRUE; Translation__c[] otherTransCases = [SELECT Id, Translation_Status__c FROM Translation__c WHERE Case__c = :trans.Case__c AND Id <> :trans.Id]; for(Translation__c otherTrans:otherTransCases){ if(otherTrans.Translation_Status__c <> 'Completed'){ isComplete = FALSE; break; } } if(isComplete){ //update new Case(Id = trans.Case__r.Id, Translation_Status__c = 'Completed'); trans.Case__r.Translation_Status__c = 'Completed'; } } else{ //update new Case(Id = trans.Case__r.Id, Translation_Status__c = trans.Translation_Status__c); trans.Case__r.Translation_Status__c = trans.Translation_Status__c; } } } }
In general what is the best way to update the field on a parent object based on a child object? The Translation_Status__c field on Translation__c is a Master-Detail relationship.
First of all, you should bulkify your trigger. Currently there is SOQL query inside a loop which can easily cause issues with limits. However, to get the functionality implemented, I would do the following.
On line 2 define the cases that you want to update: Replace lines 18-19 with the following: Replace lines 23-24 with the following: And after line 27, add the following line: However, it is good to know that if several Translation__c records are updated in bulk, it could result in incorrect results (updating same case etc.).
Regards,
Mkr
All Answers
First of all, you should bulkify your trigger. Currently there is SOQL query inside a loop which can easily cause issues with limits. However, to get the functionality implemented, I would do the following.
On line 2 define the cases that you want to update: Replace lines 18-19 with the following: Replace lines 23-24 with the following: And after line 27, add the following line: However, it is good to know that if several Translation__c records are updated in bulk, it could result in incorrect results (updating same case etc.).
Regards,
Mkr
Thank you for your quick reply. I have implemented your changes and also bulkified my trigger. It may not be the most efficient but it does the trick. W.R.T. several records being updated in bulk, you are correct however a bulk update isn't within our workflow and even if we do one the only important thing is that they don't get marked as complete when there is an incomplete translation, and that's not possible within this scheme.
Best,
Fintan.