user1211185
user1211185

Reputation: 731

CrmServiceContext not loading relationship data with CRM 2015 Online

I am in process to convert/upgrade an application to use CRM 2015 Online instead of CRM 2011. But it is failing to load relationship data with connection using CRM 2015. However it is working with CRM 2011. I have created the Entity wrapper using latest CRM 2015 SDK.

Code To Retrieve Order

var context = GetNewContext();

var ordersToProcess = (from o in context.SalesOrderSet
                       where o.SalesOrderId == orderId
                       select o).Distinct().ToList();

foreach (var salesOrder in ordersToProcess)
{
    // *** Error Here ***
    // *** CRM 2015 Code is throwing error here because transactioncurrency_salesorder is null ***
    var urrencyCode =
             salesOrder.transactioncurrency_salesorder.ISOCurrencyCode;
}

Generate Connection using CRM 2011 Code

public static CrmServiceContext GetNewContext(CrmConnection conn)
{
    var context = new CrmServiceContext(conn);
    return context;
}

Generate Connection using CRM 2015 Online Code

public static IOrganizationService GetNewService(CrmConnection conn)
{
    var service = new OrganizationService(conn);
    return service;
}

public static CrmServiceContext GetNewContext(CrmConnection conn)
{
    var context = new CrmServiceContext(GetNewService(conn));
    return context;
}

Upvotes: 2

Views: 460

Answers (1)

Henrik H
Henrik H

Reputation: 5787

transactioncurrency_salesorder is a N:1-relationship between Sales Order and Currency.

If your context inherits from OrganizationServiceContext (which I assume is the case for your 2015-code), these relationships are not loaded by default. You can have them lazy loaded by calling LoadProperty before accessing the relationship:

context.LoadProperty(salesOrder, "transactioncurrency_salesorder");

If your context inherits from CrmOrganizationServiceContext (which I assume is the case for your 2011-code), relationships are automatically lazy loaded.

Alternatively, if you do not need to actually load the Currency itself (and only want the name or Guid of the Currency), you can use the EntityReference TransactionCurrencyId. This is always set, also without calling LoadProperty. You could e.g. get the name of the Currency as follows:

salesOrder.TransactionCurrencyId.Name

Upvotes: 3

Related Questions