locked
Dynamic Entities and reading Properties RRS feed

  • Question

  • Hi,

    I need your assistance on the following two questions please:

    1.) I'm trying to do age calculations based on the birthdate field on the contact entity. I've managed to read the birthdate field on the contact, do the calculations but need to update a custom attribute on contact called xyz_age. How do I update a custom attribute on a system entity (contact)?
    2.) Further in my solution do I need to call a specific workflow which requires the contactid to be passed through when executing a workflow request. Whenever I try referencing the contactid property I get the following error: The given key was not present in the dictionary.

    Below is my code snippet:

    public static void Method2()
    
            {
    
                try
    
                {
    
                    Console.WriteLine("Executing CRM Code");
    
    
    
                    CrmAuthenticationToken token = new CrmAuthenticationToken();
    
                    token.AuthenticationType = 0;
    
                    token.OrganizationName = "xyz";
    
    
    
                    CrmService service = new CrmService();
    
                    service.Url = "http://xyz:5555/mscrmservices/2007/crmservice.asmx";
    
                    service.CrmAuthenticationTokenValue = token;
    
                    service.Credentials = System.Net.CredentialCache.DefaultCredentials;
    
                    QueryExpression query = new QueryExpression();
    
                    query.EntityName = "contact";
    
    
    
                    query.ColumnSet = new AllColumns();
    
    
    
                    RetrieveMultipleRequest retieve = new RetrieveMultipleRequest();
    
                    retieve.Query = query;
    
                    retieve.ReturnDynamicEntities = true;
    
    
    
                    RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)service.Execute(retieve);
    
    
    
                    foreach (DynamicEntity entity in retrieved.BusinessEntityCollection.BusinessEntities)
    
                    {
    
                        if (entity.Properties.Contains("birthdate") || entity.Properties.Contains("contactid"))
    
                        {
    
                            CrmDateTime sProp = (CrmDateTime)entity.Properties["birthdate"];
    
                            StringProperty sContatId = (StringProperty)entity.Properties["contactid"];
    
    
    
                            string dob = sProp.date.ToString(); //this works fine, I can read the birthdate value
    
                            string contactId = sContatId.Value.ToString(); //this is givng me the key not found error
    
    
    
                            Console.WriteLine(contactId.ToString());
    
                            Console.WriteLine(string.Format("DOB = {0} Current Age = {1}", dob, CalcAge(dob)));
    
    
    
                            //Update the dynamic enity
    
                            entity.Properties.Add(new StringProperty("xyz_age", CalcAge(dob).ToString()));
    
                        }
    
                        else
    
                        {
    
                            entity.Properties["birthdate"] = 0;
    
                            entity.Properties["contactid"] = 0;
    
                        }
    
                    }
    
    

    You help is much appreciated.

    Thanks.

    Monday, May 25, 2009 9:35 AM

Answers

  • Hi.

    contact id is Key property, so cast must be following:

     KeyProperty sContatId = (KeyProperty)entity.Properties["contactid"
    ];

    and GUID value retrieve must be following:

    string
     contactId = sContatId.Value.Value.ToString();

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Monday, May 25, 2009 10:23 AM
    Moderator