lingdanglfw(DAX)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Use JavaScript in Dynamics CRM to Set the Value in Read-Only Field on a Form for Dynamics 365 / PowerApps

We've covered some great ways to use JavaScript in Dynamics CRM 2011 in previous posts. This post focuses on a use that we recently employed for a client. In Microsoft Dynamics CRM 2011 or 4.0, you may want to use JavaScript in to set the value in read-only field on a form. However, you may have noticed that after the JavaScript sets the field to Read-Only, it does not save the value when the record is saved.

To clarify, consider the following scenario.

On the Account form, your goal is to have the "Account Number" field become Read-Only if a checkbox is checked. In this example, we'll name the checkbox "Freeze." What you may do to accomplish this is to have the "Account Number" field become Read-Only onChange of the "Freeze" checkbox. You would also want this JavaScript to fire onLoad as well. This will accomplish what you're attempting to do…almost.

Now, when you deploy this code (to a test system of course), you find something strange happens. Let's say a user edits the "Account Number" field, then checks the "Freeze" box immediately after. Upon saving the record, the "Account Number" field reverts back to the previous value. Why is this? Well, prior to checking the "Freeze" field, the "Account Number" field was editable. If we edit that field on the form, the actual field in the database is not modified until we save the record. So, if we edit the field, check the "Freeze" box then Save the record, the value will not be changed because the "Account Number" field was set as Read-Only as soon as we check the box (from the onChange event) prior to saving the record.

To meet this requirement, you could use option #1 below, or use a combination of the two.

#1    Have the JavaScript code fire onLoad of the form ONLY. Not onChange of the "Freeze" field.

Here is the code for the onLoad event:

function accountNumberReadOnly()
{
var freeze = Xrm.Page.data.entity.attributes.get("po_freeze");
var optionSetValue = freeze.getValue();
var optionSetText = freeze.getText();
if (optionSetText == "yes")
{ Xrm.Page.ui.controls.get("accountnumber").setDisabled (true) }
else
{ Xrm.Page.ui.controls.get("accountnumber").setDisabled (false) }

The downside of this method alone is that even though the user checks the box, the Account Number field is not yet Read-Only. To resolve this, add step #2 to the equation.

#2    In your onChange event for the "Freeze" field, instead of using the code above, simply utilize some code to "Save" the record. When a user checks the "Freeze" box, the record is saved and reloaded. The code in #1 will then fire onLoad, thus setting he "Account Number" field as Read-Only.

Here is the code for the onChange event:

function saveRecord()
{
var freeze = Xrm.Page.data.entity.attributes.get("po_freeze");
var optionSetValue = freeze.getValue();
var optionSetText = freeze.getText();
if (optionSetText == "yes")
{ Xrm.Page.data.entity.save(); }
}

Didn't get your fill of JavaScript yet? Here are some more posts on how to use JavaScript in Dynamics CRM

posted on   lingdanglfw  阅读(127)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2011-07-16 call webservice
2008-07-16 Beginner
点击右上角即可分享
微信分享提示