Search code examples
c#ssisdynamics-crmscript-component

How to update Name field in Pricing Approval Products(Custom Entity) in Dynamics CRM using ssis script component


I need to update Name field in Pricing Approval Products using ssis. This is my Code in script component.

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    Guid AppProductLookup = new Guid();
    AppProductLookup = getAppProductId(Row.Name, ref organizationservice);
    Entity AppProductEnt = new Entity("new_ticketproduct");
    ColumnSet columns = new ColumnSet(true);
    columns = new ColumnSet(new String[] { "new_name"});

    AppProductEnt = organizationservice.Retrieve(AppProductEnt.LogicalName, AppProductLookup, columns);
    AppProductEnt["new_name"] = Row.Name;
    organizationservice.Update(AppProductEnt);

}

public Guid getAppProductId(string name, ref IOrganizationService service)
{

    Guid AppProductId = Guid.Empty;
    QueryExpression AppProduct = new QueryExpression { EntityName = "new_ticketproduct", ColumnSet = new ColumnSet(true) };
    AppProduct.Criteria.AddCondition("new_name", ConditionOperator.Equal, name);
    EntityCollection AppProductRetrieve = service.RetrieveMultiple(AppProduct);

    if (AppProductRetrieve != null && AppProductRetrieve.Entities.Count > 0)
    {

        AppProductId = AppProductRetrieve.Entities[0].GetAttributeValue<Guid>("new_ticketproductid");

    }
    return AppProductId;
}

But I got an exception: Entity reference cannot have id and key attributes empty. But my name field in crm is text field tho. I also using check `

if (AppProductLookup != Guid.Empty)
    {
        AppProductEnt = organizationservice.Retrieve(AppProductEnt.LogicalName, AppProductLookup, columns);
        AppProductEnt["new_name"] = Row.Name;
        organizationservice.Update(AppProductEnt);
    }
    else
    {
        //AppProductEnt["new_name"] = Row.Name;
        //organizationservice.Create(AppProductEnt);
    }

My breakpoint skip this code if (AppProductLookup != Guid.Empty). Thus I got no error, but thats not what I want. I have NO idea what is wrong. So I am a bit lost here.

enter image description here

enter image description here


Solution

  • As an alternative to this, I solved my issue by Define alternate keys for Pricing Approval Products entity and Use UpsertRequest to insert or update a record in CRM. Please note Alternate key and UpsertRequest only applies to Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online.