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

(0) ShareShare
ReportReport
Posted on by 4
 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

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!

Leaderboard > Power Pages

#1
DP_Prabh Profile Picture

DP_Prabh 41

#2
oliver.rodrigues Profile Picture

oliver.rodrigues 35 Most Valuable Professional

#3
rezarizvii Profile Picture

rezarizvii 28

Last 30 days Overall leaderboard