locked
How to add a default value to a text field RRS feed

  • Question

  • I feel comfortable with the form handler and how it works in CRM but keep getting errors when I try to simply set the deafult value on the Account Record Address1_Country to "USA". I have tried a variety of ways including passing parameters and just plain code. Here are two examples:

    function prefilledField () {

    var CRM_FORM_TYPE_CREATE = "1";

    if (crmForm.FormType==CRM_FORM_TYPE_CREATE)

     {

      // Set the value of the country field.

      var country = crmForm.all.address1_country;

      country.DataValue = "USA ";

     }

    I have put a parameter in crmForm.all.name.Datavalue and also left the parameter empty

     

    and have aslo tried...

    } 

    function prefilledField (fieldName, value) {

    var iscreateForm = Xrm.Page.ui.getformtype() ==1;

    var field = Xrm.Page.getAttribute(fieldname);

    if(isCreateForm){

         field.setvalue(value);

         field.setSubmitModel("always");

         }

    }

    In the last example I pass two parameters - "address1_Country", "USA"

    What am i doing wrong. I would appreciate anyone providing me with direction including perhaps something I amay be missin in the form handler. I have tried all of the articles here and i know its me since I am just working with jscript for the first time.

    Thursday, February 21, 2013 7:23 PM

Answers

  • try with:

    function AccountOnLoad() {
    var  formType = Xrm.Page.ui.getFormType();
    if (formType ==1) {
    	Xrm.Page.getAttribute("address1_country").setValue("USA");
    }
    }

    Thursday, February 21, 2013 7:39 PM

All replies

  • try with:

    function AccountOnLoad() {
    var  formType = Xrm.Page.ui.getFormType();
    if (formType ==1) {
    	Xrm.Page.getAttribute("address1_country").setValue("USA");
    }
    }

    Thursday, February 21, 2013 7:39 PM
  • Good News it didnt give me an error but the field did not get the populated value? Am I missing something somewhere?
    Thursday, February 21, 2013 9:55 PM
  • It worked! Preview mode didnt but when I published it it worked! Thanks Guido for all you help!!!!
    Thursday, February 21, 2013 10:01 PM
  • If you are interested as to why your functions were failing, the first function I suspect is because the FORM_TYPE variable is string and needs to be int. Also this is the CRM 4 method which is being fazed out. The second function you have posted appears to have approx 6 typos with incorrect casing and wording, which means you will have undefined variables and functions.

    Paul

    Friday, February 22, 2013 5:15 AM
  • Hi,

    You can try with this

    function RetrieveTextonload()
    {
       if (Xrm.Page.ui.getFormType() == 1)
       {
         if(Xrm.Page.getAttribute("address1_country").getValue() == null)
          {
             Xrm.Page.getAttribute("address1_country").setValue("USA");
          }
       }
    }

    Gopinath V.

    Monday, February 25, 2013 7:20 AM