Skip to content
PhuocLe edited this page Jun 10, 2024 · 40 revisions

1. Create a JavaScript file and upload to your Dataverse environment

"use strict";
var FindUserId = (function () {
    "use strict";
    async function OnOpen(executionContext) {
    }
    async function OnLoad(executionContext) {
    }
    async function OnClickFind(executionContext) {
    }
    async function OnClickClose(executionContext) {
    }
    return {
        OnOpen,
        OnLoad,
        OnClickFind,
        OnClickClose
    }
})();

2. Drag and drop controls to Dataverse Dialog Builder as the picture below

00

# Control Properties
1 Metadata 01
2 Parameter 02
3 Parameter 03
4 Event 04
5 Event 05
6 Event 06
7 Label 07
8 Label 08
9 Tab 09
10 Section 10
11 Textbox 11
12 Textbox 12
13 Button 13
14 Button 14

3. Update JavaScript code

"use strict";
var FindUserId = (function () {
    "use strict";
    async function OnOpen(executionContext) {
        const options = {
            position: 1,
            width: 600,
            height: 270
        };
        const { userName } = Xrm.Utility.getGlobalContext().userSettings;
        const params = {
            pl_input_user_name: userName
        };
        const result = await Xrm.Navigation.openDialog("pl_find_user_id", options, params);
        if (result?.parameters?.pl_output_user_id?.length > 0) {
            //continue working with the user id you found ...
        }
    }
    async function OnLoad(executionContext) {
        const formContext = executionContext.getFormContext();
        const pl_input_user_name = formContext.getAttribute("pl_input_user_name").getValue();
        formContext.getAttribute("pl_textbox_user_name").setValue(pl_input_user_name);
    }
    async function OnClickFind(executionContext) {
        const formContext = executionContext.getFormContext();
        const userName = formContext.getAttribute("pl_textbox_user_name").getValue();
        const fetchData = {
            fullname: userName
        };
        let fetchXml = `
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='systemuser'>
    <attribute name='systemuserid'/>
    <filter type='and'>
      <condition attribute='fullname' operator='eq' value='${fetchData.fullname}'/>
    </filter>
  </entity>
</fetch>
`;
        fetchXml = "?fetchXml=" + encodeURIComponent(fetchXml);
        Xrm.Utility.showProgressIndicator("Processing ...");
        const response = await Xrm.WebApi.retrieveMultipleRecords("systemuser", fetchXml);
        Xrm.Utility.closeProgressIndicator();
        if (response.entities.length === 1) {
            const entity = response.entities[0];
            userId = entity.systemuserid;
            formContext.getAttribute("pl_textbox_user_id").setValue(userId.toUpperCase());
            formContext.getAttribute("pl_output_user_id").setValue(userId.toUpperCase());
        }
        else {
            formContext.getAttribute("pl_textbox_user_id").setValue(null);
            formContext.getAttribute("pl_output_user_id").setValue(null);
        }
    }
    async function OnClickClose(executionContext) {
        const formContext = executionContext.getFormContext();
        formContext.ui.close();
    }
    return {
        OnOpen,
        OnLoad,
        OnClickFind,
        OnClickClose
    }
})();

4. Explain code

  • You used Xrm.Navigation.openDialog to open the dialog with logical name pl_find_user_id that you already define in Metadata number 1
  • All your knowledge of JavaScript remains the same as what you're already familiar with develop Main form
  • Make sure you check the syntax openDialog for more information

5. The picture below show when you open Dialog built by Dataverse Dialog Builder

dialog

6. And, when user click the Find button. The code FindUserId.OnClickFind execute by your define Event number 5)

dialog2

7. Finally, the result object after user click the Close button. The code FindUserId.OnClickClose execute by your define Event number 6)

result

8. Check solution DataverseDialogBuilder.Examples.sln to see the final code