locked
Required Field conditional on Boolean Field on Form RRS feed

  • Question

  • Hi all

    I have a boolean field called Leaver? (Yes/No) and another field Date Client Left which becomes visible via a bit of Java when the Yes is selected for Leaver.  Is there some javascript that ensures the record cannot be saved IF the Leaver field is set to Yes and the Date Client Left is empty (ie. make the Date Client Left field required if Leaver is Yes), but the form can be saved if No is selected for the Leaver field?

    Thanks

    Jimbo

    Jim

    Tuesday, June 15, 2010 4:23 PM

Answers

  • Hi, You  can put  the following code  in Onsave() event(from  sdk).

    You can modify it according to your requirment, e.g. to check whether the bit field is true

    if(crmForm.all.SOME_BOOLEAN_FIELD_ID.DataValue)

    {

    // Boolean  field  is true.

    }

     

    Example

    The following code shows how to validate the job title field when the Save button is clicked and to provide a default job title when the Save and Close button is clicked.

    var CRM_FORM_SAVE_MODE_SAVE = 1;
    var CRM_FORM_SAVE_MODE_SAVEANDCLOSE = 2;
    
    // Validate only if the user clicked "Save".
    switch (event.Mode)
    {
     case CRM_FORM_SAVE_MODE_SAVE:
     
      // If the user provided a first and last name, they must provide
      // a job title also.
      if (crmForm.all.jobtitle.DataValue == null &&
       crmForm.all.firstname.DataValue != null &&
       crmForm.all.lastname.DataValue != null &&)
      {
       // Tell the user what is wrong.
       alert("Please provide a Job Title for this person.");
    
       // Give the control focus.
       crmForm.all.jobtitle.SetFocus();
    
       // Cancel the save operation.
       event.returnValue = false;
       return false;
      }
     
      break;
     
     case CRM_FORM_SAVE_MODE_SAVEANDCLOSE:
      
      // If the user forgot to provide a job title, set a default title.
      if (crmForm.all.jobtitle.DataValue == null)
      {
       // Set a default Job Title.
       crmForm.all.jobtitle.DataValue = "N/A";
       
       // Because this is a "Save and Close",
       // just save the form.
       return true;
      }  
     
      break;
    }

    Muhammad Ali Khan
    http://malikhan.wordpress.com
    • Proposed as answer by mardukes Tuesday, June 15, 2010 4:30 PM
    • Marked as answer by Jim Glass Jr Tuesday, June 15, 2010 7:33 PM
    Tuesday, June 15, 2010 4:28 PM