I am not going into the details of how to add JS to CRM code, but I can recommend looking at http://blogs.inetium.com/blogs/azimmer/archive/2009/03/08/five-tips-for-productive-form-scripting-in-crm-4-0.aspx
First, I needed a function that would add and remove text from the "comment" field. I loaded this in the form OnLoad event:
function updateComment1 (tf, cComment){
try {
if (tf == true) {
if(crmForm.all.new_comments.DataValue != null && crmForm.all.new_comments.DataValue.length > 0){
crmForm.all.new_comments.DataValue = crmForm.all.new_comments.DataValue + Chr(13)+ cComment;
}else{
crmForm.all.new_comments.DataValue = cComment;
}
}else{
crmForm.all.new_comments.DataValue = crmForm.all.new_comments.DataValue.replace(cComment,"");
crmForm.all.new_comments.DataValue = crmForm.all.new_comments.DataValue.replace(/\r\n\r\n/m,"\r\n");
}
}catch(err){
// do nothing - the comments are empty and we dont care
}
}
This code will take two arguments: one will tell it what to do, and the second is the string to either insert or remove from the faux comment field. If the first argument is true, then the string is inserted into the comment field, otherwise the string is removed (if it exists). The caveat to this is that if the user changes the spelling of the string, then this will not remove it.
Next I created a function to accept the pull-down field as an argument and handle sending the update to the first function:
function updateComment2 (oField) {
updateComment1(false, oField.prev_value) // this will remove the old text from comments
if(oField.SelectedText.substr(0,7)!="No Pref"){
updateComment1(true, oField.SelectedText) // this will put the new text in the field
}
oField.prev_value = oField.SelectedText; // reest the previous value for the next time around
}
In this system, the pull-down has a default value of "No Pref" which means the user did not have a preference for this item, and the reference to this should be removed from the comment field. Notice that there is also a property of the field called "prev_value" which is not normally part of MS CRM form fields. When the form loads, I added a couple lines of code to capture the current state of these controls:
crmForm.all.new_preference1.prev_value = crmForm.all.new_preference1.SelectedText;
crmForm.all.new_preference2.prev_value = crmForm.all.new_preference2.SelectedText;
Using this trick will allow you to add any number of new properties to a form field.
Then in each of the fields OnChange events, I added a call to my updateComment2 function. For example, the OnChange event of the new_preference1 field would look like:
updateComment2(crmForm.all.new_preference1);
When the user changes the pull-down, it calls my custom function updateComment2 and passes a reference to itself which I will use to inspect its previous value and remove the old the string from the comment field before adding the new selected text back in.
I could have combined some of this code, but have it broken down this way because I am using these functions in other places in the form by checkboxes, radio buttons and such and this gives me some flexibility to re-use code.
No comments:
Post a Comment