web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

can not retrieve an account entity and its value.

(0) ShareShare
ReportReport
Posted on by 410

Hello,

   I am trying to retrieve an acount enity value, which I am sure is created and avaalbe in my dynamic CRM. The below is my code.

Guid contactId = service.Create(victor);
                Console.WriteLine($"contact name {victor["firstname"]} was created");

                QueryByAttribute account = new QueryByAttribute(Account.EntityLogicalName);
                account.AddAttributeValue("name", "Walsall Manor");
                account.ColumnSet = new ColumnSet("accountid");

                EntityCollection entityCollection = service.RetrieveMultiple(account);

                Account retrievedaccount = (Account)entityCollection.Entities[0];

                Console.WriteLine($"Account with name - {retrievedaccount.Name} retrieved");

It is giving me an out of range error. Thanks 

*This post is locked for comments

I have the same question (0)
  • David Jennaway Profile Picture
    14,065 on at

    The 'out of range' is almost certainly with the following line, which you would get if there are no matching records in CRM, so I suggest you check again that you have an account that matches the given criteria:

     Account retrievedaccount = (Account)entityCollection.Entities[0];


  • gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    Try with this using query expression -

                Guid contactId = service.Create(victor);
                Console.WriteLine($"contact name {victor["firstname"]} was created");
    
                //  Query using ConditionExpression and FilterExpression  
                ConditionExpression condition1 = new ConditionExpression();
                condition1.AttributeName = "name";
                condition1.Operator = ConditionOperator.Contains;
                condition1.Values.Add("Walsall");
    
                FilterExpression filter1 = new FilterExpression();
                filter1.Conditions.Add(condition1);
    
                QueryExpression query = new QueryExpression(Account.EntityLogicalName);
                query.ColumnSet.AddColumns("accountid", "name");
                query.Criteria.AddFilter(filter1);
    
                EntityCollection result1 = service.RetrieveMultiple(query);
                Console.WriteLine(); Console.WriteLine("Query using Query Expression with ConditionExpression and FilterExpression");
                Console.WriteLine("---------------------------------------");
                foreach (var a in result1.Entities)
                {
                    Console.WriteLine("Account with name: " + a.Attributes["name"] +" retrieved" );
                    
                }


  • Victor Onyebuchi Profile Picture
    410 on at

    Hi David, I am very sure, there is an account entity with a name field and a value of Walsall Manor.

  • Victor Onyebuchi Profile Picture
    410 on at

    This is retrieving multiple Goutam, i want to retrieve just one entity and its value.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Yes but you can choose top 1 record once retrieved if you need one and also instead of contains you make it equal.

    To retrieve one record you need the id value -

    Check this article -

    https://rajamadduri.wordpress.com/2017/07/13/difference-between-retrieve-and-retrieve-multiple/

  • Victor Onyebuchi Profile Picture
    410 on at

    That link tells me things i already know. I am only trying to retrieve a single entity and i do not know the GUID yet.

  • Verified answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at
    I would recommend you to go to Adv. Find in CRM & search for account name = Walsall Manor, then download fetchxml. Use that fetchxml query in RetrieveMultiple using below syntax: service.RetrieveMultiple(new FetchExpression(fetchXmlString)) arunpotti.wordpress.com/.../retrieve-records-using-fetchxml-c-sdk-in-crm
  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Victor,

    As david mentioned, you will get the error "out of range" if there are no results. To avoid this, you can have a chck to count the entity record  if (entityCollection.Entities.Count > 0). Also you are just retriving the accountid column but you are using name to display "{retrievedaccount.Name}", this may also give you error.

    Hope this helps.

  • Victor Onyebuchi Profile Picture
    410 on at

    Hi Arun,

       Thanks for responding, but i am still getiin error -"

    there was an error : System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Invalid XML. (Fault Detail is equal to Exception details:
    ErrorCode: 0x80040201
    Message: Invalid XML.
    TimeStamp: 2018-08-09T08:12:39.9420553Z
    --
    Exception details:
    ErrorCode: 0x80040216
    Message: System.Xml.XmlException: '-' is an unexpected token. The expected token is '='. Line 1, position 31.
    at System.Xml.XmlTextReaderImpl.Throw(Exception e)
    at System.Xml.XmlTextReaderImpl.ParseAttributes()
    at System.Xml.XmlTextReaderImpl.ParseElement()
    at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
    at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
    at Microsoft.Crm.Platform.Server.Utility.XmlHelper.LoadXmlInfo(String xmlInfo)
    at Microsoft.Crm.Query.EntityExpression.ExtractPlatformName(String fetchXml, XElement element): Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #66C60960
    TimeStamp: 2018-08-09T08:12:39.9430511Z
    --
    )."

    the below is my Code using fetchXML:

    string fetch = @"<fetch version = '1.0' output - format = 'xml-platform' mapping = 'logical' distinct = 'false'>         
                               < entity name = 'account' >         
                               < attribute name = 'name' />          
                               < attribute name = 'primarycontactid' />            
                               < attribute name = 'telephone1' />           
                               < attribute name = 'accountid' />          
                               < order attribute = 'name' descending = 'false' />              
                               < filter type = 'and' >                
                               < condition attribute = 'name' operator= 'q' value = 'walsall trust' />                     
                               </ filter >               
                               </ entity >
                               </ fetch >";
    EntityCollection entityCollection = service.RetrieveMultiple(new FetchExpression(fetch));
                Guid retrievedacctid = Guid.Empty;
    
                string retrievedAcctName = string.Empty;
                if (entityCollection.Entities.Count > 0)
                {
                    //Account retrievedacct = null;
                    
                    foreach (var entity in entityCollection.Entities)
                    {
                        if (entity.Attributes.Contains("walsall trust"))
                        {
                            //retrievedacct = entity.GetAttributeValues<entity>("account");
                            retrievedacctid = entity.Id;
                            retrievedAcctName = entity.GetAttributeValue<string>("name");
                        }
                    }
                }
    
                Console.WriteLine($"Account with name - {retrievedAcctName} retrieved");
  • Victor Onyebuchi Profile Picture
    410 on at

    Hi Ravi, please look at my reply below. Thanks

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans