Life is Like a Boat

忘備録や経済、投資、プログラミングに関するメモやtipsなど

Dataverse Web APIで外部キーを使ってレコードをUpsertする方法

SalesforceとPower Platformの二刀流で今年度は仕事しています。Salesforceの方は都心方面の事業会社で、Power Platformは埼玉の事業会社なので通勤経路が全く逆方向です。

埼玉の事業会社の方では、Power PlatformをERP的に使う方向になっています。そのため、他システム、特に会計周りのシステムから定期的にデータを抜くOne wayのデータ連携の動線を用意する必要が出てきました。Power PlatformにはDataflowsというクラウド型のETLツールがあり、市場にもノンコードのデータ連携アプリが多々存在します。しかし、1つのレコードから条件に応じて複数のレコードを作成するケースや予算的な制約もあることから

  • Serverless functionであるAzure Functions
  • ETLパイプラインとしてのPandas
  • Azure Funcの世界とPower Platformの世界を繋ぐDataverse Web API
  • ある程度の手作業(会計システムからのデータエクスポート)が柔軟性として残せる

を組み合わせて動線を用意することにしました。

Dataverse Web APIがここで肝になるわけですが、データ連携で外部キーを使ってUpsertするのは鉄板にも関わらず、Dataverse Web APIの解説には、

An upsert operation is exactly like an update. It uses a PATCH request and uses a URI to reference a specific entity. The difference is that if the entity doesn’t exist it will be created. If it already exists, it will be updated. Normally when creating a new entity you will let the system assign a unique identifier. This is a best practice. But if you need to create a record with a specific id value, an upsert operation provides a way to do this. This can be valuable in situation where you are synchronizing data in different systems.

Sometimes there are situations where you want to perform an upsert, but you want to prevent one of the potential default actions: either create or update. You can accomplish this through the addition of If-Match or If-None-Match headers. For more information, see Limit upsert operations.

docs.microsoft.com

としかなく、一番解決策に近いと思われるこちらのブログ記事でもレコードのGUIDでのUpsertを解説しているだけです。

bengribaudo.com

明確に外部キー項目がある場合はどうすればいいのか調べたところ、

{Dataverse Web API}/new_car(new_model_id='abc-1234')

外部キーをシングルクォーテーションで囲ってPATCHリクエストすればいいだけでした。ここではnew_model_idを外部キーとしています。 外部キー項目が整数の場合、シングルクォーテーションなしでもUpsertできるのですが、シングルクォーテーションがない場合、Bad Request - Error in query syntax. の400エラーとなり、これがはまりポイントです。

powerusers.microsoft.com