You need to sign in to do that
Don't have an account?
nicloz
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".
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
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.
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:
thanks for your help this solution also help me, I can use above solution or your solution, both works fine.
Thanks so much it did work for me too.
I have a different situation, but it did work as well.
Thanks
Alex