Answered by:
Reading entity

Question
-
In the below code reading entity records (I am reading all records in Opportunity entity)
if (opportunitys != null && opportunitys.Entities != null && opportunitys.Entities.Count > 0)
(opportunitys.Entities.Count > 0) is returning 0, its has to be greater than "0".
I am not sure on this, can anybody tell me what the error could be?
BELOW IS THE CODE::
*****************************************************
private Entity ReadOpportunityByOpportunityId(string opportunityID, IOrganizationService service)
**********************************************************
{
Entity opportunity = null;
string functionName = "ReadOpportunityByOpportunityId";
try
{
QueryByAttribute query = new QueryByAttribute("opportunity");
query.Attributes.Add("ascent_opportunitynumber");
query.Values.Add(opportunityID);
EntityCollection opportunitys = service.RetrieveMultiple(query);
if (opportunitys != null && opportunitys.Entities != null && opportunitys.Entities.Count > 0)
{
opportunity = opportunitys.Entities[0];
}
}
catch (FaultException<DiscoveryServiceFault> ex)
{
if (ex.InnerException != null)
{
LogFiles(functionName + "ERROR" + DateTime.Now + " " + ">>" + ex.StackTrace + ">>" + ex.InnerException.Message);
}
else
{
LogFiles(functionName + "ERROR" + DateTime.Now + " " + ">>" + ex.StackTrace + ">>" + ex.Message);
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
if (ex.InnerException != null)
{
LogFiles(functionName + "ERROR" + DateTime.Now + " " + ">>" + ex.StackTrace + ">>" + ex.InnerException.Message);
}
else
{
LogFiles(functionName + "ERROR" + DateTime.Now + " " + ">>" + ex.StackTrace + ">>" + ex.Message);
}
}
catch (Exception ex)
{
LogFiles(functionName + "ERROR" + DateTime.Now + " " + ">>" + ex.StackTrace + ">>" + ex.Message);
}
return opportunity;
}Sunday, August 31, 2014 1:42 AM
Answers
-
Thanks for the reply, I started with a fresh set, I think it was not talking to CRM properly, the user has the correct privilege.
- Marked as answer by Nandhini12 Friday, September 5, 2014 1:44 PM
Friday, September 5, 2014 1:44 PM
All replies
-
your code can be reduced to:
private Entity ReadOpportunityByOpportunityId(string opportunityID, IOrganizationService service) { Entity opportunity = null; QueryByAttribute query = new QueryByAttribute("opportunity"); query.Attributes.Add("ascent_opportunitynumber"); query.Values.Add(opportunityID); EntityCollection opportunities = service.RetrieveMultiple(query); if (opportunities.Entities.Count > 0) { opportunity = opportunities.Entities[0]; } return opportunity; }
this because a RetrieveMulitple (if the IOrganizationService is valid) doesn't throw an exception.
If your Count returns 0 this simply means that there are no opportunities with ascent_opportunitynumber equal to the string parameter your are passing.
My blog: www.crmanswers.net - Rockstar 365 Profile
- Proposed as answer by Guido PreiteMVP Sunday, August 31, 2014 2:53 AM
Sunday, August 31, 2014 2:53 AM -
I did shorten the code.
I am still getting
opportunities.Entities.Count = 0
I suspect its retrieving any thing from
service.RetrieveMultiple(query); the parameter value I am passing is good and that opportunity exists with that id, so not sure why its not retrieving that record.
Monday, September 1, 2014 6:32 PM -
QueryByAttribute query = new QueryByAttribute("opportunity");
query.ColumnSet = new ColumnSet("ascent_opportunitynumber");
query.Attributes.Add("ascent_opportunitynumber");
query.Values.Add(oppID);
EntityCollection opportunities = service.RetrieveMultiple(query);
if (opportunities.Entities.Count > 0) this is still returning "0", not sure why its not picking up the opportunity when it exists in CRM
Wednesday, September 3, 2014 5:42 PM -
Hi,
Your code is ok. I tried it. Which credentials do you use while creating service? Maybe the user do not have read privilige of opportunity.
And there is a retrieve multiple plugin for opportunity. Can you check this too.
Polat Aydın Crm Software Developer
- Edited by Polat Aydın[MCP] Wednesday, September 3, 2014 8:45 PM
Wednesday, September 3, 2014 6:16 PM -
Thanks for the reply, I started with a fresh set, I think it was not talking to CRM properly, the user has the correct privilege.
- Marked as answer by Nandhini12 Friday, September 5, 2014 1:44 PM
Friday, September 5, 2014 1:44 PM