Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

How to delete lookup/relationship with webapi?

Posted on by 338

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();

  • Fedorov Profile Picture
    Fedorov 338 on at
    RE: How to delete lookup/relationship with webapi?

    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.. 

  • Fedorov Profile Picture
    Fedorov 338 on at
    RE: How to delete lookup/relationship with webapi?

    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..

  • Wahaj Rashid Profile Picture
    Wahaj Rashid 11,319 on at
    RE: How to delete lookup/relationship with webapi?

    Hi,

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

    Best,

    Wahaj

  • Fedorov Profile Picture
    Fedorov 338 on at
    RE: How to delete lookup/relationship with webapi?

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

  • Suggested answer
    Pawar Pravin  Profile Picture
    Pawar Pravin 5,227 on at
    RE: How to delete lookup/relationship with webapi?

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

  • Fedorov Profile Picture
    Fedorov 338 on at
    RE: How to delete lookup/relationship with webapi?

    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
    Pawar Pravin 5,227 on at
    RE: How to delete lookup/relationship with webapi?

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

    pravinpawarweb.wordpress.com/.../

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans