Skip to main content

Notifications

Announcements

No record found.

Service | Customer Service, Contact Center, Fie...
Answered

Xrm.WebApi.retrieveMultipleRecords doesn't work

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?
  • EaredGen2 Profile Picture
    EaredGen2 on at
    RE: Xrm.WebApi.retrieveMultipleRecords doesn't work

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

    Now it works correctly. Thank you!

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Xrm.WebApi.retrieveMultipleRecords doesn't work

    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.

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