You need to sign in to do that
Don't have an account?
SampathNamburi
Global variables in Apex Class
I am in the process of developing a controller in which I have to use global variables. While doing so I am receiving the below error.
My code :
public class PurchasedProducts {
List<Contact> c;
public List<Contact> getPurchasedProducts() {
if(c == null) c = [select id, name from contact WHERE FirstName =:User.FirstName];
return c;
}
}
Error : Invalid bind expression type of Schema.SObjectField for column of type String
I am trying to get the Account details of a logged in portal user using an apex class.
I might be wrongly referring the global variable ..Any help on this one is highly appreciated.
Thank you,
Sampath
Try this
All Answers
Hi,
plz try this query instead of yours....
[select id, name,FirstName from contact WHERE FirstName =:User.FirstName];
Hi,
Same error.
How about:
[SELECT id, name, FirstName FROM contact c WHERE c.FirstName = :User.FirstName];
Same error. It is something to do with the binding.
if i pass any string it works but not with the global variable User.Firstname
This works : [select id, name from contact b WHERE b.FirstName =: 'Some Firstname'] //
but not this : [select id, name from contact b WHERE b.FirstName =:User.FirstName]
Try after doing this first:
String userName = UserInfo.getUserName();
User activeUser = [Select FirstName, Username From User u where u.Username = : userName limit 1];
and then:
[select id, name from contact b WHERE b.FirstName =:activeUser.FirstName]
Try this
Yes it works !!
Can anybody correct me here
Map<Id,Profile> ProfileOwner = new Map<Id, Profile>([Select p.Id, p.Name from Profile p where Id =: profile.Id]);
- Getting same error mention about
Error: Compile Error: Invalid bind expression type of Schema.SObjectField for column of type Id
Map<Id,Profile> ProfileOwner = new Map<Id, Profile>([Select p.Id, p.Name from Profile p where Id =: profile.Id]);
The reason it's an invalid bind is because Profile.Id is like the static instance of Id, and hence returns a Schema.SObjectType instead of an profile instance's ID.
You probably want to get all profiles, and are accustomed to SQL syntax. The map constructor that takes a SOQL statment doesn't need a WHERE clause to pull all profiles and map them to the ID's. So if you want that, this is what you'd put:
The only other thing I could think you'd want to know how to do is use the global variable for the current user's profile, but that would give you only one profile in the map, so I'm sure it's not what you need. However, this is what the code for that would look like:
Cheers! :-)
Nathan
May The SForce Be With You!
youtube.com/MayTheSForceBWithYou
@SForceBeWithYou