web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / Patch a lookup column ...
Power Pages
Suggested Answer

Patch a lookup column using ajax

(1) ShareShare
ReportReport
Posted on by 6
 Hi,
I'm trying to create records in a dataverse table using ajax, as explained in this guide : Portals Web API write, update, and delete operations | Microsoft Learn
(function(webapi, $){
		function safeAjax(ajaxOptions) {
			var deferredAjax = $.Deferred();
	
			shell.getTokenDeferred().done(function (token) {
				// add headers for AJAX
				if (!ajaxOptions.headers) {
					$.extend(ajaxOptions, {
						headers: {
							"__RequestVerificationToken": token
						}
					}); 
				} else {
					ajaxOptions.headers["__RequestVerificationToken"] = token;
				}
				$.ajax(ajaxOptions)
					.done(function(data, textStatus, jqXHR) {
						validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve);
					}).fail(deferredAjax.reject); //AJAX
			}).fail(function () {
				deferredAjax.rejectWith(this, arguments); // on token failure pass the token AJAX and args
			});
	
			return deferredAjax.promise();	
		}
		webapi.safeAjax = safeAjax;
	})(window.webapi = window.webapi || {}, jQuery)

	webapi.safeAjax({
		type: "POST",
		url: "/_api/accounts",
		contentType: "application/json",
		data: JSON.stringify({
			"name": "Sample Account"
		}),
		success: function (res, status, xhr) {
      //print id of newly created table record
			console.log("entityID: "+ xhr.getResponseHeader("entityid"))
		}
	});
When inserting data in a lookup column, I'm using this format :
JSON.stringify({
    "{column_name}@odata.bind": "/{table_set_name}({record_GUID})"
})
It worked for my 1st lookup column, but for the others I'm getting an error 400 (Bad Request). I have checked table permissions and webroles and everything should be in order.
Any ideas about what could be going wrong ?
Categories:
I have the same question (0)
  • Suggested answer
    DP_Prabh Profile Picture
    341 on at
    Please try this 
     
    var record = {};
    record["primarycontactid@odata.bind"] = "/contacts(a6a4e6ac-7f2e-f111-88b4-6045bdea630b)"; // Lookup value
    webapi.safeAjax({
        type: "PATCH",
        contentType: "application/json",
        url: "/_api/accounts(a1b9c7c3-26db-f011-8543-6045bdea630b)",       /// account guid id (target table where you are updating)
        data: JSON.stringify(record),
        success: function (data, textStatus, xhr) {
            console.log("Record updated");
        },
        error: function (xhr, textStatus, errorThrown) {
            console.log(xhr);
        }
    });
     
     

    One more thing — I’m not completely sure, but if the code above still doesn’t work, please enable the Contact Web API in the Site Settings and try again. I believe that might resolve the issue.
    I hope this works for you!

  • Suggested answer
    DP_Prabh Profile Picture
    341 on at
  • Suggested answer
    rezarizvii Profile Picture
    123 on at
    Hi,
     
    If this worked for one table but failed for another even though you have double-checked the permissions, it's likely more of a configuration problem than a wrong code or missing permissions.
     
    Tell me this, have you enabled the Web API for the new table you're targeting in Site Settings?
     
    For any table where we want to perform CRUD operations, it must be explicitly enabled in the site's settings.
    For each table, please add the following two settings:
    1. webapi/<table-name>/enabled: true
    2. webapi/<table-name>/fields: *
     
    To add these site settings, follow these steps:
    • Navigate to your site and click the (...) button in the sidebar.
    • Open the Power Pages Management app.
    • In the model-driven app, go to Site Settings from the sidebar.
    • Click + New to create a new setting.
      • Make sure the setting name matches exactly as specified above.
      • Select the appropriate website from the dropdown.
     
    You can refer to this documentation for more information: How to: Use the portal Web API | Microsoft Learn
     
    If this reply helped you in any way, please give it a Like 💜 and in case it resolved your issue, please mark it as the Verified Answer ✅.
  • Suggested answer
    rezarizvii Profile Picture
    123 on at
    Hi,
     
    If it worked for one table but failed for another and you have already double-checked the permissions and web roles, it is more likely a wrong configuration error than a wrong code or missing permissions error.

    Tell me this, have you enabled the Web API for the new table you're targeting in Site Settings?

    For any table where we want to perform CRUD operations, it must be explicitly enabled in the site's settings.
    For each table, you need to add the following two settings:
    • webapi/<table-name>/enable: true
    • webapi/<table-name>/fields: *


    To add these site settings, follow these steps:

    1. Navigate to your site and click the (...) button in the sidebar.
    2. Open the Power Pages Management app.
    3. In the model-driven app, go to Site Settings from the sidebar.
    4. Click + New to create a new setting.
      • Make sure the setting name matches exactly as specified above.
      • Select the appropriate website from the dropdown.
     
    If this reply helped you in any way, please give it a Like 💜 and in case it resolved your issue, please mark it as the Verified Answer ✅.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Pages

#1
DP_Prabh Profile Picture

DP_Prabh 51

#2
rezarizvii Profile Picture

rezarizvii 35

#2
oliver.rodrigues Profile Picture

oliver.rodrigues 35 Most Valuable Professional

Last 30 days Overall leaderboard