Auto-Populate Email Signature based on the Queue name using Javascript

Below is the code that fulfilled the requirement;

While started working for this requirement, breaking down the requirement into pieces help me to develop code and Voila! it worked.

Don’t let fear , it pulls you back instead step ahead even there are ditches but ultimately the path paves you to a red carpet.

function setSignature(executionContext) {
    var formContext = executionContext.getFormContext();
    var name = formContext.getAttribute("from").getValue()[0].name;

    var fetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct ='false'>" +
        "<entity name='emailsignature'> " +
        "<attribute name='title' /> " +
        "<attribute name='emailsignatureid' /> " +
 "<attribute name='safehtml' /> " +
        "<filter type ='and'> " +
        "<condition attribute='title' operator='eq' value='" + name + "'/>" +
        "</filter> " +
        "</entity> " +
        "</fetch>";
    fetchXML = "?fetchXml=" + encodeURIComponent(fetchXML);

   Xrm.WebApi.retrieveMultipleRecords("emailsignature", fetchXML).then(
        function success(result) {
                var desc = formContext.getAttribute("description").getValue();
                if (desc.includes(result.entities[0].safehtml)) {
                    formContext.getAttribute("description").setValue(desc);
                }
                else {
                    if (desc != null) {
                        desc += "<br>" + result.entities[0].safehtml;
                        formContext.getAttribute("description").setValue(desc);
                    }
                    else {
                        formContext.getAttribute("description").setValue(result.entities[0].safehtml);
                    }
                }
                
            },
        function (error) {
            console.log("failed with error: ", error);
            return null;
        }
    );
}

Leave a comment