I have a simple Before Update Trigger that I want to run when anything on the Accounting_Instructions__c
is updated. I am not sure why my trigger is not working. I either receive a NullPointerException
or the field doesn't update.
I have tried running it as an after trigger and doing DML into a new instance of the object. I have tried looping through a list of the objects to be updated, I have tried to have a nested loop over the Case object (the Status field on Case is what I am trying to update). I have tried a simple field update in a single for loop but get NullPointerException
. I know that trigger.new is not null, I have system.debugged it and have been returned values.
This is my first trigger so I am sure it is a fundamental oversight but any advice is welcome :)
//Requirements: Pull all Account Instruction records, related to the Case I am on, that have a Case status of "Sent To Accounting"
//Pull all Account Instruction records, related to the Case I am on, that have "Accounting Complete" field checked
//Update Status on current Case to 'Complete'
//run this trigger when Accounting Instructions field "Accounting Complete" is updated
trigger CompletedAccountingInstructions on Accounting_Instructions__c (before update) {
for(Accounting_Instructions__c aic : Trigger.new) {
//aic.Case__r.Status = 'Open'; this seems like the easiest path but I receive a Null Pointer Exception when I do it this way
}
Accounting_Instructions__c[] instructions = [SELECT Id, Accounting_Complete__c, Case__r.Status FROM Accounting_Instructions__c WHERE Case__r.Status = 'Sent To Accounting' AND Accounting_Complete__c = TRUE AND Id IN :Trigger.new];
for(Accounting_Instructions__c aic : instructions){
aic.Case__r.Status = 'Open';
}
}
The only thing that I found strange was when I system.debugged the trigger.new or the aic variable, I was returned:
trigger.new:
(Accounting_Instructions__c:{Id=a1Nc0000002sssssI, OwnerId=0053400sssssQnEtAAK, IsDeleted=false, Name=ACTG-00014, CreatedDate=2017-04-28 19:19:51, CreatedById=00534000009QnsssK, LastModifiedDate=2017-05-01 15:31:58, LastModifiedById=005340000ssssAAK, SystemModstamp=2017-05-01 15:31:58, LastViewedDate=null, LastReferencedDate=null, Case__c=500c000sssssAsAAK, Action__c=Do not rerun;Rerun;Rerun w/ fee, Rerun_ACH_Date__c=2017-04-19 00:00:00, Resume_ACH_Date__c=2017-04-20 00:00:00, Other_Comments__c=Test Comment 2, Amount__c=1.00, Send_to_Accounting__c=false, Accounting_Complete__c=true, Loan_Number__c=null, Legal_Name__c=null, Date_of_Bounce__c=null, Debit_Amount__c=0.00, Returned_Payment_Type__c=null, Duplicate_Comments__c=null, Payment_Type__c=null, **Case_Status__c=Sent to Accounting})**
Which has the field **Case_Status__c**
on the object. This is different then **Case__r.Status.**
Could this be the issue? I have tried to change the field name but I am rejected as it is not a field on Accounting_Instructions__c.
UPDATES:
With Adrian's update:
public with sharing class AccountingInstructionsService
{
public static void openParentCases(List<Accounting_Instructions__c> records)
{
Map<Id, Case> parentCases = new Map<Id, Case>();
for (Accounting_Instructions__c record : records)
{
if(record.Accounting_Complete__c == TRUE) {
if(record.Case__r.Status == 'Sent to Accounting') {
parentCases.put(record.Case__c, new Case(Id = record.Case__c, Status = 'New'));
}
}
}
parentCases.remove(null);
List<Case> caseList = parentCases.values();
update caseList;
}
}
UPDATE 2:
I took what Adrian gave me and added a little more to be able to search for all of the Accounting_Instructions__c's on the Case. See below result:
public with sharing class AccountingInstructionsService
{
public static void openParentCases(List<Accounting_Instructions__c> records)
{
Id caseID;
Integer i = 0;
list<Accounting_Instructions__c> aic = [SELECT Accounting_Complete__c, Case__c FROM Accounting_Instructions__c WHERE Id IN :records];
for(Accounting_Instructions__c a : aic){
caseID = a.Case__c;
}
List<Accounting_Instructions__c> listOfAccountInstrRelatedToCase = [SELECT Id, Accounting_Complete__c, Case__c FROM Accounting_Instructions__c WHERE Case__r.Id = :caseID];
List<Case> parentCases;
Set<Id> caseIds = new Set<Id>();
for (Accounting_Instructions__c record : listOfAccountInstrRelatedToCase) {
if(record.Accounting_Complete__c == TRUE){
caseIds.add(record.Case__c);
i = i + 1;
}
else{
i = i - 1;
}
}
if(i == listOfAccountInstrRelatedToCase.size()) {
parentCases = [SELECT Id, Status FROM Case WHERE Id IN :caseIds AND Status = 'Sent to Accounting'];
if(!parentCases.isEmpty()) {
for (Case parentCase : parentCases) {
parentCase.Status = 'Completed';
}
}
update parentCases;
}
}
}