Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

$ref error Associating record in a Many-to-Many relationship Web API

Posted on by 9
I'm receiving the following error: The URI segment '$ref' is invalid after the segment <relationshipName>. I'm using the following Associate and disassociate table rows using the Web API (Microsoft Dataverse) - Power Apps | Microsoft Learn as an example for my call.  
 
My Code is below.  I've paused in the debugger to make sure on the call that the variables relatedEntitySetName, primaryEntitySetName, relationshipName, and the ids are set correctly.  Any idea what I may be doing wrong?
 
    var association = { '@odata.id': formContext.context.getClientUrl() + /api/data/v9.2/ + relatedEntitySetName + ( + relatedId + ) };
 
    var req = new XMLHttpRequest();
    req.open(POST, formContext.context.getClientUrl() + /api/data/v9.2/ + primaryEntitySetName + ( + primaryId + )/ + relationshipName + /$ref, true);
    req.setRequestHeader(Accept, application/json);
    req.setRequestHeader(Content-Type, application/json; charset=utf-8);
    req.setRequestHeader(If-None-Match, null);
    req.setRequestHeader(OData-MaxVersion/, 4.0);
    req.setRequestHeader(OData-Version, 4.0);
 
 
Apparently quotes are removed during posting.  Assume they're in their proper places. 
  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    $ref error Associating record in a Many-to-Many relationship Web API
    You can use my tool Dataverse REST Builder to generate the exact code based on your instance, one of the request type is "Associate"
  • haseem Profile Picture
    haseem 25 on at
    $ref error Associating record in a Many-to-Many relationship Web API
    It seems you might have a formatting issue in your URI construction. 
    Ex...
    var association = { '@odata.id': formContext.context.getClientUrl() + "/api/data/v9.2/" + relatedEntitySetName + "(" + relatedId + ")" };
    var req = new XMLHttpRequest();
    req.open("POST", formContext.context.getClientUrl() + "/api/data/v9.2/" + primaryEntitySetName + "(" + primaryId + ")/" + relationshipName + "/$ref", true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("If-None-Match", null);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");

     
     
  • Verified answer
    Trent Profile Picture
    Trent 9 on at
    $ref error Associating record in a Many-to-Many relationship Web API
    Solved it. There are three things that you need to make sure of:
    1.) Give the relationship a name that is less than the max. Don't let the system truncate the name for you. (This is what was getting me)
    2.) Make sure you're using the relationship name, not the related table name (I tried both)
    3.) It's case sensitive.

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans