Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

CRM Dynamics 2011 to 2016 upgrade problems

Posted on by 5

We have upgraded our CRM Dynamics system from 2011 - 2016 and getting issues as we have had to change from the SOAP web reference approach, to the SDK.

Types have changed now and we are wondering the best way to update this. We are wondering if the following function can be updated? And the best approach forward. We have updated the GetValue() function but are struggling on the others. 

Main code

public Property SetProperty(Field field, object value)
    {
        if (field.Type == typeof(LookupProperty))
        {
            Guid id = Guid.Empty;
            if (value.GetType() == typeof(Guid))
            {
                id = (Guid)value;
            }
            else id = GetRecordGuid(field.LookupEntity, field.SearchField, value);
            if (id != Guid.Empty)
            {
                LookupProperty lp = new LookupProperty();
                lp.Name = field.Name;
                lp.Value = new Lookup();
                lp.Value.Value = id;
                lp.Value.type = field.LookupEntity;
                return lp;
            }
        }
        else if (field.Type == typeof(KeyProperty))
        {
            KeyProperty kp = new KeyProperty();
            kp.Name = field.Name;
            kp.Value = new Key();
            kp.Value.Value = new Guid(Convert.ToString(value));
            return kp;
        }
        else if (field.Type == typeof(StringProperty))
        {
            StringProperty sp = new StringProperty();
            sp.Name = field.Name;
            sp.Value = value.ToString();
            return sp;
        }
        else if (field.Type == typeof(CrmBooleanProperty))
        {
            bool v = new bool();
            bool b = Boolean.TryParse(Convert.ToString(value), out v);
            if (b)
            {
                CrmBooleanProperty bp = new CrmBooleanProperty();
                bp.Name = field.Name;
                bp.Value = new CrmBoolean();
                bp.Value.Value = v;
                return bp;
            }
        }
        else if (field.Type == typeof(CrmDateTimeProperty))
        {
            string dval = Convert.ToString(value);
            string fdate = dval.Substring(6, 4)   "-"   dval.Substring(3, 2)   "-"   dval.Substring(0, 2)   "T21:00:00";

            DateTime dt = new DateTime();
            bool b = DateTime.TryParse(fdate, out dt);
            if (b)
            {
                CrmDateTimeProperty dp = new CrmDateTimeProperty();
                dp.Name = field.Name;
                dp.Value = new CrmDateTime();
                dp.Value.Value = Convert.ToString(fdate);
                return dp;
            }
        }
        else if (field.Type == typeof(CrmDecimalProperty))
        {
            decimal d = new decimal();
            bool b = Decimal.TryParse(Convert.ToString(value), out d);
            if (b)
            {
                CrmDecimalProperty dp = new CrmDecimalProperty();
                dp.Name = field.Name;
                dp.Value = new CrmDecimal();
                dp.Value.Value = d;
                return dp;
            }
        }
        else if (field.Type == typeof(CrmMoneyProperty))
        {
            decimal d = new decimal();
            bool b = Decimal.TryParse(Convert.ToString(value), out d);
            if (b)
            {
                CrmMoneyProperty mp = new CrmMoneyProperty();
                mp.Name = field.Name;
                mp.Value = new CrmMoney();
                mp.Value.Value = d;
                return mp;
            }
        }
        else if (field.Type == typeof(PicklistProperty))
        {
            int i = -1;
            bool b = Int32.TryParse(Convert.ToString(value), out i);
            if (b)
            {
                PicklistProperty pp = new PicklistProperty();
                pp.Name = field.Name;
                pp.Value = new Picklist();
                pp.Value.Value = i;
                return pp;
            }
        }
        return null;
    }

}

GetRecordGuid

public Guid GetRecordGuid(string entity, string searchField, object searchValue)
{
    ColumnSet cs = ColumnSet(new string[] { entity   "id" });
    ConditionExpression ce = ConditionExpression(searchField, searchValue);
    QueryExpression qe = QueryExpression(entity, cs, new ConditionExpression[] { ce });
    Entity de = RetrieveSingleDynamic(qe);
    if (de != null)
    {
        object key = GetValue(de, entity   "id");
        if (key != null)
        {
            return (Guid)key;
        }
    }
    return Guid.Empty;
}

GetRecordGuid

public Guid GetRecordGuid(string entity, string searchField, object searchValue, string searchField1, object searchValue1)
{
    ColumnSet cs = ColumnSet(new string[] { entity   "id" });
    ConditionExpression ce = ConditionExpression(searchField, searchValue);
    ConditionExpression ce1 = ConditionExpression(searchField1, searchValue1);
    QueryExpression qe = QueryExpression(entity, cs, new ConditionExpression[] { ce, ce1 });
    Entity de = RetrieveSingleDynamic(qe);
    if (de != null)
    {
        object key = GetValue(de, entity   "id");
        if (key != null)
        {
            return (Guid)key;
        }
    }
    return Guid.Empty;
}

public object GetValue(Entity properties, string name) 
{

return properties[name];

}

These functions are being called the following way (this is a sample):

private XmlDocument Create(Crm crm, Action action) 
{ 

//if (!String.IsNullOrEmpty(action.JobXml)) 
//{ 
// XmlDocument jobXml = new XmlDocument(); 
// jobXml.LoadXml(action.JobXml);

// List ps = new List (); 
// DynamicEntity job = new DynamicEntity...Code


We have looked at the following article on how to get values, but wondering the best approach to convert this code...

Dynamics forum article

Let me know if you need any more info, thanks

  • Suggested answer
    sdfasdf Profile Picture
    sdfasdf 840 on at
    RE: CRM Dynamics 2011 to 2016 upgrade problems

    So the problem I see with this approach is that it's returning the property already populated with its proper value. However with the newest version, you'll probably have to change it a little bit. Here's how I believe your current approach would work:

       Field field = new Field();

       field.Type = typeof(LookupProperty);

       field.Name = "primarycontactid";

       field.LookupEntity = "contact";

       Property primaryContactId = SetProperty(field, id);

       DynamicEntity account = new DynamicEntity("account");

       account.Properties.Add(primaryContactId);

    And here is the approach you may want to consider:

       Field field = new Field();

       field.LookupEntity = "contact";

       object primaryContactId = SetProperty(field, id);

       Entity account = new Entity("account");

       account["primarycontactid"] = primaryContactId;

    And this is how the SetProperty method should work:

       public object SetProperty(Field field, object value)

       {

           Guid id = Guid.Empty;

           if (value.GetType() == typeof(Guid))

           {

               id = (Guid)value;

           }

           if (id != Guid.Empty)

           {

               EntityReference reference = new EntityReference(field.LookupEntity, id);

               return reference;

           }

           (...)

       }

    Hope it helps!

  • juzuking Profile Picture
    juzuking 5 on at
    RE: CRM Dynamics 2011 to 2016 upgrade problems

    Hi,

    Thanks for your help, so with the SetProperty() method we have, it would be changed to return an Entity type. So with your example, how would we implement it for this for example:

    if (id != Guid.Empty)

               {

                   LookupProperty lp = new LookupProperty();

                   lp.Name = field.Name;

                   lp.Value = new Lookup();

                   lp.Value.Value = id;

                   lp.Value.type = field.LookupEntity;

                   return lp;

               }

  • Suggested answer
    sdfasdf Profile Picture
    sdfasdf 840 on at
    RE: CRM Dynamics 2011 to 2016 upgrade problems

    In newer versions, we don't have the LookupProperty clas anymore as it's been replaced by EntityReference class.

    Additionally, the whole concept of DynamicEntity class has been replaced by the Entity class. These classes are similar, but they have their differences. This is what's referred to as late bound classes.

    With DynamicEntity class, you would have something like this:

    LookupProperty lp = new LookupProperty();
    lp.Name = "primarycontactid";
    lp.Value = new Lookup("contact", new Guid("<value>"));

    StringProperty sp = new StringProperty();
    sp.Name = "name";
    sp.Value = "Contoso, Inc.";

    DynamicEntity e = new DynamicEntity("account");
    e.Properties.Add(lp);
    e.Properties.Add(sp);

    With the newest obejct model, same code would be as follows:

    Entity e = new Entity("account");
    e["primarycontactid"] = new EntityReference("contact", new Guid("<value>"));
    e["name"] = "Contoso, Inc.";

    As you can see, the new object model does not wrap the attribute values with the Property class, but rather uses the .NET types directly.

    You may also want to try to early bound classes to improve your development experience: docs.microsoft.com/.../create-early-bound-entity-classes-code-generation-tool

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans