Skip to main content

Notifications

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

FetchXML to query records from N:N relationship

Posted on by 355

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

  • Suggested answer
    prt33k Profile Picture
    prt33k 6,907 on at
    RE: FetchXML to query records from N:N relationship

    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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans