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 :
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 :
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.
posted on 2021-07-23 14:31 lingdanglfw 阅读(21) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2008-07-23 Re-Located Record in Grid