다음을 통해 공유


Upsert를 사용하여 레코드 삽입 또는 업데이트

 

게시 날짜: 2016년 11월

적용 대상: Dynamics CRM 2015

UpsertRequest 메시지를 사용하여 데이터 통합 시나리오와 관련된 복잡성을 줄일 수 있습니다. 대량 데이터 통합 시나리오처럼 외부 시스템에서 Microsoft Dynamics 365으로 데이터를 로드할 때 Dynamics 365에 레코드가 이미 존재하는지 알지 못할 수 있습니다. 이런 경우 UpdateRequest 또는 CreateRequest 작업을 호출하면 모를 수 있습니다. 따라서 해당 작업을 수행하기 전에 존재 유무를 확인하기 위해 먼저 레코드를 쿼리합니다. 이제 새로운 UpsertRequest(업데이트 또는 삽입) 메시지를 사용하여 이런 복잡성을 줄이고 Dynamics 365으로 보다 효율적으로 로드할 수 있습니다.

경고

Microsoft Dynamics CRM Online 조직의 경우 이 기능은 조직이 Dynamics CRM Online 2015 업데이트 1로 업데이트된 경우에만 사용할 수 있습니다. 이 기능은 Dynamics CRM(온-프레미스)에서는 사용할 수 없습니다.

이 항목의 내용

Upsert 사용

Upsert 프로세스 이해

샘플 코드

Upsert 사용

레코드가 존재하는지 확실하지 않은 경우에만 UpsertRequest을 사용하는 것이 가장 좋습니다. 즉, CreateRequest 또는 UpdateRequest 작업을 호출해야 하는지 확실하지 않을 때 사용합니다.CreateRequest를 사용하는 것보다 UpsertRequest를 사용할 때 성능이 저하됩니다. 레코드가 존재하지 않는 것이 확실한 경우 CreateRequest를 사용합니다.

UpsertRequest에는 Target이라는 속성이 포함되어 있습니다. 이 속성은 UpdateRequest 또는 CreateRequest 작업에서 사용할 엔터티 정의를 포함합니다. 또한 레코드가 존재하지 않는 경우 레코드를 만들 수 있도록 대상 엔터티 유형에 대해 CreateRequest에 필요한 모든 특성도 포함합니다.

레코드가 만들어졌는지 확인하기 위해 RecordCreated를 검사할 수 있습니다. 레코드가 존재하지 않고 만들어지지 않은 경우 RecordCreated는 true가 됩니다. 레코드가 이미 존재하고 업데이트된 경우 false가 됩니다.Target는 존재하는 레코드와 만들어진 레코드에 대해 EntityReference가 됩니다.

다음 코드 조각은 UpsertRequest를 사용하는 방법을 보여줍니다.

UpsertRequest가 작동하는 방식을 이해하려면 다음 섹션을 참조하십시오.

Upsert 프로세스 이해

다음 단계는 UpsertRequest가 수신되면 처리 논리를 설명합니다.

  1. 작업을 만들거나 삽입하기에 충분한 데이터와 함께 UpsertRequest를 보냅니다.

  2. Microsoft Dynamics 365에서는 대상 엔터티를 대상으로 하는 레코드를 조회합니다.

  3. 레코드가 이미 있는 경우:

    1. 발견된 레코드의 ID를 사용하여 대상 엔터티의 ID 속성을 설정합니다.

    2. 업데이트를 호출합니다.

    3. RecordCreatedfalse로 설정합니다.

    4. 업데이트의 대상 엔터티에서 EntityReferenceTarget에 대한 값으로 만듭니다.

    5. UpsertResponse로 돌아갑니다.

  4. 레코드가 존재하지 않는 경우:

    1. 대체 키 값을 대상 엔터티 특성에 복사합니다.

    2. Create를 호출합니다.

    3. RecordCreatedtrue로 설정합니다.

    4. 대상 엔터티 유형의 EntityReferenceCreate 요청의 ID 결과를 Target에 대한 값으로 만듭니다.

    5. UpsertResponse로 돌아갑니다.

다음 그림은 UpsertRequest가 수신될 때 수행되는 프로세스를 보여줍니다.

upsert 프로세스 흐름

샘플 코드

Upsert를 사용하여 레코드 삽입 또는 업데이트 샘플 ProductUpsertSample.cs 파일에는 새 레코드를 만들거나 기존 레코드를 업데이트하기 위해 XML 파일의 콘텐츠에 UpsertRequest 메시지를 적용하기 위한 다음 ProcessUpsert 메서드가 포함되어 있습니다.


public void ProcessUpsert(String Filename)
{
    Console.WriteLine("Executing upsert operation.....");
    XmlTextReader tr = new XmlTextReader(Filename);
    XmlDocument xdoc = new XmlDocument();
    xdoc.Load(tr);
    XmlNodeList xnlNodes = xdoc.DocumentElement.SelectNodes("/products/product");

    foreach (XmlNode xndNode in xnlNodes)
    {
        String productCode = xndNode.SelectSingleNode("Code").InnerText;
        String productName = xndNode.SelectSingleNode("Name").InnerText;
        String productCategory = xndNode.SelectSingleNode("Category").InnerText;
        String productMake = xndNode.SelectSingleNode("Make").InnerText;

        //use alternate key for product
        Entity productToCreate = new Entity("sample_product", "sample_productcode", productCode);

        productToCreate["sample_name"] = productName;
        productToCreate["sample_category"] = productCategory;
        productToCreate["sample_make"] = productMake;
        UpsertRequest request = new UpsertRequest()
        {
            Target = productToCreate
        };

        try
        {
            // Execute UpsertRequest and obtain UpsertResponse. 
            UpsertResponse response = (UpsertResponse)_serviceProxy.Execute(request);
            if (response.RecordCreated)
                Console.WriteLine("New record {0} is created!", productName);
            else
                Console.WriteLine("Existing record {0} is updated!", productName);
        }

        // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
        catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
        {
            throw;
        }

    }
    // Prompts to view the sample_product entity records.
    // If you choose "y", IE will be launched to display the new or updated records.
    if (PromptForView())
    {
        ViewEntityListInBrowser();
    }

}

참고 항목

변경 내용 추적을 사용하여 데이터를 외부 시스템과 동기화
엔터티에 대한 대체 키 정의
대체 키를 사용하여 레코드 만들기

© 2017 Microsoft. All rights reserved. 저작권 정보