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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

How to delete lookup/relationship with webapi?

(0) ShareShare
ReportReport
Posted on by 40

So, I followed some posts suggesting how to delete a lookup from an entity via webapi. But I can't get it to work myself.

Scenario: On opportunity I have created a lookup to a custom entity. Now I want to delete/set to null the lookup to the custom entity. 

As I have understood it by reading online it is not possible to set the lookup to null via an "webapi.update", instead a "webapi.delete" needs to be used.

The error I get is:

The URI segment '$ref' is invalid after the segment 'new_umbrellaopportunity_opportunity_umbrellaopportunity({83740A6A-8C43-EB11-A812-0022489A2750})

The command I send to the delete command:

https://mercuridev.crm4.dynamics.com/api/data/v9.1/opportunities(3242f616-1437-4545-afb0-e5a555240854)/new_umbrellaopportunity_opportunity_umbrellaopportunity({83740A6A-8C43-EB11-A812-0022489A2750})/$ref

The javascript code:

let deleteCommand = Xrm.Page.context.getClientUrl()   _webApiUrl   "opportunities"   "("   recordId   ")/new_umbrellaopportunity_opportunity_umbrellaopportunity("   umbrellaId   ")/$ref"; 

            req.open("DELETE", deleteCommand, false);

            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.setRequestHeader("OData-MaxVersion", "4.0");
            req.setRequestHeader("OData-Version", "4.0");

            req.onreadystatechange = function () {

                if (this.readyState === 4) {
                    req.onreadystatechange = null;
                    if (this.status === 204 || this.status === 1223) {
                    }
                }
            };

            req.send();

I have the same question (0)
  • Suggested answer
    Pawar Pravin  Profile Picture
    5,237 on at

    Please refer below blog for details, its working fine for me:

    pravinpawarweb.wordpress.com/.../

  • Fedorov Profile Picture
    40 on at

    That is one of the pages I have seen and tried.. In my eyes I'm doing it exactly like that.. :/

    Still get the error.

  • Suggested answer
    Pawar Pravin  Profile Picture
    5,237 on at

    May be you are missing false/true parameter in your code. Can you please check once ?

  • Fedorov Profile Picture
    40 on at

    I'm not quite sure which true/false that could be? Could you give me a hint?

  • Wahaj Rashid Profile Picture
    11,321 on at

    Hi,

    Can you try removing curly braces ({})from the umbrellaId?

    Best,

    Wahaj

  • Fedorov Profile Picture
    40 on at

    Hello Wahaj,

    Good idea, I tried it now, added this line of code after the declaration of "deleteCommand".

    deleteCommand = deleteCommand.replace("{", "").replace("}", "");

    I could see that the braces were removed but I got the exact same error unfortunately..

  • Fedorov Profile Picture
    40 on at

    Hey guys, I think I solved it.

    I had to "turn the entities around" like this:

    let deleteCommand = Xrm.Page.context.getClientUrl()   _webApiUrl   "new_umbrellaopportunities("   umbrellaId   ")/new_umbrellaopportunity_opportunity_umbrellaopportunity("   opportunityId   ")/$ref";

    So, at first I had the "opportunity" before the relationship name and I sent the opportunityId as the relationship guid. (giving the error)

    Now, I put my custom entity first, put the custom entity guid as the Id and for the relationship I pass the opportunityId.

    So it was like backwards from what I sort of "feel" that it should be but apparently not.. 

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
#ManoVerse Profile Picture

#ManoVerse 101

#2
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 66 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans