Tech Support > Microsoft Windows > Windows CRM > OnSave JavaScript elements not running
OnSave JavaScript elements not running
Posted by Mahmood on October 17th, 2007


Hi All

We have been running Microsoft CRM in the firm for just over a year. We have
experienced numerous problems but thankfully many of them have been sorted
out. Custom JavaScripts for the OnLoad and OnSave event on Company and
Contact forms were created and were running beautifully until...

Update Rollup 2 was installed two (2) weeks ago.

For some reason, our OnSave code does not run certain elements contained
within it. There are no errors within the code because the code did run
before the installation of Rollup 2.

No error messages are being reported, the system shows something running...
but no results appear.

Has anyone experienced this problem? Is there something we are missing after
the installation of Rollup 2?

Any help would gladly be appreciated as a lot of our code simplifies matters
for our users.

Thanking you
Mahmood

Posted by MD on October 17th, 2007


Could you post the code?

"Mahmood" wrote:

Posted by Mahmood on October 18th, 2007


Hi MD

Here is the code for the Account OnSave (called "Company" in our case):

var validationErrors = false;

// 1.
try {
if (crmForm.new_profile.value== "4" &&
crmForm.new_mediacategory.value.length == 0) {

alert("If Profile is 'Media', Media Category must also be populated");
event.returnValue = false;
validationErrors = true;

}

} catch (ex) {
}

// 2.
try {
if(crmForm.customertypecode.value == "15" &&
crmForm.new_estimatedannualfees.value.length > 0) {

alert("If Relationship Type is 'Prospect', Estimated Annual Fees can not be
populated");
crmForm.new_estimatedannualfees.focus();
event.returnValue = false;
validationErrors = true;

}

} catch (ex) {
}

// 3.
try {
/* Special case, the Client Partner had white space in it, so I trim before
doing length comparisons, clearly JScript doesn't do trim, so the Regex is
the next neatest thing.

Special case 2. This does not apply to the create form, as the lookup
functionality doesn't work for non existent clients*/
var strResult;
var objRegex = new RegExp("(^\\s+)|(\\s+$)");
var strResult =
document.frames("IFRAME_ClientPartner").document.a ll("txtLink").innerText.replace(objRegex, "");

if ((crmForm.customertypecode.value == "13" ||
crmForm.customertypecode.value == "16") && (strResult.length == 0 ||
crmForm.new_primaryservice.value.length == 0) && (crmForm.FormType != 1 &&
crmForm.FormType != 5)) {

alert("Each Corporate Client must have a Client Partner, and a Primary
Service.\nThis includes 'Corporate Clients' and 'Corporate Responsibility'
Relationship Types.");
event.returnValue = false;
validationErrors = true;

}

} catch (ex) {
}

// 4.
try {
var clientPartner =
document.frames("IFRAME_ClientPartner").document.a ll("txtLink").innerText;
crmForm.new_clientpartner.value = clientPartner;

var PreviousClientPartner =
document.frames("IFRAME_PreviousClientPartner").do cument.all("txtLink").innerText;
crmForm.new_previousclientpartner.value = PreviousClientPartner;

var TaskPartner=
document.frames("IFRAME_TaskPartner").document.all ("txtLink").innerText;
crmForm.new_taskpartner.value = TaskPartner;

} catch (ex) {
}

// 5. Welcome Email
try {
if ((!validationErrors) && (crmForm.new_customertypecodeprevious.value ==
"Not Defined" &&
crmForm.customertypecode[crmForm.customertypecode.selectedIndex].text ==
"Corporate Client") || (crmForm.new_customertypecodeprevious.value == "Not
Defined" &&
crmForm.customertypecode[crmForm.customertypecode.selectedIndex].text ==
"Corporate Responsibility")) {

var entityId = crmForm.ObjectId;
var entityType = crmForm.ObjectTypeCode;

var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false; // We want to wait for a response

oXmlDoc.load("http://crm/ispcustomlookup/StartWorkflow.aspx?strProcessName=CompanySendEmail ReminderTask&strEntityID="
+ entityId + "&strEntityType=" + entityType);
var oNode = oXmlDoc.selectSingleNode("result");

if (oNode.text == "error") {
alert("An error occured while creating the 'Send Email Reminder' task
for Marketing.\nPlease contact the System Administrator to ensure that this
error is resolved.");
} else {
alert("The 'Send Email Reminder' task has been assigned to Marketing.");
}

}


} catch (ex) {
}

//1 and //2 work fine. Thereafter, it basically does nothing.

And the code for the Contact OnSave:

var validationErrors = false;

// 1.
try {
if (crmForm.customertypecode.value == 6 &&
crmForm.new_internalclientcategory.value.length == 0) {

alert("If 'Internal Client' is selected as a Relationship Type, then
Internal Client Category must be populated");
event.returnValue = false;
validationErrors = true;

}

} catch (ex) {
}

// 2.
try {
if (crmForm.new_profile.value== "4" &&
crmForm.new_mediacategory.value.length == 0) {

alert("If Profile is 'Media', Media Category must also be populated");
event.returnValue = false;
validationErrors = true;
}

} catch (ex) {
}

// 3.
try {
if (crmForm.FormType == 1) //CRM_FORM_TYPE_CREATE
{
// Get the current Contact Name and Surname and encode it for safe
URL transmittal
var firstName = encodeURIComponent(crmForm.all.firstname.DataValue );
var lastName = encodeURIComponent(crmForm.all.lastname.DataValue) ;

//alert("extracted " + firstName + lastName);

var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false; // We want to wait for a response

oXmlDoc.load("http://crm/ISPCustomLookup/DuplicateCheck.aspx?firstname=" +
firstName + "&lastname=" + lastName);
var oNode = oXmlDoc.selectSingleNode("result");

//alert(oNode.text);

if (oNode != null && oNode.text == "True")
{

if(!confirm("A Contact with this name and surname already
exists in the system.\nAre you sure that you like to create a duplicate
entry?"))
{
event.returnValue = false;
validationErrors = true;
}

}
}


} catch (ex) {
}

// 4. Client Partner
try {
var clientPartner =
document.frames("IFRAME_ClientPartner").document.a ll("txtLink").innerText;
crmForm.new_clientpartner.value = clientPartner;

var previousclientPartner =
document.frames("IFRAME_PreviousClientPartner").do cument.all("txtLink").innerText;
crmForm.new_previousclientpartner.value = previousclientPartner;

var taskPartner=
document.frames("IFRAME_TaskPartner").document.all ("txtLink").innerText;
crmForm.new_taskpartner.value = taskPartner;


} catch (ex) {
}

// 5. Private Client --> Client Partner Link
try {
// Special case, the Client Partner had white space in it, so I trim before
doing length comparisons, clearly JScript doesn't do trim, so the Regex is
the next neatest thing.
// Special case 2. This does not apply to the create form, as the lookup
functionality doesn't work for non existent clients

var strResult;
var objRegex = new RegExp("(^\\s+)|(\\s+$)");
var strResult =
document.frames("IFRAME_ClientPartner").document.a ll("txtLink").innerText.replace(objRegex, "");

if ((crmForm.customertypecode.value == "3") && (strResult.length == 0 ||
crmForm.new_primaryservice.value.length == 0) && (crmForm.FormType != 1 &&
crmForm.FormType != 5)) {

alert("Each Private Client must have a Client Partner, and a Primary
Service.\nThis includes the 'Private Client' Relationship Type.");
event.returnValue = false;
validationErrors = true;
}
} catch (ex) {
}

// 7. Welcome Email
try {
if ((!validationErrors) && (crmForm.new_customertypecodeprevious.value ==
"Not Defined" &&
crmForm.customertypecode[crmForm.customertypecode.selectedIndex].text ==
"Private Client")) {

// GRAB THE ID OF THE CURRENT ENTITY FROM THE ADDRESS BAR
var entityId = crmForm.ObjectId;
var entityType = crmForm.ObjectTypeCode;

var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false; // We want to wait for a response

oXmlDoc.load("http://crm/ispcustomlookup/StartWorkflow.aspx?strProcessName=ContactSendEmail ReminderTask&strEntityID="
+ entityId + "&strEntityType=" + entityType);
var oNode = oXmlDoc.selectSingleNode("result");

if (oNode.text == "error") {
alert("An error occured while creating the 'Send Email Reminder' task
for Marketing.\nPlease contact the System Administrator to ensure that this
error is resolved.");
} else {
alert("The 'Send Email Reminder' task has been assigned to Marketing.");
}

}


} catch (ex) {
}

Thanks for your assistance.
Mahmood



"MD" wrote:

Posted by Jim Daly [MSFT] on October 19th, 2007


I see a several issues here:

1. The scripts reference form values as if they were standard HTML pages
rather than by using the methods documented in the MSCRM SDK. For example I
see

crmForm.customertypecode.value rather than
crmForm.all.customertypecode.DataValue

See http://msdn2.microsoft.com/en-us/library/aa680857.aspx

"Note Syntax differences between Microsoft Dynamics CRM version 1.2 and
version 3.0 in may cause client-side script failures after upgrade. The
following is a list of syntax changes:

The syntax crmForm.all.(fieldname) is now required when making a call to
field level methods and properties.
The value property has been replaced with the property DataValue. "

2. It looks like some of the code is attempting to update the page hosted
within the IFrame. For example:
document.frames("IFRAME_ClientPartner").document.a ll("txtLink").innerText.replace(objRegex, "");

I thought that this was not allowed. This is a security restriction that is
set within Internet Explorer for all IFRAME elements.

3. I see
crmForm.new_estimatedannualfees.focus();
This should be
crmForm.all.new_estimatedannualfees.SetFocus();

Generally - It seems that the code needs a review and should be scrubbed to
comply with the supported way to reference data in CRM forms.

You should also probably look to debugging the code.

Michael Hohne has a good post about how to do this. See
http://stunnware.com/crm2/topic.aspx?id=JS2


--
This posting is provided "AS IS" with no warranties, and confers no rights.


"Mahmood" wrote:


Similar Posts