Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

How to copy a data from the current user form to the main form using JS

Posted on by 3
Hi,
 
I have two lookup fields. Both titled /Branch/. Each user has one of these fields with an entry assigned to it.

When creating a new opportunity, there is a another field titled /Branch/ which I need to auto populate from the branch from the current user.

I believe this is possible using JS, but I cant work out the code required.
  • Verified answer
    Leah Ju Profile Picture
    Leah Ju Microsoft Employee on at
    How to copy a data from the current user form to the main form using JS
    Hi Partner,
    Yes, this can be done with JS code.
    There are three main steps:
    1) Get current user ID.
    2) Get user based on the ID, then we can get Brach value in the user.
    3) Populate Branch of opportunity with value from the user.
     
    Please refer to the following example code:
    function getBranch(executionContext) {
    
        var formContext = executionContext.getFormContext();
        //Get current user
        var userSettings = Xrm.Utility.getGlobalContext().userSettings;
        var currentuserid = userSettings.userId;
    
        //Retrieve the user to get branch value 
        Xrm.WebApi.retrieveRecord("systemuser", currentuserid, "?$select=_cr5a3_branch_value").then(
            function success(result) {
                console.log(result);
                // Columns
                var systemuserid = result["systemuserid"]; // Guid
                var cr5a3_branch = result["_cr5a3_branch_value"]; // Lookup
                var cr5a3_branch_formatted = result["_cr5a3_branch_value@OData.Community.Display.V1.FormattedValue"];
                var cr5a3_branch_lookuplogicalname = result["_cr5a3_branch_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
    
                //Set Branch in opportunity | cr5a3_branch_lookuplogicalname is logic name of the Brach in the opportunity
                formContext.getAttribute("cr5a3_branch_lookuplogicalname").setValue([{ id: cr5a3_branch, name: cr5a3_branch_formatted, entityType: cr5a3_branch_lookuplogicalname }]);
    
            },
            function (error) {
                console.log(error.message);
            }
        );
    }
    Note:
    You can use Dataverse REST Builder tool to build request easily.

    I hope you can mark my answer verified if it answer your question! If you have any questions, please feel free to contact me.
    Regards,
    Leah

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,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