0

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

xChaax
  • 193
  • 5
  • 27
  • What is your code trying to do? It looks like it's searching for a record based on name and then updating the name field to the value that was used to search for it. Also the line "if (!AppProductEnt.Contains("new_ticketproductid"))" means 'If a matching record isn't found'. Is this what you intended? – Mike Feingold Oct 03 '17 at 14:52
  • I edited my code already. What I want is If name already exist in CRM based on GUID's checking, I want to update the name instead of creating new. So what I already did is retrieve the existing GUID for name record in CRM and try to update based on the GUID. But Its not working as per I want. – xChaax Oct 03 '17 at 15:08
  • Still not clear. The code is searching for a record with name = blaa and then setting it to name = blaa. Why? Also if the call to getAppProductId() returns Guid.Empty then the call to Update() will fail because there's no record to update. – Mike Feingold Oct 03 '17 at 15:34
  • For example. I create a name in crm. Then If I change the name in source file, how can I update the new value(name) on crm. I need to get the guid of existing record(name) in crm first right ? Only then I can update its back. This is where I was stuck to update a new value(name) in crm for same guid. Hope this clear. Appreciate if u can help. – xChaax Oct 03 '17 at 15:44
  • Then the input file needs 2 columns. Column 1 is the name to look for and column 2 is the new name that should be used to update the record. The code shows there's only 1 column (`Row.Name`) – Mike Feingold Oct 03 '17 at 15:55
  • Both columns have same value ? – xChaax Oct 03 '17 at 15:58
  • No - column 1 has old value, column 2 has new value – Mike Feingold Oct 03 '17 at 16:04
  • @MikeFeingold how can we futher discuss this ? – xChaax Oct 03 '17 at 16:20
  • I got one column with fixed value. So everytime I want to update I can refer to that field and update others records which related to it. Right ? – xChaax Oct 04 '17 at 05:25

1 Answers1

0

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.

xChaax
  • 193
  • 5
  • 27