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

FetchXML to query records from N:N relationship

(0) ShareShare
ReportReport
Posted on by 363

I'm creating a JS function which does some things depending on whether the target entity "Collection"  is associated to active records of another entity "Library". The entities are associated by an N:N relationship.

I wrote the following fetchXML, but I'm not sure if it's the right approach to query a N:N relationship, can you help me to check it please? 

   
        // I look for "stores"...
        
        
        // ...which are associated to "collections" 
        // ("from" indicates the entity I'm querying for,
        // "to" indicates the attribute of the other entity in the relation)
        ...
            
                
                    
                
            
        
    

I stored the fetchXML query inside a variable which I'm gonna use to filter the result and set the conditions: 

function myFunction(executionContext) {

    var formContext = executionContext.getFormContext();
    
    // I store the value for the id of this collection:
    var collectionid = executionContext.getAttribute("collectionid").getValue();
   
   // I sotre the value if the FetchXML inside a variable:
   var resultStores = " 
   
        
        
            
                
                    
                
            
        
     "
    
    foreach (s in resultStores){
        // some actions here
    }
    else{
        // show alert
    }
    
    }

I have the same question (0)
  • Suggested answer
    prt33k Profile Picture
    6,907 on at

    Hi Joel,

    I am not very clear with the question however I do see some issues :

    1. The prefix 'ava' is missing in most places.

    2. the comparison with collectionid is not clear - if it is just a single value you can write the variable name directly. If it is multiple values then you need to use 'In' and not 'eq'.

    3. statecode is not available on the N:N relationship. If it is on the store entity, you need to add a new filter. If it on collection then the new link entity needs to be added.

    I believe code should be more like this :

    function myFunction(executionContext) {
    
        var formContext = executionContext.getFormContext();    
        // I store the value for the id of this collection:
        var collectionid = executionContext.getAttribute("ava_ecncylopediaid").getValue().replace(/[{}]/g, '');   
       // I sotre the value if the FetchXML inside a variable:
        var resultStores = " "  
            ""  
    		    "" 
    				"" 
    			"" 
    			"" 
            		""  
                        "" 
    					"" 
    						"" 
    					"" 
    				"" 
    			"" 
    		"" 
    	"";
    
    	var serverURL = formContext.context.getClientUrl();
    	var fetch = encodeURI(resultStores);
    	var entityname = "ava_store";
    	var Query = entityname   "?fetchXml="   fetch;
    	var req = new XMLHttpRequest();
    
    	req.open("GET", serverURL   "/api/data/v9.1/"   Query, 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) {
    			this.onreadystatechange = null;
    			if (this.status === 200) {
    				var result = JSON.parse(this.response);
    				 //foreach
    			}
    		}
    	}
    	req.send();
    }

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
Martin Dráb Profile Picture

Martin Dráb 47 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

#3
Pallavi Phade Profile Picture

Pallavi Phade 32

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans