Show entity form based on a field value in Microsoft Dynamics CRM /PowerApps

 

Having multiple forms for an entity is definitely amazing since you can control visibility of fields without having to write business rule or Javascript to hide hundreds of fields based on some value.

Now out of the box you can control CRM forms visibility/preference using security roles. However it is not you would always want to to do; sometimes you might have to control form preferences based on field value.

here is the sample code that i am using – on load of create/update/all forms i am retrieving a value from owner of that record – since on creation only owner gets filled up.  I have create a field “new_form” as a whole number on system user :

Show entity form based on a field value in Microsoft Dynamics CRM /PowerApps

 

i have currently have it a value as “2”. lets see how i am going to retrieve this value in my code and set the correct form :

//do a webapi action to retrieve the form value from systeuser.
var formnewvalue = ‘ ‘;
function retrieveFromValue() {
debugger;
var userid = Xrm.Page.getAttribute(“ownerid”).getValue();
var newid = userid[0].id.slice(1, -1);
var req = new XMLHttpRequest();
req.open(“GET”, Xrm.Page.context.getClientUrl() + “/api/data/v8.2/systemusers(” + newid + “)?$select=new_form”, true);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“Prefer”, “odata.include-annotations=\”*\””);
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
var new_form = result[“new_form”];
var new_form_formatted = result[“new_form@OData.Community.Display.V1.FormattedValue”];
formnewvalue = new_form_formatted;
showForm();
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
function showForm() {
debugger;
var lblForm;
var relType = formnewvalue
switch (relType) {
case “1”:
lblForm = “Portal Contact”;
break;
case “2”:
lblForm = “Profile Web Form”;
break;
default:
lblForm = “Contact”;
}
//check if the current form is form need to be displayed based on the value
if (Xrm.Page.ui.formSelector.getCurrentItem().getLabel() != lblForm) {
var items = Xrm.Page.ui.formSelector.items.get();
for (var i in items) {
var item = items[i];
var itemId = item.getId();
var itemLabel = item.getLabel()
if (itemLabel == lblForm) {
//navigate to the form
item.navigate();


}
}

if you notice my switch statement you will see i m assigning the name of the forms to the values that i will be setting on the user profile.

Once it gets the form name – it than runs through all the forms for this entity untill it fins the correct form and then navigates to it.

i would say this script is majorly needed for first time or for a new system – since Internet Explorer or Chrome keep the form preference , so once our code the job and opened the required form -next time even if we remove the script -it will opening that form.

Another good is – this script prevents user to switch to other form as whenever  a user try to switch the form – on load event triggers and again check the value from owner id and change it back to the required one.

so since i have assigned value “2” to my user i should see “Profile Web Form” , lets see if this works :

Show entity form based on a field value in Microsoft Dynamics CRM /PowerApps

 

woo , it did work.

point to note is you will have to attach this script to all the forms in the entity and call the function on onload for all forms.

Show entity form based on a field value in Microsoft Dynamics CRM /PowerApps

上一篇:android studio: android studio 4.2 不显示 gradle task list 问题


下一篇:什么是CLI?