+ Start a Discussion
Suresh RaghuramSuresh Raghuram 

Invalid bind expression error

Save error: Invalid bind expression type of SOBJECT:AggregateResult for Id field of SObject Contact

 

 

List<contact>  cnt = new List<Contact>([select id, name, from contact where contact.id =: [select max(id) from contact]]);

Best Answer chosen by Admin (Salesforce Developers) 
MattLacey.ax1065MattLacey.ax1065

Aggregate queries return a list of objects called AggregateResult, you can get a specific field from them using the get() method:

 

([select max(id) from Contact]).get('id')

 That said, I'm not sure you're approaching your problem the right way. What are you aiming to retrieve, the last contact added to the system?

 

If that's the case you could use createddate:

 

[select Id, Name from Contact order by CreatedDate desc limit 1]

 

Matt

All Answers

MattLacey.ax1065MattLacey.ax1065

Aggregate queries return a list of objects called AggregateResult, you can get a specific field from them using the get() method:

 

([select max(id) from Contact]).get('id')

 That said, I'm not sure you're approaching your problem the right way. What are you aiming to retrieve, the last contact added to the system?

 

If that's the case you could use createddate:

 

[select Id, Name from Contact order by CreatedDate desc limit 1]

 

Matt

This was selected as the best answer
Suresh RaghuramSuresh Raghuram

the one with created date is worked rather than the aggretgate function max thank  you very much matt could you suggest some sceaniros to write classes and to learn soql quries once agin thank u 

MattLacey.ax1065MattLacey.ax1065

I'd highly recommend reading through the Developer Guide, it's pretty hands-on and includes a lot of very useful information!