Share via


XRM ツールを使用してデータを更新

公開日: 2017年1月

対象: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online

Microsoft Dynamics 365 でデータを更新するために、CrmServiceClient クラスには、UpdateEntityUpdateStateAndStatusForEntity の 2 つのメソッドが用意されています。

XRM ツール API を使用し更新操作にはデータ ペイロードが必要です。 データ ペイロードは Dictionary<string, CrmDataTypeWrapper> オブジェクトの形式をとります。CrmDataTypeWrapper を使用して、参照するデータ ポイントに対してどのような処理を適用する必要があるか、インターフェイスに通知します。

UpdateEntity

これは、レコードのステータスや状態の設定を除いて、Dynamics 365 でレコードを更新するための anchor メソッドです。 これを使用するには、いくつかの情報を知る必要があります。更新対象のエンティティのスキーマ名、更新するエンティティの主キー フィールド、更新するレコードの GUID、および更新に使用するデータ ペイロードの配列がそれです。

CrmServiceClient crmSvc = new CrmServiceClient(new System.Net.NetworkCredential("<UserName>", "<Password>", “<Domain>”),"<Server>", "<Port>", "<OrgName>");

// Verify that you are connected
if (crmSvc != null && crmSvc.IsReady)
{
    //Display the CRM version number and org name that you are connected to
    Console.WriteLine("Connected to CRM! (Version: {0}; Org: {1}", 
    crmSvc.ConnectedOrgVersion, crmSvc.ConnectedOrgUniqueName);

    // Update the account record
    Dictionary<string, CrmDataTypeWrapper> updateData = new Dictionary<string, CrmDataTypeWrapper>();
    updateData.Add("name", new CrmDataTypeWrapper("Updated Sample Account Name", CrmFieldType.String));
    updateData.Add("address1_city", new CrmDataTypeWrapper("Boston", CrmFieldType.String));
    updateData.Add("telephone1", new CrmDataTypeWrapper("555-0161", CrmFieldType.String)); 
    bool updateAccountStatus = crmSvc.UpdateEntity("account","accountid",_accountId,updateData);

    // Validate if the account record was updated successfully, and then display the updated information
    if (updateAccountStatus == true)
    {
        Console.WriteLine("Updated the account details as follows:");
        Dictionary<string, object> data = crmSvc.GetEntityDataById("account", accountId, null);
        foreach (var pair in data)
        {
            if ((pair.Key == "name") || (pair.Key == "address1_city") || (pair.Key == "telephone1"))
            {
                Console.WriteLine(pair.Key.ToUpper() + ": " + pair.Value);
            }
        }
    }
}
else
{
    // Display the last error.
    Console.WriteLine("An error occurred: {0}", crmSvc.LastCrmError);

    // Display the last exception message if any.
    Console.WriteLine(crmSvc.LastCrmException.Message);
    Console.WriteLine(crmSvc.LastCrmException.Source);
    Console.WriteLine(crmSvc.LastCrmException.StackTrace);

    return;
}

UpdateStateAndStatusForEntity

このメソッドは、Dynamics 365 でレコードの状態を設定するために使用します。 たとえば、通常、すべてのレコードは "オープン" 状態で起動されます。 状態の名前は、レコードの種類に基づいて、もしくは開発者の選択によって変更されます。 たとえば、見積もりは、[下書き][アクティブ][クローズ][失注][受注] という複数の状態とステータスをとります。

ヒント

SDK ダウンロード パッケージの SDK\SampleCode\CS\HelperCode フォルダーの OptionSets.cs ファイルを使用して、Dynamics 365 のさまざまなエンティティで使用できるグローバル オプション セットを表示および使用できます。 グローバル オプション セットの詳細については、「グローバル オプション セットのカスタマイズ」を参照してください。

エンティティの状態を更新するには、対象の状態とステータスを名前または ID のいずれかで認識することが必要です。 ID と名前のどちらも、エンティティのメタデータをクエリし、ステータス フィールドと状態フィールドを調べることによって確認することができます。 この例では、取引先企業レコードのステータスを [非アクティブ] に設定する方法を示します。

CrmServiceClient crmSvc = new CrmServiceClient(new System.Net.NetworkCredential("<UserName>", "<Password>", “<Domain>”),"<Server>", "<Port>", "<OrgName>");

// Verify that you are connected
if (crmSvc != null && crmSvc.IsReady)
{ 
    //Display the CRM version number and org name that you are connected to
    Console.WriteLine("Connected to CRM! (Version: {0}; Org: {1}",
    crmSvc.ConnectedOrgVersion, crmSvc.ConnectedOrgUniqueName);

    // Here are the state and status code values
    // statecode = 1 ( Inactive ) 
    // statuscode = 2 ( Inactive ) 

    crmSvc.UpdateStateAndStatusForEntity("account" , accountId , 1 , 2 );

    // the same command using the second form of the method
    crmSvc.UpdateStateAndStatusForEntity("account" , accountId , "Inactive" , "Inactive");
}
else
{
    // Display the last error.
    Console.WriteLine("An error occurred: {0}", crmSvc.LastCrmError);

    // Display the last exception message if any.
    Console.WriteLine(crmSvc.LastCrmException.Message);
    Console.WriteLine(crmSvc.LastCrmException.Source);
    Console.WriteLine(crmSvc.LastCrmException.StackTrace);

    return;
}

関連項目

サンプル: XRM ツール API のクイック スタート
CrmServiceClient コンストラクターを使用した Dynamics 365 への接続
XRM を使用して Dynamics 365 でアクションを実行
属性メタデータに関する作業

Microsoft Dynamics 365

© 2017 Microsoft. All rights reserved. 著作権