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
SampathNamburiSampathNamburi 

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

Best Answer chosen by Admin (Salesforce Developers) 
gtindugtindu

Try this

User u = [Select id, FirstName from User where id =: UserInfo.getUserId() LIMIT 1];
c = [select id, name from contact b WHERE b.FirstName =: u.FirstName];

 

 

All Answers

BangwWthSalesforceBangwWthSalesforce

Hi,

 

plz try this query instead of yours....

 

 [select id, name,FirstName from contact WHERE FirstName =:User.FirstName];

SampathNamburiSampathNamburi

Hi,

 

 Same error.

 

 

 public class PurchasedProducts {
   List<Contact> c; 
  public List<Contact> getPurchasedProducts() {
       //if(c == null) c = [select id, name from contact WHERE FirstName =:User.FirstName];
       c = [select id, name,FirstName from contact WHERE FirstName =:User.FirstName];
      
  return c;
   }
   }

 

BomanBoman

How about:

 

[SELECT id, name, FirstName FROM contact c WHERE c.FirstName = :User.FirstName];

SampathNamburiSampathNamburi

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]

BomanBoman

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]

gtindugtindu

Try this

User u = [Select id, FirstName from User where id =: UserInfo.getUserId() LIMIT 1];
c = [select id, name from contact b WHERE b.FirstName =: u.FirstName];

 

 

This was selected as the best answer
SampathNamburiSampathNamburi

Yes it works !!

 

 

public class PurchasedProducts {
   User U; 
   List<Contact> C; 
   public List<Contact> getPurchasedProducts() {
      if(U == null) U = [select id, contactid, name from User WHERE ID =:userinfo.getUserId()];      
      C = [select id, name from Contact WHERE ID =:U.contactid];
      
      return C;
   }
  } 
Thanks,
Sampath

 

OyeCodeOyeCode

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

SForceBeWithYouSForceBeWithYou

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:

Map<Id,Profile> ProfileOwner = new Map<Id, Profile>([SELECT Id, Name FROM Profile]);

 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:

Map<Id,Profile> ProfileOwner = new Map<Id, Profile>([SELECT Id, Name FROM Profile WHERE Id = :UserInfo.getProfileId()]);

 

Cheers! :-)

 

Nathan

May The SForce Be With You!

youtube.com/MayTheSForceBWithYou

@SForceBeWithYou