Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to autopopulate User's Branch lookup value to Account Page Lookup Value

Posted on by 340

Hi Experts,

I have an Account form , in that Branch Lookup is there. and in Security->User->ABC (Name of the User) is there, where in User ABC's Branch is Set to BB which is a lookup field.

When i try to click on the New Account then Branch Name lookup is auto populated from User Lookup.

How i achieve this. Can anyone suggest a step by step process.

Thanks in Advance

*This post is locked for comments

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: How to autopopulate User's Branch lookup value to Account Page Lookup Value

    Hi Nilanka,

    I actually have a working code for you i did this earlier :

     function retrieveBranchValue:()

    {

           var userid = Xrm.Page.getAttribute("ownerid").getValue();

           var newid = userid[0].id.slice(1, -1);

           var req = new XMLHttpRequest();

           req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/systemusers(" + newid + ")?$select=_branch_value", true); //get branch from user

           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);

                       var userbranch = result._branch_value; //get the id of the brnach

                       var userbranchformatted = result["_branch_value@OData.Community.Display.V1.FormattedValue"]; //get the formatted name of the branch

                       if (userbrnach != null) {

                           var value = new Array();

                           value[0] = new Object();

                           value[0].id = userbranch;

                           value[0].name = userbranchformatted;

                           value[0].entityType = "branch";

                           Xrm.Page.getAttribute("branch").setValue(value); //set the lookup

                       }

                       else

                           alert("Your user profile is missing branch information; please add and try again!")

                   } else {

                       Xrm.Utility.alertDialog(this.statusText);

                   }

               }

           };

           req.send();

       }

    kindly change the schema name of the field and entity of branch.

    call the function on onload of your account form or as per your needs.

    mark my suggestion as verified if helpful.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: How to autopopulate User's Branch lookup value to Account Page Lookup Value

    Hi Nilanka,

    If you want to auto populate when you click New Account then you need to write the JavaScript to-

    1. Retrieve the branch value from the user using web api.

    2. Set the branch value in account form

    You can check this thread for reference- community.dynamics.com/.../240539

    If you want to populate it when you create the account record (i.e. when you click save) then you can create a real time workflow which will

    1. Trigger on create

    2. Sets the branch from owner to account

    Hope this helps.

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