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
nicloznicloz 

Ilegal assignment from Schema.SObjectField to String

Dears

 

Please could you help me to get the correct code to the below  issue? I need to take the value of the field name or address and save into a string value, after that these strings I will send via XML to external server.

 

Account a = [SELECT Name FROM Account WHERE Name = 'Foo' LIMIT 1];
String s = a.Name;// I can save it, but when I run the application I get another error.
String c = Account.Name;//second option I get the error "Illegal assignment from Schema.SObjectField to String".
Best Answer chosen by Admin (Salesforce Developers) 
Ispita_NavatarIspita_Navatar

Typecase the fields into string as you assign, that will sort the issue

 

Account a = [SELECT Name FROM Account WHERE Name = 'Foo' LIMIT 1];
String s = a.Name;// I can save it, but when I run the application I get another error.

// try  -->     String s =(String) a.Name;

String c = Account.Name;//second option I get the error "Illegal assignment from Schema.SObjectField to String".

 

// try  -->     String c = (String)Account.Name;

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

All Answers

Ispita_NavatarIspita_Navatar

Typecase the fields into string as you assign, that will sort the issue

 

Account a = [SELECT Name FROM Account WHERE Name = 'Foo' LIMIT 1];
String s = a.Name;// I can save it, but when I run the application I get another error.

// try  -->     String s =(String) a.Name;

String c = Account.Name;//second option I get the error "Illegal assignment from Schema.SObjectField to String".

 

// try  -->     String c = (String)Account.Name;

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

This was selected as the best answer
bob_buzzardbob_buzzard

 

String c = Account.Name;

will give you an error as you don't have a variable named 'Account'.  Thus you are trying to access a field from an object definition rather than the variable itself.

 

Can you post what the error is when you use 'a.Name' - that should work given the query that you are executing as long as there is always an account matching the query.

 

A better solution would be:

 

 

List<Account> accs = [SELECT Name FROM Account WHERE Name = 'Foo' LIMIT 1];

if (!accs.isEmpty())
{
  String s = accs[0].Name;
  // more stuff here
}
else
{
  // some handler code for when there is no matching account
}

 

 

 

 

nicloznicloz

thanks for your help this solution also help me, I can use above solution or your solution, both works fine.

 

List<Account> accs = [SELECT Name FROM Account WHERE Name = 'Foo' LIMIT 1];

if (!accs.isEmpty())
{
String s = accs[0].Name;
// more stuff here
}
else
{
// some handler code for when there is no matching account
}
elpaso750elpaso750

Thanks so much it did work for me too. 

 

I have a different situation, but it did work as well.

 

Thanks

Alex

Tom MasciTom Masci
Thanks bobb_buzzard your code snipet helped me through a stumbling block.