You need to sign in to do that
Don't have an account?
GuanDong
Cannot update Lookup field value with trigger
I'm just trying to update a lookup field value (Campagna_Account__c) on Account object using a trigger that should update it getting the value from a custom object (Sottocampagna__c).
I think the query is right because it runs well on Force.com Explorer, so I think the problem is somewhere else.
Here's the error line displayed when trigger fires:
data changed by trigger for field Campagna: id value of incorrect type: a06250000004FDNAA2
Here's my code:
I think the query is right because it runs well on Force.com Explorer, so I think the problem is somewhere else.
Here's the error line displayed when trigger fires:
data changed by trigger for field Campagna: id value of incorrect type: a06250000004FDNAA2
Here's my code:
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.Id; } }Thank you for helping me!
1. Either a.Campagna_Account__c is expecting Id of Sottocampagna, while you are assigning an Id of Campagna. Please try to query Id field in the query.
2. OR change this statement:
Thanks,
Shailesh.
All Answers
1. Either a.Campagna_Account__c is expecting Id of Sottocampagna, while you are assigning an Id of Campagna. Please try to query Id field in the query.
2. OR change this statement:
Thanks,
Shailesh.
thank you so much, it works with 2.
Would you be so kind to explain WHY it worked? (I'm obviously a newbie...)
One more thing: how can I substitute 'Segugio' with the name of Account object field called Sottocampagna__r.Name?
Shall I need to create a variable somewhere, I suppose.. but is it possible to use it inside the query?
Thank you so much...
Gianni
If it looked upto "SottoCampagna" object you would have to use first solution.
If it looked upto "Campagna" object, you use 2nd solution.
It turned out that the field was looking upto "Campagna" object and hence 2nd solution will work. The issue, with your approach was that you were trying to assign Campagna_Account__c field, the Id of "SottoCampagna" while it was expecting a Id of "Campagna" and hence it threw Invalid Id error.
I am not quite sure I understand what you are asking, but yes, you could use a variable and bind it in the query. Something like below:
Thanks,
Shailesh.