Posts

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...