To show dialog box in current entity

Dialog box in current entity

If Opportunity is won then show alert box and two buttons "yes or no", if "yes" then get current accountid and open account form. else if "no" then no action taken.

to check whether the Opportunity is won or loss.

function showConfirmDialog() {
debugger;
var statecode = Xrm.Page.getAttribute('statecode').getValue();
//alert(statecode);
if (statecode != null)
{
 // Won
 if (statecode == 1)
  {
     alert("won");
  }
else if(statecode == 2)
 {
   alert("Lost");
 }
 }
}

To perform action based on statecode.
function showConfirmDialog() {
debugger;
var statecode = Xrm.Page.getAttribute('statecode').getValue();
//alert(statecode);
if (statecode != null)
{
 // Won
 if (statecode == 1)
 {
  //alert(statecode);
    Xrm.Utility.confirmDialog("Click Yes or No to set the Personal Notes Value",
        function() {
            //alert("yes");
var currentaccid= Xrm.Page.getAttribute("parentaccountid").getValue()[0].id;
var currentaccname=Xrm.Page.getAttribute("parentaccountid").getValue()[0].name;
//alert(currentaccid);
//alert(currentaccname);
Xrm.Utility.openEntityForm( "account" , currentaccid);
//Xrm.Page.getAttribute("description").setValue("Yes");
        },
        function() {
           // Xrm.Page.getAttribute("description").setValue("No");
   });
 }
}
}

Subgrid access to count the one column and make total

ACCESS SUBGRID TO COUNT THE COLUMN AND SUM

SCENARIO:- Required total of field(priceperunit) in subgrid through javascript

Solution-I had executed below script.

//Xrm.Page.getControl('salesorderdetailsGrid').getGrid().getRows().get(i).getData().getEntity().getAttributes().get('priceperunit').getValue();

      I counted the grid total and based on count i fetched the data of 'priceperunit' data and total all the records present in sub grid.

function CostOfTotal()
{
debugger;
if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("salesorderdetailsGrid") != null && Xrm.Page.getControl("salesorderdetailsGrid") != undefined) {
//if subgrid is not null then counts the grid total
 var count = Xrm.Page.getControl("salesorderdetailsGrid").getGrid().getTotalRecordCount();
 alert("Total Products:"+count);
for(i = 0; i < count; i++) {
//to get control on grid count, which coloum you need to fetch data(like i used priceperunit)
var gridControl = Xrm.Page.getControl('salesorderdetailsGrid').getGrid().getRows().get(i).getData().getEntity().getAttributes().get('priceperunit').getValue();
alert(gridControl );
var cellValue = gridControl;
alert(cellValue);
cellValue = cellValue.replace( /,/,"." );
alert(cellValue);
var sum = sum + parseFloat(cellValue);
alert(sum);
}
}
//to set value in field.
Xrm.Page.getAttribute('new_total').setValue(sum);
}

Currency converter through in Dynamics CRM

To make currency convert into different country currencies in Dynamics CRM. I developed this below javascript.

 function convert(){
debugger;
// YOUR_APP_ID='API_KEY';
 $.ajax({
    url: 'https://openexchangerates.org/api/latest.json?app_id=API_KEY', 
    type: 'POST',
  success: function(data) {
        // exchange rata data is stored in json.quotes
        alert(data.rates.INR);
        // base currency is stored in json.base
        alert(data.base);
        // timestamp can be accessed in json.timestamp
        alert(data.timestamp);

var INR =data.rates.INR;
var USD = data.rates.USD;
var EURO= data.rates.INR;

var txtINR =Xrm.Page.getAttribute("new_inr").getValue();
var txtUSD =Xrm.Page.getAttribute("new_dollar").getValue();
var txtEURO = Xrm.Page.getAttribute("new_euro").getValue();
//from inr to usd and dollar
if(txtINR != null)
{
var val = parseFloat(txtINR).toFixed(2);
//INR to USD
var inrtousd = (1/INR)*val;
alert(inrtousd);
//INR to EURO
var inrtoeuro = (EURO/INR)*val;
alert(inrtoeuro);
break;
}
else if(txtUSD != null)
{
//from usd to inr,euro
var val1 = parseFloat(txtUSD).toFixed(2);
//USD to INR
var ustoinr = val1*INR;
alert(ustoinr);
//USD to EURO
var ustoeuro =val1*EURO;
alert(ustoeuro);
break;
}
else if(txtEURO !=null)
{
//from euro to inr,usd
var val2= parseFloat(txtEURO).toFixed(2);
//EURO TO INR
var eurotoinr = (INR/EURO)*val2;
alert(eurotoinr);
//EURO TO USD
var eurotousd(1/EURO)*val2;
alert(eurotousd);
break;
}
    }
  });
  }

Set maps on entity based on zipcode

Based on user zipcode we can set the location of user in Dynamics CRM.



<html><head>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
function initialize() {
var map_canvas = document.getElementById('map_canvas');
var map_options = {
center: new google.maps.LatLng(-26.4420246,133.281323),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(map_canvas, map_options)
var geocoder = new google.maps.Geocoder();
var address = window.parent.Xrm.Page.data.entity.attributes.get('address1_composite').getValue();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
map.setZoom(12);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
//alert("Geocode was not successful for the following reason: " + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<meta charset="utf-8"></head>
<body style="word-wrap: break-word;">
<div id="map_canvas" style="width: 100%; height: 100%;">google maps</div>
</body></html>

According to Role hide the section

Show Fields on the Basis of Security Role of Current User Using Javascript in MS Dynamics CRM



If you want to show/Hide or Lock/Unlock specific field instead of a whole section you can just replace the "ShowHideSectionAccordingToRole" function with the below function :


function ShowHideFieldAccordingToRole()
{

if(CheckCurrentUserRole("CSR Manager"))
{
//Show the Field
Xrm.Page.ui.controls.get("FieldName").setVisible(true);
               //To UnLock the field
              // Xrm.Page.getControl("FieldName").setDisabled(false); 
}
else 
{
//Hide the Field
Xrm.Page.ui.controls.get("FieldName").setVisible(false);
               //To lockLock the field
              // Xrm.Page.getControl("FieldName").setDisabled(true); 
}
}