Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Custom JavaScript for N:N Relationship.

Posted on by Microsoft Employee

I have written a JS Code for retrieving data from N: N Relationship.

The code is given below:-

var fetchXml = "";

function filterLookup()
{
debugger;
//alert("Filter Lookup");
var checkValue = new Array();
checkValue = Xrm.Page.getAttribute("smp_country").getValue();
alert("Check Value:- " +checkValue[0].name );
// if(Xrm.Page.getControl("smp_country")!=null && Xrm.Page.getControl("smp_country")!= undefined)

if(checkValue[0].name!=null && checkValue[0].name!= undefined)

{
Xrm.Page.getControl("smp_account_type").removePreSearch(PopulateAccountType);
Xrm.Page.getControl("smp_account_type").addPreSearch(PopulateAccountType); 

else 
{
Xrm.Page.getControl("smp_account_type").removePreSearch(PopulateAccountType);
}

function PopulateAccountType()
{
debugger;
//if(Xrm.Page.getControl("smp_country")!=null && Xrm.Page.getControl("smp_country")!= undefined)

if(checkValue[0].name!=null && checkValue[0].name!= undefined)
{

var countryID = Xrm.Page.getAttribute("smp_country").getValue();
var countryGUID = countryID [0].id;

alert("GUID :- "+ countryGUID ); 
SDK.REST.retrieveMultipleRecords("smp_smp_accounttype_smp_country", "$select=smp_accounttypeid&$filter=smp_countryid eq guid'"+countryGUID +"'", retrieveAccountTypes, errorCallBack, retrieveAccountTypesComplete); 

Xrm.Page.getControl("smp_account_type").addCustomFilter(fetchXml);
}

function retrieveAccountTypesComplete() {

}

function errorCallBack(error) 
{
alert(error.message);
}

function retrieveAccountTypes(account_Type) 
{
fetchXml = "<filter type='and'><condition attribute='smp_accounttypeid' operator='in'>"
for(var i=0; i<account_Type.length; i++)
{
var filterData = "<value>"+account_Type[i].smp_accounttypeid+"</value>";
fetchXml= fetchXml.concat(filterData );
}

var data = "</condition></filter>"
fetchXml=fetchXml.concat(data);
}

The issue I am Facing that the Filtering of account_type is delaying after second click.

The filtering is not done in the first attempt.

I need help on this.

Thanks in advance.

*This post is locked for comments

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,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans