Thursday, April 29, 2021

How to format Date and Time with AM / PM

 I have used below code for formatting date time in a web-resource in dynamic crm.

// Formatting Date and Time with AM / PM 

function formatAMPM()

{

var date = new Date();

var hours = date.getHours();

var minutes = date.getMinutes();

var ampm = hours >= 12 ? 'PM' : 'AM';

hours = hours % 12;

hours = hours ? hours : 12; // the hour '0' should be '12'

minutes = minutes < 10 ? '0' + minutes : minutes;

var strTime = (date.getMonth()+1)+"/"+date.getDate()+"/"+ date.getFullYear()+" "+hours+':'+minutes +' '+ampm;

return strTime;

}


How to format Date in MMDDYYYY


// Formatting Date
this.setDateOptions = function () {
    Date.prototype.formatMMDDYYYY = function () {
        return this.getMonth() + "/" + this.getDate() + "/" + this.getFullYear();
    }

}



More Reference

How to validate end date to be greater than start date

Guidelines to write good JavaScript code in Microsoft Dynamic CRM

Refreshing Rollup field using JavaScript

Most common JavaScript methods for Dynamic 365 CRM


How to validate end date to be greater than start date

 

// Validating Start and End Date

function ValidateMaintenanceEndDate(executionContext)

{

 var formContext = executionContext.getFormContext();

 var startDateField = formContext.getAttribute("new_maintenancestart");

 var endDateField = formContext.getAttribute("new_maintenanceend");

 var endDateFieldControl = formContext.getControl("new_maintenanceend");

 var startDate = startDateField.getValue();

 var endDate = endDateField.getValue();

 

 if (startDate != null && endDate != null)

 {

  startDate = new Date(startDate.toISOString().substr(0, 10));

  endDate = new Date(endDate.toISOString().substr(0, 10));

 

  endDateFieldControl.clearNotification("ErrEndDate");

 

  if (startDate >= endDate)

  {

   endDateFieldControl.setNotification("cannot be before or equal to Maintenance Start.""ErrEndDate");

  }

  else

  {

   endDateFieldControl.clearNotification("ErrEndDate");

  }

 }

}



More Reference

How to format Date and Time with AM / PM

Guidelines to write good JavaScript code in Microsoft Dynamic CRM

Refreshing Rollup field using JavaScript

Most common JavaScript methods for Dynamic 365 CRM


How to format a phone number in Dynamic 365 crm

 

// Formatting Phone Number

function formatPhoneNumber(phoneNumber)

{

    if (phoneNumber.length != 10) {

        return phoneNumber;

    }

    var piece1 = phoneNumber.substring(0, 3); //123

    var piece2 = phoneNumber.substring(3, 6); //456

    var piece3 = phoneNumber.substring(6); //7890

 

    return kendo.format("({0})-{1}-{2}", piece1, piece2, piece3);

}




More Reference here

How to format a phone number in Dynamic 365 crm

How to validate mobile with country code in Dynamic CRM

How to validate mobile number in Dynamic CRM

Remove comma from number fields in Dynamic 365 CRM


Wednesday, April 28, 2021

How to open a web resource and pass parameter in Dynamic 365 CRM

 



Dynamic CRM  - Xrm.Navigation.navigateTo

I would be using navigateTo here in this blog to launch a webresource. 


JavaScript which is used to launch webresource and pass parameters.

function launchContactWebResource()

{

debugger;

 

var selectedRow = Xrm.Page.getControl("contactgrid").getGrid().getSelectedRows(); 

var selectedRecordId = selectedRow.getAll()[0].getData().getEntity().getId().replace('{', "").replace('}', "");

var selectedRecordName = selectedRow.getAll()[0].getData().getEntity().getPrimaryAttributeValue();

 

 

var selectedContact =

{

    ContactName: selectedRecordName,

    ContactId: selectedRecordId

};


var pageInput = {

    pageType: "webresource",

    webresourceName: "hel_/html/contact/ShowContacts",  // Web-Resource Schema name

    data: JSON.stringify(selectedContact)   // pass parameter here

};


var navigationOptions = {

    target: 2,

    width: 400, 

    height: 400, 

    position: 1    // Open in Center

};


window.parent.Xrm.Navigation.navigateTo(pageInput, navigationOptions).then(

    function success() {

        // Run code on success         

    },

    function error(error) {

        window.parent.Xrm.Utility.alertDialog(error.message);

    }

);

}



Web-Resource Code to fetch and Display Parameters.


<html>

<head>

<script type="text/javascript">

         

    function onload()

    {

        GetSelectedRecord();               

    }

          

    function GetSelectedRecord()

    {

        debugger;

        var id = "";

        var name = "";

        if (location.search != null) {

            if (location.search.split("=")[1] != null) {

                id = JSON.parse(decodeURIComponent(location.search.split("=")[1]))["ContactId"];

                name = JSON.parse(decodeURIComponent(location.search.split("=")[1]))["ContactName"];

            }

        }

 

        var table = document.getElementById("tblOrgActivity");

        var rowCount = table.rows.length;

        var row = table.insertRow(rowCount);

        row.insertCell(0).innerHTML = id;

        row.insertCell(0).innerHTML = name;

    }      

 

    </script>

</head>

<body onload="baseContacts.onload()">

    <div id="mydata">

        <div>

            <table id="tblOrgActivity" border="1">               

                <tr>

                    <td>Name</td>

                    <td>ID</td>

                </tr>

            </table>

        </div>

 

    </div>

</body>

</html>



Launch Web-Resource in Adjacent TAB


var navigationOptions = {

    target: 2,

    width: { value: 50, unit: "%" },    

    position: 2    // Open in Adjacent Tab    

};






Generic Method to retrieve parameters



var entityId = getUrlVars()["entityId"];

var entityName = getUrlVars()["entitytypename"];

 

// Read a page's GET URL variables and return them as an associative array.

function getUrlVars()

{

    var vars = [], hash;

    var hashes = unescape(window.location.search.replace('?'''));

    hashes = hashes.replace('Data=''').replace('data=''').split(',');

    for (var i = 0; i < hashes.length; i++)

    {

        hash = hashes[i].split('=');

        vars.push(hash[0]);

        vars[hash[0]] = hash[1];

    }

    return vars;

}