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)

Updating User Profile

(0) ShareShare
ReportReport
Posted on by

We are currently on CRM 2016 v8.1 and we have a need for users to update their user profile to update their Site when they travel to different locations.  We have a referral entity that offers a customer a product/service and we need to give credit to the user and site for the sale if a customer accepts the offer. The process is when a user updates the status of a referral record, the referral is automatically updated with the user's site via javascript in order to credit the location for the business. This works as expected but some of our users travel to different locations to work for short periods of time (a day or more). We are looking for options on how to handle when a user changes locations.

 

One option is to have the user manually update the site on the referral for each referral but this is not a practical solution. We would like for the user to update their Site 1x and not have to worry about it again until they go back to their primary site or travel to another site.

 

Another option we thought about was creating a new user form specifically for these users, creating a security role giving the user write access to update the Site on their profile. This would solve the need of the user having to only update the site only once for the day.  The issue with this is that the write privilege on the User entity is BU & above, not for the user level, allowing users to edit other user profiles. Definitely not acceptable. 

 

We could use a dialog but since dialog's are deprecated, we are staying away from them. I know we could create a plugin but I’m not proficient at creating them and don’t have the time to fight through creating it. What other options do we have?

 

Any suggestions would be grateful.

 

Thank you in advance,
Chrys

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    ScottDurow Profile Picture
    21 on at

    Hi,

    I would create a new entity to store this information that you can secure at user level - the problem when updating the user record is that it invalidates the security cache and will cause a delay when the user accesses CRM after the change as the security is cached again.

    You can make the owner of the record the user that the settings apply to.

    Hope this helps

  • ChrysDW Profile Picture
    on at

    Thank you Scott. We created a new entity and we have tried thinking of several different ways of handling this but we can't figure out how to do it. Can you explain your thoughts on what we can do with the new entity? I appreciate your help.

    Chrys

  • Verified answer
    ChrysDW Profile Picture
    on at

    We were able to resolve our issue by creating a separate entity (MySite). The entity includes a lookup to the sites and the user.  The user can change the site each time they are at a different location and the referral will be assigned to the team based on the site that it gets referred by.  Below is the script we used to automatically update the referral based on the MySite location of the current user:

    function getAssignedSite() {

       //Make sure the record is new

       let FORM_TYPE_CREATE = 1;

       let formType = Xrm.Page.ui.getFormType();

       if (formType == FORM_TYPE_CREATE) {

           try {

               //Use CRM's Advanced Find to build the FetchXML query

               let mysiteFetchXML = ['<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">' +

                   '<entity name="org_mysite">' +

                   '<attribute name="org_mysiteid" />' +

                   '<attribute name="org_name" />' +

                   '<attribute name="org_userassignedtositeid"/>' +

                   '<attribute name="org_siteid"/>' +

                   '<order attribute="org_name" descending="false" />' +

                   '<filter type="and">' +

                   ' <condition attribute="org_userassignedtositeid" operator="eq-userid" />' +

                   '</filter>' +

                   '</entity>' +

                   '</fetch>'].join('');

               //Encode the FetchXML query.

               let encodedFetchXML = encodeURIComponent(mysiteFetchXML);

               //get the Service Root URL (getClientUrl); Web API found in the Developer Resources; LogicalCollectionName found in the XRMToolBox Metadata Browser (org_mysites), query statement for FetchXML (?fetchXml=)

               let ODataPath = Xrm.Page.context.getClientUrl() + "/api/data/v8.1/org_mysites?fetchXml=";

               //Build the request to retrieve data

               let req = new XMLHttpRequest();

               req.open("GET", ODataPath + encodedFetchXML, true);

               req.setRequestHeader("Accept", "application/json");

               req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

               req.setRequestHeader("OData-MaxVersion", "4.0");

               req.setRequestHeader("OData-Version", "4.0");

               req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");

               req.onreadystatechange = function () {

                   if (this.readyState == 4) {

                       req.onreadystatechange = null;

                       if (this.status == 200) {

                           let results = JSON.parse(this.response);

    //If there are no my site records for the user, use the site from the user profile

    //mysiteidValue = mysiteDetails['_org_siteid_value'] -- had to lookup this value (['_org_siteid_value'] ) from debugging the script in Chrome and walking through the code

                           if (results.value.length == 0) { getUsersite(); }

                           else {

                               for (let i = 0; i < results.value.length; i++) {

                                   let mysiteDetails = results.value[i];

                                   let mysiteidValue = mysiteDetails['_org_siteid_value'];

                                   let mysiteidName = mysiteDetails['_org_siteid_value@OData.Community.Display.V1.FormattedValue'];

                                   let mysiteEntityType = "org_site";

           //Set the value of the lookup (id: guid, name:name of the entity being looked up, entityType: type of record () of the entity being looked up)

                                   Xrm.Page.getAttribute("org_referredbysiteid").setValue([{ id: mysiteidValue, name: mysiteidName, entityType: mysiteEntityType }]);

                               }

                           }

                       }

                       else {

                           alert(this.statusText);

                       }

                   }

               };

               req.send();

           }

           catch (err) {

               alert("Please contact the CRM System Administrator.")

           }

       }

    }

    //Retrieve the user's site on the user profile

    function getUsersite() {

       let usersiteFetchXML = ['<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">' +

           '<entity name="systemuser">' +

           '<attribute name="systemuserid"/>' +

           '<attribute name="org_siteid"/>' +

           '<order descending="false" attribute="org_siteid"/>' +

           '<filter type="and">' +

           ' <condition attribute="systemuserid" operator="eq-userid" />' +

           '</filter>' +

           '</entity>' +

           '</fetch>'].join('');

       let encodedFetchXML = encodeURIComponent(usersiteFetchXML);

       //get the Service Root URL (getClientUrl); Web API found in the Developer Resources; LogicalCollectionName found in the XRMToolBox Metadata Browser (systemusers), query statement for FetchXML (?fetchXml=)

       let ODataPath = Xrm.Page.context.getClientUrl() + "/api/data/v8.1/systemusers?fetchXml=";

       //Build the request to retrieve data

       let req = new XMLHttpRequest();

       req.open("GET", ODataPath + encodedFetchXML, true);

       req.setRequestHeader("Accept", "application/json");

       req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

       req.setRequestHeader("OData-MaxVersion", "4.0");

       req.setRequestHeader("OData-Version", "4.0");

       //this header is needed for FetchXML

       req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");

       req.onreadystatechange = function () {

           if (this.readyState == 4) {

               req.onreadystatechange = null;

               if (this.status == 200) {

                   let results = JSON.parse(this.response);

                   if (results.length == 0) { alert("no record") }

                   else {

                       for (let i = 0; i < results.value.length; i++) {

                           let usersiteDetails = results.value[i];

                           let usersiteidValue = usersiteDetails['_org_siteid_value'];

                           let usersiteidName = usersiteDetails['_org_siteid_value@OData.Community.Display.V1.FormattedValue'];

                           let usersiteEntityType = "org_site";

    //Set the value of the lookup (id: guid, name:name of the entity being lookuped, entityType: type of record () of the entity being looked up)

                           Xrm.Page.getAttribute("org_referredbysiteid").setValue([{ id: usersiteidValue, name: usersiteidName, entityType: usersiteEntityType }]);

                       }

                   }

               }

               else {

                   alert(this.statusText);

               }

           }

       };

       req.send();

    }

    Hope this can help someone else,

    Chrys

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