Thursday 29 November 2018

Dynamics Portal-Note Required Based On Quick View Form Value


Microsoft Dynamics Portal provides capability to allow login customers without needed any license. Here I am not explaining benefits of Dynamics Portal. I am going to show a use case for developers.

Case Study: - There is a requirement in portal where we need to make attach note section mandatory based on value of quick view form.  Now Quick view form is supported by Dynamics Portal. So, it is nice to use Quick view form to show information based on lookup value.
While creating a case from portal, customer needs to select reason (lookup field). On selection of this lookup Quick view form displayed with required document list and a Boolean field which specify document is required or not.

Based on this Boolean field value need to make attachment required or not.


















Solution: - We need to use Javascript to complete this requirement.
  • Login to Dynamics Portal with Administrator rights.
  • Click on “My Support” button than “Open a New Case” button.
  • Once create case page is opened click on edit button.












  • Once edit section is opened, click on “Option” tab.
  • Now add your Javascript code in “Custom JavaScript text block.
















  • Click on Save button.
JavaScript Code 

/////////////////////////////////  Code Started ///////////////////////////////////////////

$(document).ready

(

function () {
    $('iframe#MandatoryInformation').load(function () {
        setTimeout(getdoc, 50);


    });
     
}
);

function getdoc() {
    let iframe = document.getElementById("MandatoryInformation");
    var innerDoc = (iframe.contentDocument)
        ? iframe.contentDocument
        : iframe.contentWindow.document;
    // check document requirement
    let isDocRequired = innerDoc.getElementById("boolean_field_schemaName_1");
    if (isDocRequired) {
        if (isDocRequired.checked) {
            //make attachment  mandatory
            addNoteValidator("AttachFile", "AttachFileLabel");

        }
        else {
            //make attachment non mandatory
            removeNoteValidator("AttachFile", "AttachFileLabel");
        }
    }
    else {
        //make attachment non mandatory
        removeNoteValidator("AttachFile", "AttachFileLabel");
    }

}
function addNoteValidator(fieldName, fieldLabel) {


    if (typeof (Page_Validators) == 'undefined') return;
    // Create new validator
    $("#" + fieldLabel).parent().addClass("required");

    var newValidator = document.createElement('span');
    newValidator.style.display = "none";
    newValidator.id = "RequiredFieldValidator" + fieldName;
    newValidator.controltovalidate = "casetypecode";
    newValidator.errormessage = "<a href='#" + fieldLabel + "'>" + "Make sure you should attach all required documents.</a>";
    newValidator.validationGroup = "";
    newValidator.initialvalue = "";
    newValidator.evaluationfunction = function () {
        var value = $("#" + fieldName).val();
        if (value == null || value == "") {
            return false;
        } else {
            return true;
        }
    };

    // Add the new validator to the page validators array:
    Page_Validators.push(newValidator);

    // Wire-up the click event handler of the validation summary link
    $("a[href='#" + fieldLabel + "']").on("click", function () { scrollToAndFocus(fieldLabel + '', fieldName); });
}
function removeNoteValidator(fieldName, fieldLabel) {
    $.each(Page_Validators, function (index, validator) {
        if (validator.id == "RequiredFieldValidator" + fieldName) {
            Page_Validators.splice(index, 1);
        }
    });

    $("#" + fieldLabel + "").parent().removeClass("required");
}

/////////////////////////////////  Code Ended///////////////////////////////////////////

Code Explanation: -  
  • On Load of create case page we are adding “On Load” event to our Iframe.  This event will fire on change to content of your “Quick View Form”.

















    $('iframe#MandatoryInformation').load(function () {
// use setTimeout to complete Iframe load.
        setTimeout(getdoc, 50);
    });
  • Timeout is used because Iframe takes some time to load completely.
  • Select Iframe content

    let iframe = document.getElementById("MandatoryInformation");

    var innerDoc = (iframe.contentDocument)
        ? iframe.contentDocument
        : iframe.contentWindow.document;
  • check document requirement field value

    let isDocRequired = innerDoc.getElementById("boolean_field_schemaName_1");
    if (isDocRequired) {
        if (isDocRequired.checked) {
  • Make attachment mandatory if Boolean in true

addNoteValidator("AttachFile", "AttachFileLabel");
  • Remove attachment mandatory if Boolean in false

  removeNoteValidator("AttachFile", "AttachFileLabel");

Result: -
  • If Field value is true.

























  • If Field value is false.

Share your feedback and follow blog to be updated !!


Tuesday 6 November 2018

Dynamics CRM: Sent Email Template With Conditional Image


In Dynamics CRM we use Email activity a lot to send all kind of notification and approval.  CRM also provide Email Template that we can use to send customize email.
But some time we need to extend this capability.

Sample Case: - We need to send email to customer based on case origin type with below conditions.
  • We need to use embedded image. We cannot use global image hosted on public domain.
  • We need to use web resource images of CRM.
  • Need to use Email template.
  • Image need to change based on case origin value.


Solution: - After lot of research and try different solution. We conclude that we need to perform below task to achieve our requirement.
  • Need base64 string with html image tag to insert in email.
  • Need to create an email to set sender and receiver value.
  • We need email template information not to send just to get body and subject value.
  • Need to create our custom placeholder and replace these placeholder as per our requirement.

To achieve this we need some custom workflow activity for:
  • Convert web resource to base64 image string
  • Get email template subject and description at run time and set these values in our email
  • To replace our placeholders
  • Send Email

Steps: -
  1.  Create PNG Web resource to store images.







      2. Create an action to get web resource to base64 image string based on condition. Use Getbase64Image custom workflow.




























   3. Create placeholder in your Email template. I have use [url] as placeholder.

















4. Create Workflow to complete our requirement and send email
·         Call previously create action to get embedded image.
·         In next step create an email set to and from value.
·         In next step call update email from email template custom workflow. This workflow will use Instantiate Template Request because do not need to send this email template. We need it’s Subject and description value.

InstantiateTemplateRequest instTemplateReq = new InstantiateTemplateRequest
                {
                    TemplateId = EmailTemplate.Get(executionContext).Id,
                    ObjectId = Case.Get(executionContext).Id,
                    ObjectType = Case.Get(executionContext).LogicalName
                };
InstantiateTemplateResponse instTemplateResp = (InstantiateTemplateResponse)service.Execute(instTemplateReq);
  Entity emailT = (Entity)instTemplateResp.EntityCollection.Entities[0];
Entity email = service.Retrieve(EmailSource.Get(executionContext).LogicalName, EmailSource.Get(executionContext).Id, new ColumnSet("description", "subject"));
  email.Attributes["description"] = emailT.Attributes.Contains("description") ? (String)emailT.Attributes["description"] : "";
email.Attributes["subject"] = emailT.Attributes.Contains("subject") ? (String)emailT.Attributes["subject"] : "";
service.Update(email);

 5.  Call Replace Placeholder workflow to update placeholder with embedded image.
 6.  Finally send this email. 

















I have not considered email Template with attachment. For Email Template with attachment you need to extend “update email from email template” custom workflow.

Result: -













I am sharing custom workflow assembly. Hope this will help other also.

Field Security Profile - Based on Owner

 Recently received requirement related to Field security profile. Expectation : - 1.       Set to users need access of secure attributes. 2....

Test