It is recommended to use Business Rules to replace the functionalities of JavaScript. Yet, BRs got a lot of limitations, but it can be used for some of the most used aspects like controlling the UI of forms.
Anyway, one of the cool things we did using JS is controlling field visibility and business required levels based on Create Form and Update Form. There is a simple way of achieving the same with BR.
What we do here is checking the CreateOn date. Obviously, we don’t have a value in this field till save the record. Below simple condition shows how the Description field is hidden for Create Form and showing for Update Form.
Caution
Anyway, there is one trick here. Business Rule will only work if CreateOn field is present in the form. What we need to do is, just get the CreateOn into form, but set Visible by Default = No to hide it.
Nov 21, 2019
Oct 8, 2019
Passing Unsecure Configurations to a Plugin
We discussed how to pass Configurations to a JavaScript in a previous post. Click this to read that.
Lets see how we can pass Configurations to a Plug-in. In this scenarios, we'll see how we pass some Key Value pairs to a plug-in written for Lead entity.. We are going to store these values in a XML format within Unsecure Configuration section in registered plugin step.
This is the XML data format.
Here is the Plug-in code that reads above values to be used in whatever the logic within the Plug-in. Interestingly, you will notice how we use a constructor class where configurations are being read within. Then we use GetUnsecureConfigValue() method to read each value in the XML.
Caution
If you have lengthy values in XML, there is a chance that line-breaks and spaces being added without your intention. In such cases, you may use some Regular Expression functions to omit those. For above code, I have read the configuration data in two ways and commented out one. If explained issue likely to be hitting your situation, you may use the commented line instead of what is used.
Anyway, this method of passing configuration data is can be used to keep parameters differently for different environments. What is important to know is solution imports are overriding these vales.
Lets see how we can pass Configurations to a Plug-in. In this scenarios, we'll see how we pass some Key Value pairs to a plug-in written for Lead entity.. We are going to store these values in a XML format within Unsecure Configuration section in registered plugin step.
This is the XML data format.
<leadConfig> <setting name="RegionCode" value="000X23AA55" /> <setting name="IntegrationKey" value="1200-6753-0980-0901" /> </leadConfig>
Here is the Plug-in code that reads above values to be used in whatever the logic within the Plug-in. Interestingly, you will notice how we use a constructor class where configurations are being read within. Then we use GetUnsecureConfigValue() method to read each value in the XML.
using System; using Microsoft.Xrm.Sdk; using System.ServiceModel; using System.Xml; namespace TrialPlugin { public class LeadPreCreate : IPlugin { private string UnsecureConfig { get; set; } public LeadPreCreate(string unsecureConfig, string secureConfig) { if (string.IsNullOrEmpty(unsecureConfig)) throw new InvalidPluginExecutionException("Plugin Configuration missing."); UnsecureConfig = unsecureConfig; } public void Execute(IServiceProvider serviceProvider) { try { IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity TargetEnt = (Entity)context.InputParameters["Target"]; if (TargetEnt.LogicalName != "lead") return; IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = factory.CreateOrganizationService(context.UserId); string regionCode = GetUnsecureConfigValue("RegionCode"); //string regionCode = Regex.Replace(GetUnsecureConfigValue("RegionCode"), @"\t|\n|\r", "").Replace(" ", String.Empty); string integrationKey = GetUnsecureConfigValue("IntegrationKey"); //string regionCode = Regex.Replace(GetUnsecureConfigValue("IntegrationKey"), @"\t|\n|\r", "").Replace(" ", String.Empty); throw new InvalidPluginExecutionException("RegionCode: " + regionCode + ", IntegrationKey: " + integrationKey); } } catch (FaultException<OrganizationServiceFault> e) { throw e; } } private string GetUnsecureConfigValue(string key) { const string XPATH = "leadConfig/setting[@name='{0}']"; string configVal = string.Empty; try { var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(UnsecureConfig); var node = xmlDoc.SelectSingleNode(string.Format(XPATH, key)); return node == null ? configVal : node.Attributes["value"].Value; } catch { return configVal; } } } }
Caution
If you have lengthy values in XML, there is a chance that line-breaks and spaces being added without your intention. In such cases, you may use some Regular Expression functions to omit those. For above code, I have read the configuration data in two ways and commented out one. If explained issue likely to be hitting your situation, you may use the commented line instead of what is used.
Anyway, this method of passing configuration data is can be used to keep parameters differently for different environments. What is important to know is solution imports are overriding these vales.
Labels:
C#,
Dynamics 365,
Plug-in
Subscribe to:
Posts (Atom)