You need to sign in to do that
Don't have an account?
GuanDong
Include a field value in 'where' query clause
Hello!
Here's my code to update a lookup field value (Campagna_Account__c) on an Account object using a trigger that update it getting the value from a custom object (Sottocampagna__c):
Now my question is: how can I substitute the value 'Segugio' with a variable that pick up the value of a particular field on Account object called Sottocampagna__r.Name?
I'm a newbie, and your answer let me learn tons of things...
Thank you so much
Here's my code to update a lookup field value (Campagna_Account__c) on an Account object using a trigger that update it getting the value from a custom object (Sottocampagna__c):
trigger PopolaCampagna on Account (before insert, before update) { Sottocampagna__c var = [ SELECT Campagna__r.Id FROM Sottocampagna__c WHERE Name='Segugio' ]; for (Account a: Trigger.new) { a.Campagna_Account__c = var.Campagna__r.Id; }
Now my question is: how can I substitute the value 'Segugio' with a variable that pick up the value of a particular field on Account object called Sottocampagna__r.Name?
I'm a newbie, and your answer let me learn tons of things...
Thank you so much
Please use below code and see if it works.
If this works please make sure to mark this answer as solution.
This error appears:
[Error] Error...: Method does not exist or incorrect signature: [List<Account>].keyset() row 10 column 145
Thanks a lot
Since trigger.new return a list of sObject not a map. so first we collect all the Id and then proceed with the remaining code.
If it resolve the query then please mask it as a solution. Please refer https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm for more informattion about trigger context variables.
It seems to work, but with 1 exceptions:
2) Both on existing or new records it works, but I need to save 2 times the record. The first time it doesn't work because the field appears still empty, but then, when I press 'Edit' and then 'Save', trigger does updates the field.
What do you suggest?
Thanks a lot for your precious help.
I believe we need to use the trigger once we have the account saved. in this case we need to use after insert trigger. I hope this works fine and you need not to save a record twice. since you won't get records in
Since records are still not saved and we are firing SOQL on the same.
unluckly it doesn't work. Putting 'after insert' causes the following error message:
execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.PopolaCampagna: line 13, column 1
Thank you so much
P.S.: I've received several mail from you with different solution, I've tried just the one shown here above.