Press ESC to close

Arjan ter HeegdeArjan ter Heegde Welcome to my blog about Microsoft Power Platform and Dynamics 365

Default signature for appointments in Dynamics 365 CE

In Dynamics 365 activities, only emails automatically include the default signature in the description text editor. This does not happen for appointments, but several users in our company have expressed a desire for this feature. To ensure that the default signature is loaded in the description when creating an appointment, I have created the following JavaScript:

function retrieveDefaultEmailSignature(executionContext) {
    var formContext = executionContext.getFormContext();

    // creaty only
    if (formContext.ui.getFormType() !== 1) {
        return; 
    }

    var userId = Xrm.Utility.getGlobalContext().userSettings.userId.replace(/[{}]/g, "");
    var apiUrl = Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.0/RetrieveEmailSignature";

    var request = {
        "SignatureId": "00000000-0000-0000-0000-000000000000",
        "SenderId": userId,
        "SenderType": "systemuser"
    };

    var req = new XMLHttpRequest();
    req.open("POST", apiUrl, true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var result = JSON.parse(this.response);
                var emailSignature = result.SignatureText;
                if (emailSignature) {
                    console.log("Set default signature for user in editor");
                    formContext.getAttribute("description").setValue("<br>" + decodeHtmlEntities(emailSignature));
                }
            } else {
                console.log("Error: " + this.statusText);
            }
        }
    };
    req.send(JSON.stringify(request));
}
function decodeHtmlEntities(text) {
    var element = document.createElement('div');
    if (text) {
        element.innerHTML = text;
    }
    return element.innerText || element.textContent;
}
JavaScript

Add this JavaScript as On Load on the Appointment entity main form:

After adding this, the default signature (if set by the user) will be loaded in the description for new appointments.

Arjan ter Heegde

Arjan ter Heegde

Comments (1)

  • Richard Olezsays:

    3 Sep 2024 at 21:01

    Hi,

    Your function is mostly correct, but there are a few things that can be improved or corrected:

    Correcting the API endpoint: The RetrieveEmailSignature request is typically made via the Dynamics 365 Web API, but the endpoint and the request might need to be adjusted depending on the specific API and method. If this is a custom API, the URL and the request structure might differ. Ensure that the endpoint /api/data/v9.0/RetrieveEmailSignature is correct for your environment.

    Form Type Check: The form type check (formContext.ui.getFormType() !== 1) is correct as it ensures the logic only runs when creating a new record (form type 1 corresponds to ‘create’).

    XMLHttpRequest handling: The way you handle the XMLHttpRequest is correct but could be modernized using fetch API, which is more readable and promise-based.

    Setting the email signature: The line where you set the email signature works, but ensure that the description field exists and is correct.

    Error Handling: The error handling could be expanded to include more details for better debugging.

    Here is a slightly improved version of your function:

    function retrieveDefaultEmailSignature(executionContext) {
    var formContext = executionContext.getFormContext();

    // Only for create forms
    if (formContext.ui.getFormType() !== 1) {
    return;
    }

    var userId = Xrm.Utility.getGlobalContext().userSettings.userId.replace(/[{}]/g, “”);
    var apiUrl = Xrm.Utility.getGlobalContext().getClientUrl() + “/api/data/v9.0/RetrieveEmailSignature”;

    var request = {
    “SignatureId”: “00000000-0000-0000-0000-000000000000”, // Make sure this is the correct ID or adapt as necessary
    “SenderId”: userId,
    “SenderType”: “systemuser”
    };

    var req = new XMLHttpRequest();
    req.open(“POST”, apiUrl, true);
    req.setRequestHeader(“Accept”, “application/json”);
    req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
    req.onreadystatechange = function () {
    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 200) {
    var result = JSON.parse(this.response);
    var emailSignature = result.SignatureText;
    if (emailSignature) {
    console.log(“Set default signature for user in editor”);
    formContext.getAttribute(“description”).setValue(“” + decodeHtmlEntities(emailSignature));
    }
    } else {
    console.error(“Error retrieving email signature: ” + this.statusText + ” (Status Code: ” + this.status + “)”);
    }
    }
    };
    req.send(JSON.stringify(request));
    }

    function decodeHtmlEntities(text) {
    var element = document.createElement(‘div’);
    if (text) {
    element.innerHTML = text;
    }
    return element.innerText || element.textContent;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *