Back to Basics # 47: Understand setShowTime With Webresource in Dynamics CRM

Introduction:

In Dynamics 365 CRM, at times we must show only date part for a field of date time type if at all customers were not in favour of creating same field with date Only format. Then this can be achieved by the usage of setShowTime Client API reference, which specify whether a date control should show the time portion of the date or not. As an example, Vaccination date is used to explain this functionality.

Step 1:

Login to the required environment and select required solution [Contact Customizations Solution in this case] as shown in the   below figure.

Step 2:

After Step 1, select contact web resource in solution and click on Edit as shown in the below figure.

Step 3:

After Step 2, in contact web resource javascript file write the code to show only date part of the date time field using the below syntax

formContext.getControl(arg).setShowTime(bool);

As

formContext.getControl(“cr5bc_vaccinationdate”).setShowTime(false);

As shown in the below figure

Step 4:

After Step 3, write the code for show required date in the following method

function  showvaccinationDatePart(executionContext)

{

    let formContext = executionContext.getFormContext();

    if (formContext !== null && formContext != ‘undefined’)

    {

        formContext.getControl(“cr5bc_vaccinationdate”).setShowTime(false);

    }

}

As shown in the below figure

Step 5:

After Step 4, call this function from on load event of contactform with the below code

handleOnLoad: function (executionContext)

        {

        console.log(‘on load – contact form’);

        showvaccinationDatePart(executionContext);

        },

As shown in the below figure

Step 6:

After Step 5, now the complete code looks like this

if (typeof (ContosoVaccination) == “undefined”)

{

    var ContosoVaccination = {__namespace: true};

}

if (typeof (ContosoVaccination.Scripts) == “undefined”)

{

    ContosoVaccination.Scripts = {__namespace: true};

}

ContosoVaccination.Scripts.ContactForm =

{

    handleOnLoad: function (executionContext)

        {

        console.log(‘on load – contact form’);

        showvaccinationDatePart(executionContext);

        },

    __namespace: true

}

function  showvaccinationDatePart(executionContext)

{

    let formContext = executionContext.getFormContext();

    if (formContext !== null && formContext != ‘undefined’)

    {

        formContext.getControl(“cr5bc_vaccinationdate”).setShowTime(false);

    }

}

Step 7:

After Step 6, now save and publish the Webresource and open a contact record and observe vaccination date field was shown with only Date part of a calendar control as shown in the below figure.

 

Note:

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. Vaccination Date custom field was created with date time data type in contact record and then placed on the contact form and saved and published changes.
  3. Contact Form on load event should be registered as an event handler after creation / updation of the new/existing Webresource of type js.
  4. Microsoft documentation found here.

Conclusion: In this way, one can easily use setShowTime Client API Reference to show only date part of the date time field using custom code with web resource in Dynamics 365 CRM.

2 thoughts on “Back to Basics # 47: Understand setShowTime With Webresource in Dynamics CRM

  1. Pingback: Back To Basics :Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

  2. Pingback: Rewind March 2022 – Common Man Tips for Power Platform, Dynamics CRM,Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s