You need to sign in to do that
Don't have an account?
System.QueryException: List has no rows for assignment to SObject
Hello,
I am trying to implement an example that uses a method called getLeadAddressByEmail,which uses SOQL to look up a lead based on an email address. The query returns a lead object with only Street, City, State, and PostalCode. But when I run the program, I get the following error:
System.QueryException: List has no rows for assignment to SObject
Class.getLeadInfo.getLeadAddressByEmail: line 4, column 10 External entry point
Source Error:
Line 105: [return: System.Xml.Serialization.XmlElementAttribute("result", IsNullable=true)] Line 106: public Lead getLeadAddressByEmail([System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] string LeadEmail) { Line 107: object[] results = this.Invoke("getLeadAddressByEmail", new object[] { Line 108: LeadEmail}); Line 109: return ((Lead)(results[0])); |
FYI, here's my code:
public partial class _Default : System.Web.UI.Page
{
private string _userId = "yyyyy@zzzzzzz.com";private string _password = "XXXXXXXXXXXXX";
private string _sessionId;private string _serverUrl;
private string _nextLoginTime;private string _leadEmail;
protected void Page_Load(object sender, EventArgs e){
}
protected void cmdSubmit_Click(object sender, EventArgs e){
//Confirm Lead Email Exists
_leadEmail = this.txtEmail.Text;//See if Session ID exists
_sessionId = (string)(Application["_sessionId"].ToString() == null ? Application["_sessionId"] : "");_serverUrl = (string)(Application["_serverUrl"].ToString() == null ? Application["_serverUrl"] : "");_nextLoginTime = (
string)(Application["_nextLoginTime"].ToString() == null ? Application["_nextLoginTime"] : "01/01/200000:00:00"); if ((!isConnected())){
getSessionInfo();
}
//Call getLeadInfo Web Service
getLeadInfoService getLeadInfo = new getLeadInfoService();getLeadInfo.SessionHeaderValue = new getLeadInfo.SessionHeader();getLeadInfo.SessionHeaderValue.sessionId =
this._sessionId;getLeadInfo.Lead getLeadInfoResponse = default(getLeadInfo.Lead);getLeadInfoResponse = getLeadInfo.getLeadAddressByEmail(_leadEmail);
this.txtAddress.Text = getLeadInfoResponse.Street.ToString();
this.txtCity.Text = getLeadInfoResponse.City.ToString();this.txtState.Text = getLeadInfoResponse.State.ToString();
this.txtZip.Text = getLeadInfoResponse.PostalCode.ToString();}
public bool isConnected(){
bool functionReturnValue = false; if (!string.IsNullOrEmpty(_sessionId) & _sessionId != null){
//if (DateTime.Now.ToString() > _nextLoginTime)
if (_nextLoginTime.CompareTo(DateTime.Now.ToString()) < 0){
functionReturnValue = false;}
else
{
functionReturnValue = true;}
}
else {functionReturnValue = false;}
return functionReturnValue;}
public void getSessionInfo(){
sforce.LoginResult lr = default(sforce.LoginResult);sforce.
SforceService ss = new sforce.SforceService();lr = ss.login(_userId, _password);
_sessionId = lr.sessionId;
Application["_sessionId"] = lr.sessionId;_serverUrl = lr.serverUrl;
Application["_serverUrl"] = lr.serverUrl;Application["_nextLoginTime"] = DateTime.Now;}
}
Web Service Code:
global class getLeadInfo {
WebService static Lead getLeadAddressByEmail(string LeadEmail) {
Lead c = [SELECT Street, City, State, PostalCode from Lead WHERE Email = :LeadEmail];
Lead l = new Lead(Street = c.Street, City = c.City, State = c.State, PostalCode = c.PostalCode);
return l;
}
}
Any help would be greatly appreciated. Thanks.
Monir
This a problem with your apex webservice code
Lead c = [SELECT Street, City, State, PostalCode from Lead WHERE Email = :LeadEmail];
The semantics of this are that an exception is thrown is anything other than 1 row is returned, which is the error you are seeing. If you need to handle this case, they you should assign the query result to an array, and examine the size of the array.
hi,
i may not be 100% correct on this, but from a quick look @ the code, in the webservice call try
rather than the
return (Lead(results[0]));
use
Lead lead = new Lead(); // or null assignment - whatever u want.
if (results != null && results.Length > 0)
{
lead = results[0] as Lead; // or (Lead(results[0])); - depending on your preferred syntax.
}
return lead;