You need to sign in to do that
Don't have an account?
Assigning records to a map (and getting them out again properly) in a trigger
I need some help with a simple (me thinks) syntax problem.
I have a before insert trigger on case that takes a number of custom case fields (from web2case) and creates a person account if the customer doesn't exist in the database.
When the person has been created I update the case with the AccountID from the previous stage. Works like a charm.
Now I turn back and requery the Account table based on the AccountId to get at the PersonContactId. (Need to do a requery as this field otherwise is empty).
As far as I can see in the debug logs this step is also working as intended but I probably fumble when assigning the query to a map because extracting the values from the map doesn't work.
Map<Id,Account> AccountContactMap= new Map<Id,Account>(); for (Account acc :[Select Id, PersonContactId from Account where Id IN :accIds]){ AccountContactMap.put(acc.Id, acc); } for (Case caseObj:Trigger.new) { if (caseObj.ContactId==null && !takenSSNumbers.contains(caseObj.SSNumber__c)) Account newAcc = AccountContactMap.get(caseObj.AccountId); caseObj.ContactId = newAcc.PersonContactId; }
The code refuses to save on the caseObj.ContactId assignment row with the following error: Save error: Variable does not exist: newAcc.PersonContactId
When I inspect the debug log I can see that Id and PersonContactId are assigned:
(201026000)|VARIABLE_ASSIGNMENT|[67]|newAcc|{"PersonContactId":"003D000000q9HfIIAU","Id":"001D000000lvhoxIAA"}|0x6ce315e1
Both these AccountId and PersonContactId from the log points to the same Person Account, the newly created one which is what I want. So the data is there, it's just not 'popping out' the way I want....
I have tried both to assign the map directly with the select statement and with this latest try, through the map.put() method. No difference.
Also, the AccIds list contains one row in my test which it should.
What am I doing wrong?
/Mats
Mats,
Its a variable scope thing - in the absence of braces after the If statement, the first statement, which includes the declaration of the Account variable is within the if, and therefore that variable is not visible to the statement after it. Enclose both statements within braces
All Answers
Mats,
Its a variable scope thing - in the absence of braces after the If statement, the first statement, which includes the declaration of the Account variable is within the if, and therefore that variable is not visible to the statement after it. Enclose both statements within braces
Aaaaargh what a noob mistakel
I have been staring at the problem for hours trying different approaches without seeing that. Thank you my friend for your help!
/Mats