Featured Post

Dynamics 365 - Business Process Flow and Things to take note before you upgrade into Dynamics 365.

In 2 months ago, my company decided to upgrade the Dynamics CRM 2016 into Dynamics 365 and there is something different about Dynamics 365 B...

Tuesday, January 27, 2015

Dynamics CRM 2015 - Navigate Form

Go to the form that you need to navigate. Press F12 and press on arrow button( highlighted box)














Click on the highlighted box



Now going back to the F12 page, under the tab index you should able to see the currentformid. Please copy the guid.





Now we going to create a JavaScript to navigate the form when certain criteria is achieved.Please include the below javascript within your if else statement.

 Xrm.Page.ui.formSelector.items.get("theguidthatyoucopy").navigate();

Thursday, January 15, 2015

Dynamics CRM 2013 - Business Rule Vs Javascript CRM 2013

Business Rule, a feature that introduced in CRM 2013. Business Rule allows the system administrator to configure the business logic much more easier than using JavaScript without coding skill required.

But have you ever wonder what if both features are performing the same thing during onLoad or onChange event at the same time. So, we are going to configure the business rule and JavaScript to perform the same functionality and analyse the result. 

First Scenario OnLoad Event

At first i will create a business rule 


then i created a JavaScript to do the same thing that business rule does.















Result for OnLoad , seems that business rule is won.

Now i am going to test the OnChange Event. 

As expected business rule won again.
Now i will set the business rule to set the value instead of showing error message. Let's see the result.

Conclusion, business rule and JavaScript cannot work well on the same thing. If you ever encounter the problem where JavaScript is not working without error, please double check whether is there any business rule perform the same functionality that JavaScript does.

But is business rule can totally replace the JavaScript? The answer is No. Let's break into details so that you have more understand the limitation between business rule and JavaScript.



Dynamics CRM 2013 - Useful JavaScript

Get a value from CRM field

var value= Xrm.Page.getAttribute(“fieldschemaname”).getValue();

Set Value to field

Xrm.Page.getAttribute(“fieldschemaname”).setValue();

Get Entity ID
var ID= Xrm.Page.data.entity.getId();


Hide a Tab/Section from CRM form

Tab 

Xrm.Page.ui.tabs.get(tabname/SectionName).setVisible(false);

Section inside a Tab 
Xrm.Page.ui.tabs.get("tab_3").sections.get("tab_3_section_2").setVisible(false);

Show a Tab/Section from CRM form

Tab 
Xrm.Page.ui.tabs.get(tabname/SectionName).SetVisible(true);

Section inside a Tab Xrm.Page.ui.tabs.get("tab_3").sections.get("tab_3_section_2").setVisible(true);


Set Focus

Xrm.Page.getControl("fieldschemaname").setFocus(true);


Expand / Collapse Tab

Expand tab
Xrm.Page.ui.tabs.get("tabname").setDisplayState("expanded");

Collapse tab
Xrm.Page.ui.tabs.get("tabname").setDisplayState("collapsed");

Set Requirements field

Xrm.Page.getAttribute(“fieldschemaname”).setRequiredLevel("none");
Xrm.Page.getAttribute(“
fieldschemaname”).setRequiredLevel("required");
Xrm.Page.getAttribute(“
fieldschemaname”).setRequiredLevel("recommended");

Set Focus field

Xrm.Page.getControl(“fieldschemaname”).setFocus(true);

Get Security role

Xrm.Page.context.getUserRoles();

Get Lookup ID and name

var lookupvalue = new Array();

lookupvalue = Xrm.Page.getAttribute("fieldschemaname").getValue();

if (lookupvalue !=null)
{
  var name = lookupvalue [0].name;
  var id = lookupvalue [0].id;
   
}


Refresh Ribbon Button

Xrm.Page.ui.refreshRibbon()

Set a lookup value

var lookup = new Array();
lookup[0] = new Object();
lookup[0].id =valueid;
lookup[0].name = valuename;
lookup[0].entityType = entityname;
Xrm.Page.getAttribute ("fieldschemaname").setValue(lookup); 


Set error message

Xrm.Page.getControl("fieldschemaname").setNotification("message");

Get Current User

var userID = Xrm.Page.context.getUserId();
var userName = Xrm.Page.context.getUserName();

Save and Close

Save function
Xrm.Page.data.entity.save();

Save and Close function
Xrm.Page.data.entity.save("saveandclose");

Save and New function
Xrm.Page.data.entity.save("saveandnew");

Close function
Xrm.Page.ui.close();

Get Form Type

var formtype = Xrm.Page.ui.getFormType();




Wednesday, January 14, 2015

Dynamics CRM 2013 - Show custom page inside an iframe CRM 2013

1. Create a iframe inside the CRM form.

























2. Create a web resources (JS)


function putiframe()
{
var id= Xrm.Page.data.entity.getId();
var getform = Xrm.Page.ui.getFormType();


var host = window.location.protocol + '//' + window.location.hostname + 'port number' + '/SA/RichTextBox.aspx?id=' + id+ '&getform=' + getform ;
if ( getform != 1)
{
Xrm.Page.getControl("iframename").setSrc(host);

}
}

3, Save and Publish  and set onload event to call the javascript.

Dynamics CRM 2013 - OData Query

First, please go to https://crm2011odatatool.codeplex.com/ download the odata query designer solution and import into your CRM. Once you published the solution, go to Setting and look for Odata Query Designer.

1. Select the entity and the filter criteria based on your business requirements.













2. Click Generate




3. Now we going to create a web resources (JS),passing the value dynamically.

function Odata()
{
//Get Opportunity Id
var formType = Xrm.Page.ui.getFormType();
Master= Xrm.Page.getAttribute("crmfieldschemaname").getValue();

var guid = Master[0].id;
OdataCode(guid);

}

function OdataCode(guid)
{
  var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
  crmServerUrl = Xrm.Page.context.getServerUrl();
         var oDataUri =crmServerUrl+ODATA_ENDPOINT+"replace your code here";     jQuery.ajax({
       type: "GET",
       contentType: "application/json; charset=utf-8",
       datatype: "json",
       url: oDataUri,
       beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
       success: function (data, textStatus, XmlHttpRequest)
           {
               // Use only one of these two methods

               // Use for a selection that may return multiple entities

               ProcessReturnedEntities(data.d.results);
           },
       error: function (XmlHttpRequest, textStatus, errorThrown) {  alert("Ajax call failed: " + textStatus + " - " + errorThrown + " || " + XmlHttpRequest.responseText);}
   });
}

function ProcessReturnedEntities(ManyEntities)
{
  var grandTotal = 0;
  for(var i=0; i< ManyEntities.length; i++)
  {
     var oneEntity = ManyEntities[i];
     var totalPriceAttribute = oneEntity."the field that you want to select";                
}

}

4. thing to remember when you replace the filter value dynamically.


 var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
 crmServerUrl = Xrm.Page.context.getServerUrl();
       
var oDataUri =crmServerUrl+ODATA_ENDPOINT+"/new_petitionSet?$filter=new_CurrentMasterName/Id eq guid'"{remember to have a space here}+guid+"' and new_PetitionStatus/Id eq guid'50992E47-5518-E311-9CC8-005056A64D95'";