+ Start a Discussion
chuk2chuk2 

Field update trigger save error: cannot be assigned

Could a hand, this save error is rather vague and does not give a line number (line 0). Save error: Expression cannot be assigned. I am making a trigger that takes a zip code from a record and fills in the district based of a table of zip codes and districts. 

 

trigger VolunteerBeforeInsert on Volunteer__c (before insert) {

for(Volunteer__c a: Trigger.new) {
if (a.District__c == Null && a.Zip_Code__c != Null) {
string sZip = a.Zip_Code__c;
Zip_Code__c oZip = [SELECT Name, District__c FROM Zip_Code__c WHERE Zip_Code__c.Name = :sZip LIMIT 1].get(0);
Distric__c oDistrict = [SELECT id FROM District__c WHERE District__c.Name = :oZip.District__c LIMIT 1];
volunteer__c.District__c = oDistrict.id;
}
}

}

Best Answer chosen by Admin (Salesforce Developers) 
chuk2chuk2

My co-worker just pointed out my problem -> the last assignment usese volunteer__c.district__c when it should be a.district__c... after that i had one or two other syntax errors due to being a noob, but they got cleared up fast... Thanks for all your help though, it helped me think critically

All Answers

VKrishVKrish

Do you need .get(0) at the end of zip_code__c statement? why cant you use similar to district__c assignment?

Select query returns list of objects though you give limit 1 in it. When you use limit 1 in the select querry, the stmt returns list of objects with just 1 value in it.

So try to assign the select stmt to list of zip_code__c & list of District__c

Then in District__c select stmt, you can  replace oZip.District__c in where clause with oZip[0].District__c

Also try to handle the case where there is no oZip object returned from 1st query. In this case you cannot directly try to use a empty value in 2nd select stmt.

 

chuk2chuk2

That helps make my code better and more robust, but after taking your advice, i still get the cannot assign expression error and i am still not sure how to handle that. 

 

New Code: 

 

trigger VolunteerBeforeInsert on Volunteer__c (before insert) {

for(Volunteer__c a: Trigger.new) {
if (a.District__c == Null && a.Zip_Code__c != Null) { //Only update the records that are missing a district and have a zipcode to work from
 string sZip = a.Zip_Code__c;
Zip_Code__c oZip = [SELECT Name, District__c FROM Zip_Code__c WHERE Zip_Code__c.Name = :sZip LIMIT 1];
if (oZip.count() > 0) {
District__c oDistrict = [SELECT id FROM District__c WHERE District__c.Name = :oZip.District__c LIMIT 1];
volunteer__c.District__c = oDistrict.id;
}
}
}

}

VKrishVKrish
trigger VolunteerBeforeInsert on Volunteer__c (before insert) {

for(Volunteer__c a: Trigger.new) {
List<Zip_Code__c> zZip = new List<Zip_Code__c>();
List<District__c> oDistrict = new List<District__c>();
 if (a.District__c == Null && a.Zip_Code__c != Null) {
//Only update the records that are missing a district and have a zipcode to work from string sZip = a.Zip_Code__c; oZip = [SELECT Name, District__c FROM Zip_Code__c WHERE Zip_Code__c.Name = :sZip LIMIT 1]; if (oZip.size() > 0) { oDistrict = [SELECT id FROM District__c WHERE District__c.Name = :oZip[0].District__c LIMIT 1]; volunteer__c.District__c = oDistrict[0].id; } } } }

 

If the assignment error is not in both the select stmts, then it might be the last line.

Is the District__c is a look up field in volunteer__c? If not you CANNOTassign the District object's Id to it.

 

chuk2chuk2

The reason i did not include the list notation [0] is because when i did, i got an error about the expression having to be a list type SOBJECT:Zip_Code__c and SOBJECT:District__c respectively and District__c is a lookup in Volunteer for the DIstric table, so i am pretty sure the id is correct form. 

chuk2chuk2

My co-worker just pointed out my problem -> the last assignment usese volunteer__c.district__c when it should be a.district__c... after that i had one or two other syntax errors due to being a noob, but they got cleared up fast... Thanks for all your help though, it helped me think critically

This was selected as the best answer