web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Answered

Xrm.WebApi.retrieveMultipleRecords doesn't work

(0) ShareShare
ReportReport
Posted on by

Hello

I need to disable a field on a form using JS if a current user isn't member of some specific team.

I wrote this code:

disableField(executionContext){
        
        var formContext = executionContext.getFormContext();    
        var currentUserId = formContext._globalContext._userSettings.userId;
        var fetchResult;
        
        
        var fetchXML = "?<fetch mapping='logical' version='1.0'>"
        +"<entity name='team'>"
        +  "<attribute name='name'/>"
        +  "<filter>"
        +   "<condition attribute='systemuserid' operator='eq' value='{D6470EA5-16D7-EB11-BACB-000D3A1F9C1B}' />"
        +    "<condition attribute='name' operator='eq' value='Private access to notes' />"
        +  "</filter>"
        +  "<link-entity name='teammembership' from='teamid' to='teamid' link-type='inner'/>"
        +"</entity>"
        +"</fetch>";

        var fetchXML2 = `
        ?fetchXml=
        <fetch mapping='logical' version='1.0'>
            <entity name='team'>
                <attribute name='name'/>
                    <filter>
                        <condition attribute='systemuserid' operator='eq' value=${currentUserId} />
                        <condition attribute='name' operator='eq' value='Private access to notes' />
                    </filter>
                <link-entity name='teammembership' from='teamid' to='teamid' link-type='inner'/>
            </entity>
        </fetch>
        `; 


        Xrm.WebApi.retrieveMultipleRecords("team"fetchXML).then(
            function success(result) {
                if(result.entities.length > 0){
                    debugger;
                    formContext.getControl("description").setDisabled = true;
                }
            },
            function (error) {
                console.log(error.message);
            }
        );
        alert(fetchResult);
    },
 
Unfortunatelly, I couldn't understand why it doesn't work. All code (including debugger) doesn't work in condition block. Which problems with executing do I have? Also I don't know how to write fetchXML in such way as split fetch by lines and have opportunity to use construction like this: ${currentUserId}. If I use fetch2 so I have error when I load contact form. How can I solve these problems?
I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    Hi,

    If you want to construct fetch xml in the form of fetchxml2 and use ${currentUserId}, you can refer to my code:

    function disableField(executionContext){
                    
                    var formContext = executionContext.getFormContext();    
                    var currentUserId = Xrm.Utility.getGlobalContext().userSettings.userId;
                    var name = "lisi";
                    
                    var fetchXML = `
                      
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                      
                                    
                      
                    `; 
                    
                    fetchXML = "?fetchXml="   encodeURIComponent(fetchXML);
                    
                    Xrm.WebApi.retrieveMultipleRecords("account", fetchXML).then(
                                    function success(result) {
                                                    if(result.entities.length > 0){
                                                                    alert("success:"  result.entities);
                                                    }else{
                                                                    alert("none");
                                                    }
                                    },
                                    function (error) {
                                                    console.log(error.message);
                                                    alert(error.message);
                                    }
                    );
    }
    

    Regarding the conditional statement not being executed, you can check the following three points:

    1. When I use `formContext._globalContext._userSettings.userId;` to get `uerid`, there is an error, please check your side, if you can’t get it successfully, use the method in my code to get it.
    2. Check if your xml is constructed correctly, you can download XrmToolBox: https://www.xrmtoolbox.com/, and use the FetchXml Builder plug-in to construct your fetchxml.
    3. Check if result.entities.length is greater than 0.
  • EaredGen2 Profile Picture
    on at

    I had problems with xml and didn't use encodeURIComponent :)

    Now it works correctly. Thank you!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
11manish Profile Picture

11manish 36

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 32 Most Valuable Professional

#2
Mallesh Deshapaga Profile Picture

Mallesh Deshapaga 32

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans