Dynamics CRM Support Blog
Problems configuring the CRM Outlook client? Use the wizard!
CRM Resource Center
FAQ About Technical SupportOnline | On-Premise
FAQ About Billing
Implementation Guide
Microsoft Dynamics CRM Developer Center and SDK
Having some trouble with code. I am trying to set the default value to "Service Dates" a contract template type on the on form event for the contract entity. I continue to get an error as follows. Below is the code I have in as well. I admit I borrowed the code from another site for setting the default unit on product and tweaked. I am trying to learn so if you can guide me on where it is wrong and how to fix it I would greatly appreciate it.
Error: There was an error with thisfield's customized event.
Field: window
Event: onload
Error: The value of the property 'SetContractTemplate' is null or undefined, not a Function object.
CODE:
// This sets the default values for contract template upon a new contract// Template = Service Dates function SetContractTemplate()var CRM_FORM_TYPE_CREATE = "1"; var lookupItem = new Array(); var contracttemplate = "Service Dates"; if (crmForm.FormType==CRM_FORM_TYPE_CREATE) { // Set: GUID of contract type "Service Dates", object type code and text name "Service Dates" lookupItem[0] = new LookupControlItem ("{3C427B6E-8E43-E111-B059-0050568C000E}", 2011, "Service Dates"); // Set the form control value to the lookupItem just created. crmForm.all.contracttemplateid.DataValue = lookupItem ; }
Sorry I should have closed this post. I was able to get it to work with the following code:
if (crmForm.FormType ==1)
Jennifer,
Is this for CRM 2011?
Yes CRm 2011 on premise
Can you try converting your code to CRM 2011 code?
This will not solve the issue but it can make it easier to troubleshoot.
Use this tool:
crm2011scriptconvert.codeplex.com
function SetContractTemplate()var CRM_FORM_TYPE_CREATE = "1"; var lookupItem = new Array(); var contracttemplate = "Service Dates"; if (Xrm.Page.ui.getFormType()==CRM_FORM_TYPE_CREATE) { // Set: GUID of contract type "Service Dates", object type code and text name "Service Dates" lookupItem[0] = new LookupControlItem ("{3C427B6E-8E43-E111-B059-0050568C000E}", 2011, "Service Dates"); // Set the form control value to the lookupItem just created. Xrm.Page.getAttribute("contracttemplateid").setValue(lookupItem ); }
Are you still getting the same error Jennifer?
Yes same error message
I have been able to get the following code to work however I want it only to set the default template for a new "Create" form not for a preexisting contract. Any help with the code would be greatly appreciated.
function SetContractTemplate(){var contractTemplate = Xrm.Page.getAttribute("contracttemplateid").getValue();var lookupItem = new Array(); var contracttemplate = "Service Dates"; {if (Xrm.Page.getAttribute("contracttemplateid").getValue == (null || "undefined"));lookupItem[0] = new LookupControlItem ("{3C427B6E-8E43-E111-B059-0050568C000E}", 2011, "Service Dates"); Xrm.Page.getAttribute("contracttemplateid").setValue(lookupItem ); }}
Dear Jennifer,
please try to add the if (isCreateForm) statement.