function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
GuanDongGuanDong 

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:
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!

 
Best Answer chosen by GuanDong
Shailesh DeshpandeShailesh Deshpande
There are 2 things here:

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.
Sottocampagna__c var = [
SELECT Id, Campagna__r.Id
FROM Sottocampagna__c
WHERE Name='Segugio'

];

2. OR change this statement:
 
for (Account a: Trigger.new) {
//a.Campagna_Account__c = var.Id;
a.Campagna_Account__c = var.Campagna__r.Id;
   }

Thanks,
Shailesh.

All Answers

Shailesh DeshpandeShailesh Deshpande
There are 2 things here:

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.
Sottocampagna__c var = [
SELECT Id, Campagna__r.Id
FROM Sottocampagna__c
WHERE Name='Segugio'

];

2. OR change this statement:
 
for (Account a: Trigger.new) {
//a.Campagna_Account__c = var.Id;
a.Campagna_Account__c = var.Campagna__r.Id;
   }

Thanks,
Shailesh.
This was selected as the best answer
GuanDongGuanDong
Mr. Deshpande,

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

 
Shailesh DeshpandeShailesh Deshpande
Campagna_Account__c on account is lookup field, but i was not sure which object it was looking upto and hence i gave 2 answers assuming it may be looking upto either "SottoCampagna" object or "Campagna" object.

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:
 
String accName = 'Segugio';

Sottocampagna__c var = [
SELECT Id, Campagna__r.Id
FROM Sottocampagna__c
WHERE Name=:accName

];



Thanks,
Shailesh.