locked
retrieve the retrieve multiple value (crmService) RRS feed

  • Question

  • hi, i created retrieve multiple example according to the library of msdn, but it shows nothing. the code is :

                ColumnSet cols = new ColumnSet();

                // Set the properties of the ColumnSet.
                cols.Attributes = new string[] {"accountid"};

                // Create the ConditionExpression.
                ConditionExpression condition = new ConditionExpression();

                // Set the condition for the retrieval to be when the contact's address' city is Sammamish.
                condition.AttributeName = "name";
                condition.Operator = ConditionOperator.Like;
                condition.Values = new string[] { "1212" };

                // Create the FilterExpression.
                FilterExpression filter = new FilterExpression();

                // Set the properties of the filter.
                filter.FilterOperator = LogicalOperator.And;
                filter.Conditions = new ConditionExpression[] { condition };

                // Create the QueryExpression object.
                QueryExpression query = new QueryExpression();

                // Set the properties of the QueryExpression object.
                query.EntityName = EntityName.account.ToString();
                query.ColumnSet = cols;
                query.Criteria = filter;

                // Retrieve the contacts.
                BusinessEntityCollection accounts = service.RetrieveMultiple(query);
                foreach (BusinessEntity be in accounts.BusinessEntities)
                {
                    DynamicEntity de = (DynamicEntity)be;
                    string ids = de.Properties[0].ToString();
                    Response.Write(" <script   language=JavaScript> alert( '" + ids + "'); </script> ");            
                }     

    ////// also, for  string ids = de.Properties[0].ToString(); , the example in the previous post is something like  string ids = de.Properties["firstname"].ToString();, but it shows error that the parameter should be integer, when i used  string ids = de.Properties["accountid"].ToString();

    p.s i can find the name = 1212 Limited in the crm

    is there anything wrong of the code?

    thanks

    Tuesday, January 11, 2011 5:01 AM

Answers

  • If you are using business entity and you know it will return "account". You can always cast it directly. See below.

                foreach (account act in retrieved.BusinessEntityCollection.BusinessEntities)
               {

                    Console.WriteLine(act.name.ToString());
               }


    regards,
    Toong Yang
    http://eastoceantechnical.blogspot.com/
    • Marked as answer by verystupidguy Tuesday, January 11, 2011 8:39 AM
    Tuesday, January 11, 2011 8:15 AM
  • For the like operator, you need to use something like %

    new object[] {"1101%"}

    old school sql thing ;)


    regards,
    Toong Yang
    http://eastoceantechnical.blogspot.com/
    • Marked as answer by verystupidguy Wednesday, January 12, 2011 2:01 AM
    Tuesday, January 11, 2011 2:08 PM

All replies

  • I sae you query the accountid which should be a lookup in crm.

    try something like this.

    Lookup property = (Lookup)entity.Properties["new_contractheaderid"];


    regards,
    Toong Yang
    http://eastoceantechnical.blogspot.com/
    Tuesday, January 11, 2011 5:32 AM
  • hi,  i tried to change, but the error is the same, it showed Error 7 Cannot implicitly convert type 'string' to 'int' .

    but i just followed this example http://technet.microsoft.com/en-us/library/bb929002.aspx for the retrieve.

     

    also, i found that the ConditionOperator.Like; doesn't work, the program can fetch the record when i used

    condition.AttributeName = "name";
                condition.Operator = ConditionOperator.Equal;
                condition.Values = new string[] { "1212 Limited" };

    but it couldn't fetch the record when it is Like when 1212

    thanks
               

    Tuesday, January 11, 2011 7:27 AM
  • try this

    ColumnSet cols = new ColumnSet();
    cols.Attributes = new string[] {"accountid"};
    ConditionExpression condition = new ConditionExpression();
    condition.AttributeName = "name";
    condition.Operator = ConditionOperator.Like;
    condition.Values = new string[] { "1212" };

    FilterExpression filter = new FilterExpression();
    filter.FilterOperator = LogicalOperator.And;
    filter.Conditions = new ConditionExpression[] { condition };

    QueryExpression query = new QueryExpression();
    query.EntityName = EntityName.account.ToString();
    query.ColumnSet = cols;
    query.Criteria = filter;

    BusinessEntityCollection accounts = service.RetrieveMultiple(query);

    foreach (DynamicEntity cont in accounts.BusinessEntityCollection.BusinessEntities)
    {
           if (cont.Properties.Contains("accountid"))
           {
               Key accountidKey = (Key)entity.Properties["accountid"];
               String account_id = accountidKey.Value.ToString();
           }
    }

     

    Tuesday, January 11, 2011 8:04 AM
  • If you are using business entity and you know it will return "account". You can always cast it directly. See below.

                foreach (account act in retrieved.BusinessEntityCollection.BusinessEntities)
               {

                    Console.WriteLine(act.name.ToString());
               }


    regards,
    Toong Yang
    http://eastoceantechnical.blogspot.com/
    • Marked as answer by verystupidguy Tuesday, January 11, 2011 8:39 AM
    Tuesday, January 11, 2011 8:15 AM
  • i tried this;

              condition.AttributeName = "autoNumber";
                condition.Operator = ConditionOperator.Like;
                condition.Values = new string[] { "11009006" };

    then it can retrieve the record.

    but if i used

              condition.AttributeName = "autoNumber";
                condition.Operator = ConditionOperator.Like;
                condition.Values = new string[] { "1100" };

    it cannot retrieve anything, thus what is the uses of Like? when i should provide the entire name of it ? (the SQL like can fetch all records that autoNumber contained the sub-string "1100"). while ConditionOperator.Equal is doing the same thing.

    thanks

    Tuesday, January 11, 2011 8:48 AM
  • For the like operator, you need to use something like %

    new object[] {"1101%"}

    old school sql thing ;)


    regards,
    Toong Yang
    http://eastoceantechnical.blogspot.com/
    • Marked as answer by verystupidguy Wednesday, January 12, 2011 2:01 AM
    Tuesday, January 11, 2011 2:08 PM