Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Javascript code to check Current users Team

Posted on by 245

Hi i have a requirement to check the logged in users Team on click of custom button and then change the status based on Team .

Kindly let me know if any pointers.

*This post is locked for comments

  • Shahzeb Khan Profile Picture
    Shahzeb Khan 165 on at
    RE: Javascript code to check Current users Team

    Hi Sir,

    There is error in response when i am using this code.

    kindly guide.

  • Parashuram  Profile Picture
    Parashuram 381 on at
    RE: Javascript code to check Current users Team

    Thanks Gautam ...

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Javascript code to check Current users Team

    You can also try with single request -

            function getLoginUserTeam()
            {
              
                var loggedInUserId = Xrm.Page.context.getUserId();
                loggedInUserId = loggedInUserId.replace('{', '').replace('}', '');
                var req = new XMLHttpRequest();
                req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/teams$select=name&$expand=teammembership_association($filter=systemuserid eq "+loggedInUserId+")",true);
                req.setRequestHeader("OData-MaxVersion", "4.0");
                req.setRequestHeader("OData-Version", "4.0");
                req.setRequestHeader("Accept", "application/json");
                req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                req.onreadystatechange = function() {
                    if (this.readyState === 4) {
                        req.onreadystatechange = null;
                        if (this.status === 200) {
                            var results = JSON.parse(this.response);
                            // I have not tried you can debug results part 
                            for (var i = 0; i < results.value.length; i++) {
                                var teamname = results.value[i]["name"];
                                                        
                            }
                        } else {
                            Xrm.Utility.alertDialog(this.statusText);
                        }
                    }
                };
                req.send();
            }


  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Javascript code to check Current users Team

    Try with this -

            function getLoginUserTeam()
            {
                var UserTeams = null;
                var loggedInUserId = Xrm.Page.context.getUserId();
                loggedInUserId = loggedInUserId.replace('{', '').replace('}', '');
                var req = new XMLHttpRequest();
                req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/teammemberships?$select=teamid,teammembershipid&$filter=systemuserid eq "+loggedInUserId+"", true);
                req.setRequestHeader("OData-MaxVersion", "4.0");
                req.setRequestHeader("OData-Version", "4.0");
                req.setRequestHeader("Accept", "application/json");
                req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                req.onreadystatechange = function() {
                    if (this.readyState === 4) {
                        req.onreadystatechange = null;
                        if (this.status === 200) {
                            var results = JSON.parse(this.response);
                            for (var i = 0; i < results.value.length; i++) {
                                var teamid = results.value[i]["teamid"];
                                var teammembershipid = results.value[i]["teammembershipid"];
    
                                var teamid = teamid.replace("{","").replace("}","");
                                var req = new XMLHttpRequest();
                                req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/teams("+teamid+")?$select=name,teamid", true);
                                req.setRequestHeader("OData-MaxVersion", "4.0");
                                req.setRequestHeader("OData-Version", "4.0");
                                req.setRequestHeader("Accept", "application/json");
                                req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                                req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
                                req.onreadystatechange = function() {
                                    if (this.readyState === 4) {
                                        req.onreadystatechange = null;
                                        if (this.status === 200) {
                                            var result = JSON.parse(this.response);
    
                                            // You will get the team id and team name here
                                            var name = result["name"]; // Team Name
                                            var teamid = result["teamid"];
    
                                            if(UserTeams == null)
                                                UserTeams  =  name;
                                            else
                                                UserTeams = UserTeams + "," +  name;
    
    
                                        } else {
                                            Xrm.Utility.alertDialog(this.statusText);
                                        }
                                    }
                                };
                                req.send();
                            }
                        } else {
                            Xrm.Utility.alertDialog(this.statusText);
                        }
                    }
                };
                req.send();
    
    
            }


  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Javascript code to check Current users Team

    Hi Divya ,

    Check below old post here .

    community.dynamics.com/.../218804

    In addition I would suggest to download CRMRESTBuilder and prepare same code with web api instead of using organization service .

  • DivyaBharathi Profile Picture
    DivyaBharathi 245 on at
    RE: Javascript code to check Current users Team

    I haven't tried this before .it would be great if you post some sample code for the same

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: Javascript code to check Current users Team

    Hi DivyaBharathi,

    It is actually not meant to be a link. I intended for it to be an example pseudocode for your JavaScript retrieve.

  • DivyaBharathi Profile Picture
    DivyaBharathi 245 on at
    RE: Javascript code to check Current users Team

    Hi Adrian thank you for your quick response

    //retrieve team

    customer.crm.dynamics.com/.../teams$select=name&$expand=teammembership_association($filter=systemuserid eq id);

    this link is not accessible

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: Javascript code to check Current users Team

    Hi DivyaBharathi,

    You can use JavaScript and the Ribbon Workbench for Dynamics 365 & Dynamics CRM to achieve this. This is a rough method for implementing this.

    1. Create a custom button with the Ribbon Workbench that executes custom JavaScript when clicked.
    2. In the custom JavaScript, retrieve the logged in users ID, and use this value to retrieve the users teams. The code will look something like this pseudocode.
      var id = Xrm.Page.context.getUserId(); //get ID
      
      //retrieve team
      customer.crm.dynamics.com/.../teams$select=name&$expand=teammembership_association($filter=systemuserid eq id);
      
      //change status based on team
      

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