Posts

Showing posts with the label JavaScript

Most Used JavaScript Syntax used in MS CRM

In this blog we will see how we can retrieve different data types fields values in MS CRM using JavaScript. Different types of data types in MS CRM: Single Line of Text Option Set (Dropdown) Two Options (Radio Button) Image Whole Number Floating Point Number Decimal Number Currency Multiple Lines of Text Date and Time Lookup  Single or Multiline Field :  var name = formContext.getAttribute("new_name").getValue(); formContext.getAttribute("new_name").setValue(); Option-Set Field :  int value = formContext.getAttribute("new_type").getValue(); var optionsetText = formContext.getAttribute("new_type").getText(); formContext.getAttribute("new_type").setValue(100); //100 will be the optionset value Multi-Select Option-Set Field :  var multiselectValueArr = formContext.getAttribute("new_multiselect").getValue(); formContext.getAttribute("new_multiselect").setValue([100, 200, 300]); Two-Option Field :  var value = formConte...

Open custom page using JavaScript

Image
Custom pages are low code page type within model-driven apps. Custom pages bring the power of canvas apps into model-driven apps. Once we develop any custom page we will get its unique name from solution, that name of page will be used in JavaScript. We can also pass some data to custom page as a parameter which will be used by custom page for further processing. JavaScript snippet to call custom page: function callCustomPage(selectedItems) { var item = selectedItems; let pageInput = { pageType: "custom", name: "new_custompage_bb4a2", entityName: "incident", recordId: item, }; let navigationOptions = { target: 2, width: 400, height: 350, }; Xrm.Navigation.navigateTo(pageInput, navigationOptions) .then(function () { // Handle success }) .catch(function (error) { // Handle error }); } Here we are passing recordId as a additional parameter to custom page. Note: Please make sure to add cus...

Customize OOB Subgrid to show records based on dynamic filter criteria

  OOB dynamic CRM subgrids are limited to static filter criteria's only. If we want to add any dynamic filter criteria to filter records on subgrid then it's not possible with normal subgrid configuration. However, we can achieve such requirements by customizing fetchxml of view present on subgrid using JavaScript. eg. If we want to filter accounts on case entity by matching criteria (Current case record Case number = All accounts with matching Account number i.e [case number(case) = account number (account)]) then we can achieve such requirements by using below JavaScript Add below JS on load of case entity. so that on load it will get case number and filter account subgrid dynamically function filterAccountSubgridInCase(executionContext) { var formContext = executionContext.getFormContext(); var gridContext = formContext.getControl("Subgrid_Accounts"); var caseNumber = formContext.getAttribute("new_casenumber").getValue(); //customizing ...

Custom Subgrid Using HTML Web resource

Sometimes you will get requirement to show records in subgrid without any proper relationship. In such scenario you can go with custom subgrid and show all those related records in HTML table that won't be possible with OOB way of ms crm Below is the sample code for HTML web resource for creating sample custom subgrid Here we are showing all accounts in Case entity where matching criteria is Case Number = Account number(that too without any relationship between Case and account) html><head><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta...

Trigger Workflow Using JavaScript

 In Microsoft Dynamics 365, we can trigger the workflow based on OOB condition(eg. record is created or on change of field, etc). If we want to trigger the workflow on specific condition which is not present as a OOB then we can use JavaScript to trigger the workflow. You can follow following code for executing workflow using JavaScript: ExecuteWorkflowUsingJS: function(executionContext) { var formContext = executionContext.getFormContext(); var ExecuteCustomWorkflowRequest = function(entityID, WorkflowID) { this.EntityId = { "guid": entityID }; this.entity = { id: WorkflowID, entityType: "workflow" }; this.getMetadata = function() { return { boundParameter: "entity", parameterTypes: { "entity": { "typeName": "Microsoft.Dynamics.CRM.workflow", ...

Web API for Create, Update, Delete record

  1. Create Web API Request :  Syntax:  Xrm.WebApi.createRecord(entityLogicalName, data).then(successCallback, errorCallback); //json object var entity = {}; entity.address1_country = "India"; entity["areaid@odata.bind"] = "/areas(0b4ecbb5-e574-eb11-a812-0022481a9090)"; //lookup field Xrm.WebApi.online.createRecord("account", entity).then( function success(result) { var newEntityId = result.id; }, function(error) { Xrm.Utility.alertDialog(error.message); } ); 2. Update Web API Request :  syntax :  Xrm.WebApi.updateRecord(entityLogicalName, id, data).then(successCallback, errorCallback); //json object var entity = {}; entity["areaid@odata.bind"] = "/areas(0b4ecbb5-e574-eb11-a812-0022481a9090)"; //lookup field entity.address2_country = "India"; Xrm.WebApi.online.updateRecord("account", "0b4ebjb5-e574-eb11-a812-0022481a9090", entity).then( function success(re...

Query Data Using Web API

Sometimes you need to retrieve the data of different entity using javaScript and use that data to perform some business logic. For this Purpose you can use either XmlHttp request or Web API request. In this blog we will see how to retrieve the single record using Web API 1. Retrieve Single Record Using Web API :  Parameters used in webApi to retrieve single record are :  1. Entity logical name,  2. Guid of record,  3. columns which needs to retrieve from that record. Xrm.WebApi.online.retrieveRecord("account", "0b4ecbb5-e574-eb11-a812-0022481a9090", "?$select=address1_city,address1_country").then( function success(result) { var address1_city = result["address1_city"]; var address1_country = result["address1_country"]; //perform your business logic here }, function(error) { Xrm.Utility.alertDialog(error.message); } ); You can get GUID of record which needs to retrieve using lookup field p...

Check Dirty Fields or Forms in Dynamics 365

 In Dynamics 365, sometimes there is requirement to check if field value/form is changed or not. In that case we can use getIsDirty Client API reference which will return value true if field or form value is changed. Syntax: formContext.data.entitygetIsDirty();   //Form is dirty formContext.data.entity.attributes.get(arg).getIsDirty();  //field is Dirty To get the list of attributes that have been modified in this session, you can check the IsDirty of the attribute :  getListOfDirtyAttributesOnForm: function(executionContext) { var formContext = executionContext.getFormContext(); var AccountAttribute = formContext.data.entity.attributes.get(); if (AccountAttribute != null) { for (var i in AccountAttribute) { if (AccountAttribute[i].getIsDirty()) { listOfAttributes += AccountAttribute[0].getName() + ", "; } } } }

Accessing Fields on QuickView Form through javaScript

QuickView form is template to view information about related entity record within a form for another entity record. Sometimes you need to access data from QuickView form for performing business logic. In order to get QuickView form in js you can use following syntax :  Syntax :  formContext.ui.quickForms.get(arg); Following is the example how you can access field from QuickView Form :  getAttributeValueFromQuickViewForm: function(executionContext) { var formContext = executionContext.getFormContext(); var quickViewControl = formContext.ui.quickForms.get("quickFormName"); if (quickViewControl != undefined) { if (quickViewControl.isLoaded()) { //not necessary to use this condition quickViewControl.getControl("new_name").getAttribute().setValue("Abhishek Dhandare"); //to get value used below syntax quickViewControl.getControl("new_type").getAttribute().getValue(); //perform your ...

Get Current User's Security Roles

 To get Current user information, you can use getGlobalContext.userSettings client API reference. Sometimes business wants to perform some business logic based on particular user's security role, in that case you can get security role of current user and perform your logic using below client API reference. You can follow following link for more details about current user properties:  Current User Properties Following is the example of how you can restrict your business logic to particular user's by getting their security roles:  getCurrentUserSecurityRole: function(executionContext) { var formContext = executionContext.getFormContext(); var roles = Xrm.Utility.getGlobalContext().userSettings.roles; roles.forEach(function(item) { if (item.name.toLowerCase() === "Your Security Role Name") { //perform your business logic here! } } }); }

Get Current Model-Driven App Properties

To perform business logic or restrict your business logic to particular app only, you can use microsoft cliet API reference getCurrentAppProperties.  you can refer below link for different properties of model driven app :  Model-driven app properties Following is the example how you can use app properties to restrict your business logic:  getAppName: function(executionContext) { var formContext = executionContext.getFormContext(); var globalContext = Xrm.Utility.getGlobalContext(); globalContext.getCurrentAppProperties(). then( function successCallback(app) { var appName = app.uniqueName; var appId = app.appId; alert("app name : " +appName+ " app Id : " +appId); if(appName == "Your app name") { //perform your business logic here } }, function errorCallback() { console.log("Error"); } ); },

Field Level Notification

  Field level notifications are mostly used for  displaying an error message for the field/control to indicate that data isn't valid. If you want to give field level notification using javaScript then you can use following syntax: Syntax :  formContext.getControl(arg).setNotification(message, uniqueId); message  - to display message. uniqueId - to clear the message Following is the example of use of field-level notification :  FormLevelNotification: function(executionContext) { var formContext = executionContext.getFormContext(); var age = formContext.getAttribute("age").getValue(); if(age != null && age.getValue() < 18) { //set otification to field formContext.getControl("age").setNotification("Age should be greater than 18", "200") //200 is uniqueId formContext.getAttribute("age").setValue(null); } else { //clear notification from field formContext.getContro...

Form Level Notification

 If  you wan to show multiple notifications on form or you wan to clear notification from form then you can use following syntax :  Syntax :  formContext.ui.setFormNotification(message, level, uniqueID); message   -  message which will be displayed on form. level         -  There are 3 types of level (ERROR, INFO, WARNING).                       Level will decide notification icon. UniqueID - it is used when user want to clear notification. Following is the example of setting and clearing notification on form: FormLevelNotification: function(executionContext) { var formContext = executionContext.getFormContext(); //Set notifications formContext.ui.setFormNotification("Messsage for ERROR notification. ", "ERROR", "errorid"); formContext.ui.setFormNotification("Message for WARNING notification. ", "WARNING", "warningid"); formContext.ui.se...

Show/Hide - field/section/tab on entity form

  In Dynamics 365, You can show/hide fields or tabs or sections based on your business logic. Syntax for field:  formContext.getControl("firstname").setVisible(false);   //Hide field firstname formContext.getControl("firstname").setVisible(true); //Show field firstname syntax for tab:  formContext.ui.tabs.get("Tab_name").setVisible(true);   //Show tab on form formContext.ui.tabs.get("Tab_name").setVisible(false); //hide tab on form Syntax for section:  formContext.ui.tabs.get("Tab_name").sections.get("section_name").setVisible(true);

Lock/Unlock Field on Form/BPF

There are 2 ways to Lock or Unlock the field on form: 1. Using Business Rule Business Rule is simplest way to lock/unlock the field on form. It can be also used to show/Hide field on form or showing any error message on form or making field mandatory/non-mandatory. Note: Business Rule can not work on fields present in Business Process Flow(BPF). 2. Using JavaScript :  Following steps are used to lock/Unlock field on form:  Use formContext object model get control of field using formContext object model use setDisabled property to lock/unlock the field Syntax:               formContext.getControl(arg).setDisabled(bool); arg: Schema name of field bool: true for locking the field and false for unlocking the field In order to Lock/Unlock the field in BPF, use "header_process_schemaname" as a argument. eg. if First Name field with schema name as firstname  is present in BPF and you have to lock the First Name field in BPF, then use ...

Use of FormContext

Microsoft Dynamics CRM offers an object model (formcontext) to manipulate, analyze state of form and data. The formcontext container provides properties and methods to work with the data on a form. Following are the ways to get/set data from different data-types field using formcontext object model :  1. Single Line or Multiple line of Text :  getTextFieldValue: function(executionContext) { var formContext = executionContext.getFormFcontext(); var getTextValue = formContext.getAttributes("firstname").getValue(); //get text value var setTextValue = formContext.getAttributes("title").setValue("Write Text here!"); //set text value } 2. OptionSet Value :  getOptionsetValue: function(executionContext) { var formContext = executionContext.getFormFcontext(); var getOptionsetValue = formContext.getAttributes("FieldSchemaName").getValue(); //return code of optionset text var getOptionsetText = formContext.getAttributes("FieldSchemaName...