locked
CRM 4 Clear field Data Values OnSave RRS feed

  • Question

  • I've written a script to clear the data values on some fields OnSave depending on the value of a picklist on the form. It seems like it should be working when I step through it and debug, I see the value change to Null - but then it doesn't save. Can anyone help me see what I'm doing wrong?

      // get equipment type
      var type = crmForm.all.new_equipmenttype.SelectedText;
    
      // define Save set based on selected equipment type
      switch (type) {
        case "Generator":
          var saveSet = new Array(
            "capacity",
            "capacityunit",
            "systemage",
            "fuelstoragevalues",
            "fuelstoragecapacity");
          break;
        case "UPS":
          var saveSet = new Array(
            "capacity",
            "capacityunit",
            "batmonsystem",
            "cellsperstring",
            "numofstrings");
          break;
        case "ATS": case "Chiller": case "AHU": case "VAV": case "CRAC":
          var saveSet = new Array(
            "capacity",
            "capacityunit");
          break;
        case "Special Fire Suppression":
          var saveSet = new Array(
            "areaprotected",
            "suppressiontype",
            "vaporagent",
            "vaporagentwritein",
            "supsysmanufacturer");
          break;
        case "Switchgear": case "Critical Power Distribution":
          var saveSet = new Array(
            "capacity",
            "capacityunit",
            "circuits");
          break;
      }
      // define array of fields to step through
      var fields = new Array(
        { object: crmForm.all.new_capacity, label: "capacity" },
        { object: crmForm.all.new_capacityunit, label: "capacityunit" },
        { object: crmForm.all.new_systemage, label: "systemage" },
        { object: crmForm.all.new_fuelstoragevalues, label: "fuelstoragevalues" },
        { object: crmForm.all.new_fuelstoragecapacity, label: "fuelstoragecapacity" },
        { object: crmForm.all.new_batterymonitoringsystem, label: "batmonsystem" },
        { object: crmForm.all.new_cellsperstring, label: "cellsperstring" },
        { object: crmForm.all.new_numberofstrings, label: "numofstrings" },
        { object: crmForm.all.new_areaprotected, label: "areaprotected" },
        { object: crmForm.all.new_suppressiontype, label: "suppressiontype" },
        { object: crmForm.all.new_vaporagent, label: "vaporagent" },
        { object: crmForm.all.new_vaporagentwritein, label: "vaporagentwritein" },
        { object: crmForm.all.new_suppressionsystemmanufacturer, label: "supsysmanufacturer" },
        { object: crmForm.all.new_circuits, label: "circuits" });
    
      // step through all fields
      for (x in fields) {
        var thislabel = fields[x].label;
        // if the current field is not in the Save Set, clear it
        if (thislabel in oc(saveSet)) {
          // do nothing, data should save properly
        } else {
          var thisfield = fields[x].object;
          var test = thisfield.DataValue;
          thisfield.DataValue = null;
          test = thisfield.DataValue;
          thisfield.ForceSubmit = true;
        }
      }

    It calls the following function from the OnLoad code:

        oc = function (a) {
          var o = {};
          for (var i = 0; i < a.length; i++) {
            o[a[i]] = '';
          }
          return o;
        }
    • Edited by CamilleJ Thursday, February 10, 2011 3:19 PM code adjustment
    Thursday, February 10, 2011 3:12 PM

Answers

  • Sorry to bother you again. Instead of JavaScript is it possible to have a workflow which will trigger OnCreate and OnChange of new_equipmenttype and based on the selection in new_equipmenttype it clears the fields.
    Regards Faisal
    • Marked as answer by CamilleJ Friday, February 11, 2011 2:44 PM
    Friday, February 11, 2011 2:31 PM
  • I didn't read word by word what the problem is, but my sense for the issue that you have is, when the form's onsave event is called, CRM has finished constructing XML string that it needs to post back to the server, which means any changes that you made in onsave event may not be taken into account. 

    CRM onsave event is basically provided to rescue you from any invalid input. 


    Daniel Cai | http://danielcai.blogspot.com
    • Marked as answer by CamilleJ Friday, February 11, 2011 2:44 PM
    Friday, February 11, 2011 2:39 PM
  • that's exactly what I was afraid of, and the conclusion I'm pretty much at. So, I have my choices:

    A. use workflow instead of jscript to clear the fields after save - which will allow entries to be retained between different types during user configuration for usability

    OR

    B. implement the code I have in the OnChange event of the Equipment Type picklist (if users change type by mistake their entries are lost)

    Thanks to all for the input - I really appreciate it!


    Camille
    http://mscrm-workarounds.blogspot.com
    • Marked as answer by CamilleJ Friday, February 11, 2011 2:44 PM
    Friday, February 11, 2011 2:43 PM

All replies

  • Put this code on change of picklist and remov it from OnSave. As you are doing ForceSubmit therefore it will be saved.
    Regards Faisal
    Thursday, February 10, 2011 3:23 PM
  • Faisal, thanks for the quick response.

    I've considered that - but I'd rather not. I already have code running there that hides/shows all appropriate fields based on the equipment type.

    The whole idea is that if a user starts entering data for one equipment type, and then decides to change the equipment type, I like the idea of leaving their entries in case they switched by mistake so they don't have to enter it all again upon returning to the original equipment type. This is why I didn't want it in the OnChange.

    I figured I could leave their entries until they save, at which time I could clear all entries that don't apply to the current equipment type. Will this not work?


    Camille
    http://mscrm-workarounds.blogspot.com
    Thursday, February 10, 2011 3:27 PM
  • Fine. Try adding simple code and see whether it works:-

    if (crmForm.all.new_equipmenttype.SelectedText == "Generator")
    {
    crmForm.all.<fieldname>.DataValue = null ;
    crmForm.all.<fieldname>.DataValue = null ;
    }
    else if (crmForm.all.new_equipmenttype.SelectedText == "UPS")
    {
    crmForm.all.<fieldname>.DataValue = null ;
    crmForm.all.<fieldname>.ForceSubmit = true ;
    }


    Regards Faisal
    Thursday, February 10, 2011 3:40 PM
  • The test with the following code, and that specific test:

      if (crmForm.all.new_equipmenttype.SelectedText == "Generator")
    {
    crmForm.all.new_cellsperstring.DataValue = null ;
    crmForm.all.new_cellsperstring.ForceSubmit = true ;
    }
    It did NOT clear the value. Cells Per String is an int field, and is hidden on the form at the time this code runs.
    Camille
    http://mscrm-workarounds.blogspot.com
    Thursday, February 10, 2011 3:51 PM
  • Try this:- 
    if (crmForm.all.new_equipmenttype.SelectedText == "Generator")
    {
    crmForm.all.new_cellsperstring_d.style.display = inline ;
    crmForm.all.new_cellsperstring.DataValue = null ;
    crmForm.all.new_cellsperstring.ForceSubmit = true ;
    crmForm.all.new_cellsperstring_d.style.display = none ;
    }

    Also try:-

    if (crmForm.all.new_equipmenttype.SelectedText == "Generator")
    {
    crmForm.all.new_cellsperstring_d.style.display = inline ;
    crmForm.all.new_cellsperstring.DataValue = "" ;
    crmForm.all.new_cellsperstring.ForceSubmit = true ;
    crmForm.all.new_cellsperstring_d.style.display = none ;
    }

    Regards Faisal
    Thursday, February 10, 2011 3:59 PM
  • Thanks Faisal, but neither of those work either.
    Camille
    http://mscrm-workarounds.blogspot.com
    Thursday, February 10, 2011 6:02 PM
  • I didn't read the whole story, but there is a typo in Faisal's code. Both inline and none should be quoted as something like the following: 

    crmForm.all.new_cellsperstring_d.style.display = 'none ';
    crmForm.all.new_cellsperstring_d.style.display = 'inline';
    

     


    Daniel Cai | http://danielcai.blogspot.com
    Thursday, February 10, 2011 7:21 PM
  • You can use Disabled = false to enable the disabled fields before you forcesubmit in Onsave.

     

    crmForm.all.new_textfield.disabled =
     true
    ;
    OR
    crmForm.all.new_textfield.disabled = false;

    Hope this helps. Amar

    CRM Forum Guidance on how to Help Us Help You

    Thursday, February 10, 2011 10:20 PM
  • Thank you Daniel and Amarsen. Unfortunately, none of these things are working...

    I can see the field flash for a moment and everything, but the field still contains the value after save. It's like the... OH MY! I think I've just had an epiphany. I'll try it and report back...


    Camille
    http://mscrm-workarounds.blogspot.com
    Friday, February 11, 2011 1:49 PM
  • Scratch that - I was thinking I needed to flag it as isDirty or something for it to actually save it... but then remembered that isDirty is read only, and that's what the ForceSubmit is for...

    Oh well. Anyone else have any ideas?


    Camille
    http://mscrm-workarounds.blogspot.com
    Friday, February 11, 2011 1:54 PM
  • Sorry to bother you again. Instead of JavaScript is it possible to have a workflow which will trigger OnCreate and OnChange of new_equipmenttype and based on the selection in new_equipmenttype it clears the fields.
    Regards Faisal
    • Marked as answer by CamilleJ Friday, February 11, 2011 2:44 PM
    Friday, February 11, 2011 2:31 PM
  • That's not a bad idea, and I bet it would work.

    Workflow maintenance if fields or equipment types are changed/added might be a nightmare though. Would likely need to create a child workflow for each equipment type to keep things clean and easily updateable.

    Since I already have the code written I think I might deploy inside OnChange in the interest of getting it done before going through that in-depth workflow design (if I can't get it working in OnSave that is...). But thanks for the suggestion - I honestly didn't even think of that!


    Camille
    http://mscrm-workarounds.blogspot.com
    Friday, February 11, 2011 2:37 PM
  • I didn't read word by word what the problem is, but my sense for the issue that you have is, when the form's onsave event is called, CRM has finished constructing XML string that it needs to post back to the server, which means any changes that you made in onsave event may not be taken into account. 

    CRM onsave event is basically provided to rescue you from any invalid input. 


    Daniel Cai | http://danielcai.blogspot.com
    • Marked as answer by CamilleJ Friday, February 11, 2011 2:44 PM
    Friday, February 11, 2011 2:39 PM
  • that's exactly what I was afraid of, and the conclusion I'm pretty much at. So, I have my choices:

    A. use workflow instead of jscript to clear the fields after save - which will allow entries to be retained between different types during user configuration for usability

    OR

    B. implement the code I have in the OnChange event of the Equipment Type picklist (if users change type by mistake their entries are lost)

    Thanks to all for the input - I really appreciate it!


    Camille
    http://mscrm-workarounds.blogspot.com
    • Marked as answer by CamilleJ Friday, February 11, 2011 2:44 PM
    Friday, February 11, 2011 2:43 PM