简体   繁体   中英

Dynamic 365 - Unable to update related entity data in Dynamics crm from Json file

My Requirement is to update the Dynamics CRM Account entity based on the JSON input. For the direct field in Account I am able to update them, but I'm struggling with the related entities.

The JSON :

{
  "id": "C76B4E57-5BAC-EA11-A812-000D3AD773FF",
  "name": "3000005170",
  "description": "\"\"FISHING-TV\" LLC Support",
  "status": "Active",
  "statusReason": "Active",
  "engagedParty": {
    "id": "ae8e617a-45ac-ea11-a812-000d3ad773ff",
    "name": "\"\"FISHING-TV\" LLC",
    "@referredType": "Organization"
  },
  "agreement": [
    {
      "id": "7a94566e-73b1-ea11-a812-000d3ab958aa",
      "name": "201498-0000 <D365 UI: Contract.SES TN DB: opportunity.ses_sestn Comment: >"
    }
  ]
}

My Model Class:

public class AccountExtranet {
  public string id {
    get;
    set;
  }
  public string name {
    get;
    set;
  }
  public string description {
    get;
    set;
  }
  public string status {
    get;
    set;
  }
  public string statusReason {
    get;
    set;
  }
  public Engagedparty3 engagedParty {
    get;
    set;
  }
  public List <Agreement3> agreement {
    get;
    set;
  }
  public List <Characteristic5> characteristic {
    get;
    set;
  }
  public List <Contactmedium3> contactMedium {
    get;
    set;
  }
  public ValidforExt validFor {
    get;
    set;
  }
}

Class file through which I am updating

private static void UpdateAccount(CrmServiceClient crmclient, List <Account> allAccount, AccountExtranet extranetAccount, TraceWriter log) {
  Account account = allAccount.FirstOrDefault();
  account.Name = extranetAccount.description;
  AccountState statecode;
  bool statusExist = Enum.TryParse <AccountState> (extranetAccount ? .status, out statecode);
  if (statusExist) {
    account.StateCode = statecode;
  }
}

There is no direct way to update all the child records along with parent record in single transaction (deep update), though we have an option to do deep insert using web api.

So you have to initialize each entity object separately and set the attribute values to be updated along with record Id (GUID) to update each child record.

var account = new Entity("account");
account.Id = new Guid("C76B4E57-5BAC-EA11-A812-000D3AD773FF");
// set attribute values
account["name"] = "3000005170";
account["description"] = "FISHING-TV LLC Support";

//Update the account
service.Update(account);

var agreement = new Entity("agreement");
agreement.Id = new Guid("7a94566e-73b1-ea11-a812-000d3ab958aa");
// set attribute values
agreement["name"] = "201498-0000 <D365 UI: Contract.SES TN DB: opportunity.ses_sestn Comment: >";

//Update the agreement 
service.Update(agreement);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM