InvalidCastException when updating a Dynamics Invoice Detailing Detail

I have the following method to retrieve an object from Dynamics:

private Entity GetEntity(CrmOrganizationServiceContext context, string id, string entityLogicalName, params string[] columnSet)
{
    Guid guid;
    if (!Guid.TryParse(id, out guid))
    {
        return null;
    }

    if (columnSet != null && columnSet.Any())
    {
        return context.Retrieve(entityLogicalName, guid, new ColumnSet(columnSet));
    }

    return context.Retrieve(entityLogicalName, guid, new ColumnSet(allColumns: true));
}

      

I am trying to restore an invoicedetail object and update its attributes. The way to update the attributes is as follows:

public void UpdateAttributes(Entity entity, EntityDto entityDto)
{
    foreach (var attribute in entityDto.Attributes)
    {
        entity[attribute.Key] = attribute.Value;
    }
}

      

And the Update method:

public void Update(CrmOrganizationServiceContext context, IEnumerable<EntityDto> entities)
{
    foreach (var entityDto in entities)
    {
        var entity = GetEntity(context, entityDto.CrmId.ExternalId, entityDto.TypeName, entityDto.Attributes.Keys.ToArray());
        if (entity != null)
        {
            _entityService.UpdateAttributes(entity, entityDto);
            context.Attach(entity);
            context.UpdateObject(entity);
            context.Update(entity);
        }

        context.SaveChanges();
    }
}

      

I noticed that for the invoice object, this method works, but when I try to get invoicedetail (this is a reference to the invoice object) and update the attributes using the method above, I get three additional attributes in my entity out of nowhere (all of them are references to entity) and when I try to update it I get an exception:

System.ServiceModel.FaultException`1: 'System.InvalidCastException: Microsoft Dynamics CRM has encountered an error. Admin Reference or Support: # 635D1534 '

Invoicedetail object attributes

Entity attributes I want to update

As I read on MSDN, updating invoicedetail objects is supported. How can I resolve this exception and update the invoicedetail object in Dynamics?

UPDATE 06.21.2017

After a few days of research, I noticed that if I remove the priceperunit attribute from my dataset for the invoicedetail object, everything works fine. I can create a new object in Dynamics using priceperunit, but I cannot update it in the same way.

entity["priceperunit"] = 999.0;

      

The type of this field in Dynamics is currency (as I see it is not an entity reference), so I think when I try to update this value, Dynamics will add a decimal to the currency (or something like that). Does anyone know how to update values ​​like this?

+3


source to share


2 answers


Did you know that entity references are actually EntityReference

objects on this line:

entity[attribute.Key] = attribute.Value;  

      

Perhaps these are objects Entity

or EntityDto

, in which case you get InvalidCastException

.



If you are unsure and unable to debug, you can inject them and instantiate them EntityReference

:

foreach (var attribute in entityDto.Attributes)
{
    if (attribute.Value is Entity)
    {                                    
        entity[attribute.Key] = new EntityReference(((Entity)attribute.Value).LogicalName, ((Entity)attribute.Value).Id);
    }
    // Check if EntityDto...
}

      

+4


source


You cannot use entity[attribute.Key] = attribute.Value;

for all data types.

Invoices and InvoiceDetail have a parent-child relationship, so the parent invoice ID will be available as Entityreference (lookup) in Detail.

You have to check the data type of each attribute and do the mapping inside the attributes foreach loop.



entity[attribute.Key] = attribute.Value.ToEntityReference();

      

or

entity[attribute.Key] = new EntityReference("entity_name",Id);

      

+1


source







All Articles