[英]change the color of field based on a value crm 2011
我在CRM表單中有一個“出生日期”字段和一個“年齡”字段。 保存記錄后,將計算年齡,如果年齡小於18歲,則“年齡”字段應變為紅色。
當我保存表單時,該字段會變成紅色一秒鍾,然后又恢復為正常。 我的網絡資源正在使用表單的OnSave事件。 當在“年齡”字段的ONChange事件上使用更改顏色代碼時,我必須保存兩次表單以顯示它變為紅色。
如何擺脫兩次保存表單的麻煩? 這是我的代碼:
function setAge()
{
var DOB = Xrm.Page.getAttribute("inmate_dob").getValue();
var Today = Xrm.Page.getAttribute("inmate_bookingdate").getValue();
Today.setHours(0, 0, 0, 0);
var db = 0;
if(DOB > Today )
{
alert("Please Enter Genuine BirthDate !!!");
Xrm.Page.getAttribute("inmate_dob").setValue(null);
}
else
{
db = Today.getFullYear() - DOB.getFullYear();
var x = Today.getDate() ;
var y = DOB.getDate() ;
var a = Today.getMonth() + 1;
var b = DOB.getMonth() + 1;
if((a < b) || (a==b & x < y))
db=db - 1;
}
var result = Xrm.Page.getAttribute("inmate_age").setValue(db);
if (db <18)
{
document.getElementById("inmate_age").style.backgroundColor = 'red';
document.getElementById("inmate_age_c").style.backgroundColor = 'red';
}
}
}
我還嘗試過對Age字段的ONChange事件單獨使用最后一個IF條件。 這提示我將記錄保存兩次。
您不能計算字段onChange嗎?
例如
Xrm.Page.getAttribute('inmate_age').addOnChange(function() {
var theyAre18 = yourFunctionToCalculateIfTheyAre18OrNot();
if (theyAre18) {
//make the background white
}
else {
//make the background red
}
});
這樣,您可以在加載后立即運行它,並且僅在值更改時才更改。 當您更改顏色onSave時,無論如何該表單都將重新加載,並且所做的更改將丟失。
我認為您的特定錯誤與以下事實有關:保存后,表單會刷新,而失去設置的顏色。 您也需要在onLoad上進行計算。
Basically what happened is:
OnSave -> Calculate
Form refresh -> Lose the color
OnLoad -> you need to re-apply your script
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.