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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Get Associated Record Member

(0) ShareShare
ReportReport
Posted on by 53

Hi,

I need to check if user is associated to a record via Jscript. 

I have a user sub-grid on account entity to grant access to record, I need to check in Jscript if specific user is associated to account record from another entity - Opportunity.

Any idea? Please help .

Thanks in advance.

.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    StefanS365 Profile Picture
    3,575 Most Valuable Professional on at

    Hi there,

    Why not use an Access Team on the Account form to grant access to the record? There's an built-in check if a user is already a member or not.

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    You just have to run the following query from JS and if it returns record, then the user is associated.

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">

     <entity name="account">

       <attribute name="primarycontactid" />

       <attribute name="accountid" />

       <order attribute="name" descending="false" />

       <filter type="and">

         <condition attribute="accountid" operator="eq" uiname="Test" uitype="account" value="{0B24B8B0-9619-E911-A975-0022480187F0}" />

       </filter>

       <link-entity name="systemuser" from="new_associatedusersid" to="accountid" link-type="inner" alias="ab">

         <filter type="and">

           <condition attribute="systemuserid" operator="eq" uiname="Test Test" uitype="systemuser" value="{629B3E9F-111F-E911-A974-0022480731BB}" />

         </filter>

       </link-entity>

     </entity>

    </fetch>

    in your script you just have to replace both Account GUID and user GUID int the fetch query

    You can execute fetch via webapi, have a look at this link

    [View:https://www.c-sharpcorner.com/article/execute-fetchxml-with-webapi-in-dynamics-365-using-javascript/:750:50]

    Hope this helps

  • Saran Profile Picture
    53 on at

    Thanks Kokulan , but i am getting below error.

    'SystemUser' entity doesn't contain attribute with Name = 'new_associatedusersid'

  • Mahendar Pal Profile Picture
    45,095 on at

    Hi,

    Can you share some details about your sub-grid, are you using any custom relationship between these entity, sharing details will help others to help you with correct direction. Here new_ is prefix of the solution it maybe you have a different prefix, Share entity relationship details here for checking.

  • Saran Profile Picture
    53 on at

    Below fetch xml provided by Kokulan seems to be working fine now.

  • Suggested answer
    StefanS365 Profile Picture
    3,575 Most Valuable Professional on at

    Access teams provide access rights to the record and related records, such as to an account and all opportunities related to this account. In case of parental relationship between the records, cascading rules are applied.

    Source: [View:https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/use-access-teams-owner-teams-collaborate-share-information]

    There's no need for javascript here.

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    @pinku.kumar.sah I thought you, associated users, using a 1:N relationship and had a subgrid on Account but your screenshot made it clear that you are using Access Teams template.

    When an Access Template is used, a team is created for every record based on that template. This team won't exist until the first user is added to the subgrid.

    Since its a system managed access team not user managed, I do not think the user in the account sub-grid will get access to all the related records. It explains why you want to check at the opportunity level to see if the user is in the account subgrid.

    There are few ways to query this and following is one of them

    We query the team entity, with the Access Team template name and the account and also check if the team has the user you want to check as the member. Please see below

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
    <entity name="team">
    <attribute name="name" />
    <link-entity name="teamtemplate" from="teamtemplateid" to="teamtemplateid" link-type="inner" alias="al">
    <filter type="and">
    <condition attribute="teamtemplatename" operator="eq" value="TEAM TEMPLATE NAME GOES HERE" />
    </filter>
    </link-entity>
    <link-entity name="teammembership" from="teamid" to="teamid" visible="false" intersect="true">
    <link-entity name="systemuser" from="systemuserid" to="systemuserid" alias="am">
    <filter type="and">
    <condition attribute="systemuserid" operator="eq" uiname="Test Test" uitype="systemuser" value="{User GUID goes here - the user you want to check permisison for }" />
    </filter>
    </link-entity>
    </link-entity>
    <link-entity name="account" from="accountid" to="regardingobjectid" link-type="inner" alias="an">
    <filter type="and">
    <condition attribute="accountid" operator="eq" uiname="A. Datum Corporation (sample)" uitype="account" value="{ACCOUNT GUID  goes here}" />
    </filter>
    </link-entity>
    </entity>
    </fetch>

    Like I said before, execute this query using the webapi and if the query does not return any record, it means the user is not in the account sub-grid. If it returns a record the user is in the account sub-grid.

    Hope this helps

  • Saran Profile Picture
    53 on at

    Thanks Kokulan, I tried this as well, below is the error message

    'Microsoft.Dynamics.CRM.team' was found, but it is not assignable to the expected type 'Microsoft.Dynamics.CRM.account'. The type specified in the resource must be equal to either the expected type or a derived. just FYI- i am on CRM online 9.0

  • Saran Profile Picture
    53 on at

    I am using access team, Need to check in JS if user is part of access team.

  • Verified answer
    Kokulan Profile Picture
    18,054 on at

    Hi

    Could you try the following in your javascript function please?

    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'> <entity name='team'> <attribute name='name' /> <link-entity name='teamtemplate' from='teamtemplateid' to='teamtemplateid' link-type='inner' alias='al'> <filter type='and'> <condition attribute='teamtemplatename' operator='eq' value='Access Team Template Name' /> </filter> </link-entity> <link-entity name='teammembership' from='teamid' to='teamid' visible='false' intersect='true'> <link-entity name='systemuser' from='systemuserid' to='systemuserid' alias='am'> <filter type='and'> <condition attribute='systemuserid' operator='eq' uiname='Test Test' uitype='systemuser' value='{USER GUID}' /> </filter> </link-entity> </link-entity> <link-entity name='account' from='accountid' to='regardingobjectid' link-type='inner' alias='an'> <filter type='and'> <condition attribute='accountid' operator='eq' uiname='A. Datum Corporation (sample)' uitype='account' value='{ACCOUNT GUID}' /> </filter> </link-entity> </entity> </fetch>";
    var encodedFetchXml = encodeURI(fetchXml);
    var queryPath = "/api/data/v8.0/teams?fetchXml=" + encodedFetchXml;
    var requestPath = Xrm.Page.context.getClientUrl() + queryPath;
    var req = new XMLHttpRequest();
    req.open("GET", requestPath, true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function ()
    {
    if (this.readyState === 4)
    {
    this.onreadystatechange = null;
    if (this.status === 200)
    {
    var returned = JSON.parse(this.responseText);
    var results = returned.value;
    if (results.length > 0) {
    alert("User is associated with Account");
    }
    else {
    alert("User is NOT associated with Account");

    }
    }

    else {
    alert(this.statusText);

    }
    }
    };

    req.send();

    Please replace the RED placeholders with your actual values. Let me know if this solves your problem.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans