When working with Power Pages, updating lookup columns can be challenging, especially when dealing with custom forms and client-side operations. In this article, we’ll walk through how to create or update a lookup column in a Dataverse table using the Web API, with clear examples and best practices.

Prerequisites

Configure Site Settings for Web API & Tables

Assign Web Roles and Table Permissions

Make sure the appropriate web roles and table permissions are configured:

Special Permissions to both tables

  1. Customers Table (Parent)

Screenshot (80)
  1. Service Request Table (Child )

Screenshot (79)

Tables used in Demo:

Screenshot (84)Screenshot (85)

UI Design for Demo

Screenshot (86)

Code Example of Save / Update Lookup Record

function saveRecords() {
    const record = {
        "cr399_requesttitle": requestTitle.value,
        "cr399_description": description.value,
        "[email protected]": `/cr399_customerses(${customerId})`
    }

    console.log(requestId)

    const type = requestId ? "PATCH" : "POST"
    const url = requestId ? `/_api/cr399_servicerequestses(${requestId})` : "/_api/cr399_servicerequestses"

    webapi.safeAjax({
        type: type,
        url: url,
        contentType: "application/json",
        data: JSON.stringify(record),
        success: function (res, status, xhr) {
            requestId = xhr.getResponseHeader("EntityId")
            console.log(res)
            form.reset();
            getRecords()
        }
    })
}

This function creates or updates a Service Request record and assigns a Customer lookup using the Power Pages Web API, based on whether a record ID already exists.

1. Prepare the data to be saved

const record = {
    "cr399_requesttitle": requestTitle.value,
    "cr399_description": description.value,
    "[email protected]": `/cr399_customerses(${customerId})`
}

- It links the Service Request to an existing Customer record.

- The value passed is the Customer record GUID.

  1. Decide whether to create or update a record

const type = requestId ? "PATCH" : "POST"
const url = requestId
    ? `/_api/cr399_servicerequestses(${requestId})`
    : "/_api/cr399_servicerequestses"

If requestId exists:

If requestId does not exist:

This logic allows the same function to handle both create and update operations.

3. Send the request using Web API

webapi.safeAjax({
    type: type,
    url: url,
    contentType: "application/json",
    data: JSON.stringify(record),

4. Handle the successful response

success: function (res, status, xhr) {
    requestId = xhr.getResponseHeader("EntityId")
    form.reset();
    getRecords()
}

Github Repo : https://github.com/ParthivSuthar/Power-Pages-Lookup-Demo

Conclusion

In this article, we walked through a practical example of creating and updating a Service Request record with a Customer lookup using the Power Pages Web API.