I'm trying to assign a string result from a query into a trigger's string variable, and this is the error I receive:

Compile Error: Illegal assignment from LIST to String at line 42 column 12

string SEMCODE;

// Most Recent SEM Code
SEMCODE = [select SEM_Code__c from Interaction__c where Contact__c = :record.Contact__c
           ORDER BY Start_Date_Time__c Desc Limit 1];

How can I get the SEM_Code__c value from the single record query result?




The reason for the error message was that you were assigning a List into string variable, which the system will not allow. You can do something like this:

 string SEMCODE;
            FROM Interaction_c 
            WHERE Contact_c = :record.Contact_c 
            ORDER BY Start_Date_Time__c DESC LIMIT 1].SEM_Code__c;

You can find more information about this here:

  • 1
    Thanks theGreatDanton, it compiled, now to finish this thing! – Kevin Russell Jan 8 '14 at 15:26
  • @KevinRussell Do you also understand why this works and what you were doing wrong ? – Samuel De Rycke Jan 8 '14 at 15:44
  • Not exactly, but I imagine the query is a query object and the .SEM_Code__C is how to access the field property.... It did work for me in my trigger. – Kevin Russell Jan 8 '14 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.