Skip to content
PhuocLe edited this page Jun 7, 2024 · 40 revisions
  1. Create 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
    }
})();
  1. 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
  1. 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) {
            //working task 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
    }
})();
  1. Dialog built by Dataverse Dialog Builder dialog

  2. result object after user click the Close button result