locked
ServiceContext.CreateQuery Not Returning All Key / Value Pairs RRS feed

  • Question

  • CMR 2013 On Premise

    Hello,

    I am querying the customeraddress entity and updating several values...or trying to.

    When a lead is created the user may not have a city in the address field. When the lead is saved the address is saved. Later when the system tries to update the city in a plugin I use this line to obtain the address

    var res = from z in ServiceContext.CreateQuery("customeraddress")

    Problem is that since the city field is empty it drops the city key / value pair and thus when I go to update it doesn't exists. Is there a way I can tell the service context to get me all keys / value pairs (aka db columns)?

    TIA

    Full code snippet in case above is not enough context:

     //query to get address records
    var res = from z in ServiceContext.CreateQuery("customeraddress")
    where z["parentid"].Equals(AddyContactId)
    select z;
    
    foreach (var c in res)
    {
       Entity e = (Entity)c;
    
        //here I go to find / update the city key only it is not there
    
    }

    Friday, February 7, 2014 2:16 PM

Answers

  • CRM will only return attributes on a record that have a value in them. There is no way to change this behaviour. CRM will also always return the primary key, which is why you get customeraddressid in your example.

    The only way to identify all possible columns is to query the metadata, for example with a RetrieveEntityRequest.


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    • Marked as answer by zzpluralza Tuesday, February 11, 2014 9:50 PM
    Tuesday, February 11, 2014 9:42 PM
    Moderator

All replies

  • Try this,

    Entity entityName = ServiceContext.Retrieve("customeraddress", parentid, new ColumnSet(true));

    • Proposed as answer by VikkyVignesh Monday, February 10, 2014 8:01 AM
    • Unproposed as answer by zzpluralza Tuesday, February 11, 2014 8:37 PM
    Monday, February 10, 2014 8:01 AM
  • I'm sorry but this did not work either. While I agree 100% it is valid I am finding...at least in my case...that the Boolean true for a ColumnSet to return all columns is *NOT* working. The only way I could get all columns to return was to list EVERY column in a string array and pass that as the columnset.

    Frustrating. I'm not sure if it is me or not but if it is Microsoft then this is the second bug this week I have had the pleasure of finding in 2013 On Premise.

    • Marked as answer by zzpluralza Tuesday, February 11, 2014 11:59 AM
    • Unmarked as answer by zzpluralza Tuesday, February 11, 2014 12:25 PM
    Tuesday, February 11, 2014 11:59 AM
  • This is getting really frustrating.

    So if I specify the columns then one record will come back with all 6 key / value pairs and the other will only come back with one.....and it's not even one of the one I specified???!!!

    Arrrggghhh!!!

     List<Tuple<string, string, ConditionOperator>> Cond = new List<Tuple<string, string, ConditionOperator>>();
    Cond.Add(new Tuple<string, string, ConditionOperator>("parentid", AddyContactId.ToString(), ConditionOperator.Equal));
    
    string[] cols = new string[]{"city", "postofficebox", "stateorprovince", "line1", "postalcode" };
    
    QueryExpression AddyQuery = OE_Workflow_Helpers.BuildEntityQuery(CustomerAddress.EntityLogicalName, cols, Cond, LogicalOperator.Or);
    
    
    
    EntityCollection entityName = OrgSvc.RetrieveMultiple(AddyQuery); 
    
    
                                
    foreach (var c in entityName.Entities)
    { ...}

    Wanna guess what the key / value pair is for the one column that does come back in the other record(s)?

    customeraddressid

    Is this a bug or am I just totally clueless about the proper way to do this query??


    • Edited by zzpluralza Tuesday, February 11, 2014 12:40 PM
    Tuesday, February 11, 2014 12:39 PM
  • CRM will only return attributes on a record that have a value in them. There is no way to change this behaviour. CRM will also always return the primary key, which is why you get customeraddressid in your example.

    The only way to identify all possible columns is to query the metadata, for example with a RetrieveEntityRequest.


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    • Marked as answer by zzpluralza Tuesday, February 11, 2014 9:50 PM
    Tuesday, February 11, 2014 9:42 PM
    Moderator
  • That explains a lot!! Thank You.
    Tuesday, February 11, 2014 9:50 PM