Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

Set Field Based Upon Logged in User

Posted on by 40

We are using Business Units in our D365 CE environment.  I added to the Account creation form a field for "Business Unit".  I would like to be able to default the "Business Unit" field on the Account to the Business Unit associated with the logged in user when a record is created.  Any guidance on how best to accomplish this would be appreciated.

Categories:
  • Justin Palmer Profile Picture
    Justin Palmer 40 on at
    RE: Set Field Based Upon Logged in User

    I am working on data import into D365 CE today but when I am able to try the provided solutions I will make sure to mark the one that I used.

  • Priyesh Profile Picture
    Priyesh 7,392 User Group Leader on at
    RE: Set Field Based Upon Logged in User

    Thanks Justin,

    If my or anyone's approach helped you, please mark the answer as verified. This will help others identify the solution.

  • Justin Palmer Profile Picture
    Justin Palmer 40 on at
    RE: Set Field Based Upon Logged in User

    I appreciate the breadth and details of the answers provided by all.  I am new to the D365 community and it is great to see a community with this type of collaboration.  Coming from a NetSuite background I would have to say that the JavaScript onLoad approach is a familiar approach but I appreciate the diversity in the approaches.  Thanks to all who have contributed answers.

  • Suggested answer
    LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: Set Field Based Upon Logged in User

    Hi Justin,

    You could also use JS code to do this.

    We just need to get the current login user first and set his Business unit to the field, here is the sample code.

    //Get Buniness Unit according to the current login user and set it as default value to BUnit field
    function setBusinessUnit(executionContext) {
        var formContext = executionContext.getFormContext();
        var useridOrg = Xrm.Page.context.getUserId();
        var userid = useridOrg.substring(1, useridOrg.length - 1);
        Xrm.WebApi.retrieveRecord("systemusers", userid, "?$select=_businessunitid_value").then(
            function success(result) {
                var _businessunitid_value = result._businessunitid_value;
                var _businessunitid_value_formatted = result["_businessunitid_value@OData.Community.Display.V1.FormattedValue"];
                var field = formContext.getAttribute("new_businessunit");
                var lookupValue = new Array();
                lookupValue[0] = new Object();
                lookupValue[0].id = _businessunitid_value; // GUID of the lookup id
                lookupValue[0].name = _businessunitid_value_formatted; // Name of the lookup
                lookupValue[0].entityType = "businessunit"; //Entity Type of the lookup entity
                field.setValue(lookupValue);
            },
            function (error) {
                alert(error.message);
            }
        );
    }

    Please add this js to webresource and add the "setBusinessUnit" function to Onload event in Account form.

    community65.png community66.png

    And here is the result, when I opening Account form, the "Business Unit" field will be set value of the current user's Business Unit.

    community64.png

    Hope it helps.

    Best Regards,

    Leo

  • Verified answer
    Priyesh Profile Picture
    Priyesh 7,392 User Group Leader on at
    RE: Set Field Based Upon Logged in User

    Hi,

    Here's what you can do.

    I'm assuming you have a lookup to the Business Unit entity created on the Account record, in that case, you can create the following workflow on Create of the Account with the following steps -

    1. Create a Real-time workflow (even a Background workflow would do) that has only 1 Update step for the Account which you are creating. Make sure the scope is set to the One Who Makes Changes to the record.
    updateStep.png

    2. In the Update Steps, set the Lookup to the related Created By's Business Unit lookup as shown below

    createdByStep.png

    So, whenever you or anyone creates a record, their Business Unit will be populated in the lookup -

    defaultBUShown.png

    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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans