CA Service Management

  • 1.  SDM 14.1.x - Javascript - require field based on category

    Posted May 14, 2018 10:51 AM

    SDM 14.1.x - multiple customers.

     

    Requirement:  The Requester field should be required if the Request Area (category) contains 'New Hire'.

     

    Work in Progress:  I have a preSaveTrigger which checks for this condition and calls the detailReportValidation function.

     

    Issues: 

     

    1. This works fine on initial save, however, an alert pop-up is shown rather than the red box and UI message.

    2. Opening the ticket in Edit mode and then saving - even with no changes made and the Requester field not empty - results in the same behavior.  The workaround is to enter the Requester field and remove the trailing space shown in the combo name.  You can then Save the ticket.

     

    Code:

    function preSaveTrigger(){
        var factory;
        factory = propFactory;

        if (factory == "cr"){
        element = document.main_form.elements;
       
        /* Requester is required for New Hire tickets */
        if (typeof element['KEY.category'] == "object"){
            if (typeof element['KEY.requested_by'] == "object"){
                var z_category = element['KEY.category'].value;
                var z_requester = element['KEY.requested_by'].value;

                if (z_category.indexOf("New Hire") != -1){
                    var z_requesterid = element['requested_by_combo_name'].id;
                    //var z_requesterid = element['requested_by'].id;
                    var z_requestertemp = document.getElementById(z_requesterid);
                    //var z_requestertemp = document.getElementById('df_0_0');
                    if ( (z_requester === null) || (z_requester === "") ){
                        detailReportValidation(z_requestertemp,true,"Please add the HR analyst submitting this form as the Requester.\nThis is required for New Hire tickets!");
                        return false
                    }
                    else {
                        detailReportValidation(z_requestertemp,false);
                    }
                }
            }
          }      /* End Requester required for New Hire */
         } /* End 'cr' rules */
        return true
    }

     

    This is saved to $NX_ROOT\site\mods\www\wwwroot\scripts.

     

    Troubleshooting so far:  I have tried using the requested_by ID value rather than the combo name and I have also tried to get the element using the hard coded ID.  Neither had any effect.

     

    I am trying to do this client side as opposed to a majic mod as it will not require a services restart and it can be modified by form group.

     

    PS:  This is heavily borrowed from examples on this site, but I can't find the original to give credit back to the developers - so if this is from your work please sing out and I will reference your posts!

     

    TIA,

     

    J.W.



  • 2.  Re: SDM 14.1.x - Javascript - require field based on category

    Posted May 15, 2018 03:41 PM


  • 3.  Re: SDM 14.1.x - Javascript - require field based on category

    Posted May 16, 2018 04:30 PM

    Daniel,

     

    Thanks for the reminder!  I have used your solutions previously.  The issue I have is that I need the Requester field to always remain visible and only the make_required value changed. Your sample for detail_chg has all the optional fields hidden on load and these have the make_required set to "yes".

     

    When I call the zShowAttr and pass the 'requested_by' name to it, then nothing changes.  It was already visible but since it does not have have the make_required set at all, there appears to be nothing for the function to change.  I double checked this by call zHideAttr and it hides correctly.

     

    I have tried to call zSetRequired(zId, 1) directly, passing the hard-coded id of 'df_0_0' as a test and I can see it complete, but no change is made.

     

    I will look it over some more later.  I may be missing something simple.

     

    J.W.



  • 4.  Re: SDM 14.1.x - Javascript - require field based on category
    Best Answer

    Posted May 15, 2018 04:06 PM
      |   view attached

    We configured our instance to do something similar, completely in the form and based on a Javascript function using detailMakeReq() called with the onBlur event on the Category .  This example is for the detail_chg.htmpl but can be easily modified for detail_cr / in / pr.  If however you need the error dialog to display on a completely different form than these you'll need to include a couple of extra lines.

     

    var saveAckMsgExtra = "${args.trigger_message:}";
    var error_msg = '<PDM_FMT ESC_STYLE=JS2>$args.ERROR_MSG</PDM_FMT>';  // These two go immediately after the var form_title variable.

     

    var argHumantouchLog = "$args.KEEP.humantouch_log";  //  This goes in the script section immediately following <PDM_INCLUDE FILE=std_head.htmpl>

     

    var prop_ref = "${prop_ref:}";   // This goes right before the CA .js files are called

     

    ******************************************************************

     

    function set_required_fields_by_category()
    {
    //var a = document.getElementsByName("SET.chgtype")[0]; // 'Type' Dropdown
    var b = document.getElementById("df_0_3").value; // 'Category'
    var c = b.substr(0, 5).toLowerCase();
    var d = b.substr(0, 4).toLowerCase();
    if ( c != "" ) {
       if ( c == "cc ex" ) {
       // If the category = 'CC Express' then make 'Authorized Approver' and 'Schedule Start Date' required
       detailMakeReq("zapprover,sched_start_date");
       detailMakeReq("effort,backout_plan", true);
    } else if ( c == "cc em" ) {
       // If the category == 'CC Emergency' then make 'Authorized Approver' and 'Schedule Start Date' required
       detailMakeReq("zapprover,sched_start_date");
       detailMakeReq("effort,backout_plan", true);
    } else if ( d == "cc r" ) {
       // If the category = 'CC Routine' then make 'Implementation Plan', 'Backout Plan', 'Schedule Start Date' required
       detailMakeReq("effort,backout_plan,sched_start_date");
       detailMakeReq("zapprover", true);
    } else if ( d == "cc o" ) {
       // If the category = 'CC Operational' then make none of these fields required
       detailMakeReq("effort,backout_plan,sched_start_date", true);
       detailMakeReq("zapprover", true);
       // If the category = 'CC Downtime' then make none of these fields required
    } else if ( d == "cc d" ) {
       detailMakeReq("effort,backout_plan,sched_start_date", true);
       detailMakeReq("zapprover", true);
       // Otherwise make none of these fields required
    } else {
       detailMakeReq("effort,backout_plan,sched_start_date", true);
       detailMakeReq("zapprover", true);
    }
    }
    }

     

     

     

    Derek~

    Attachment(s)

    zip
    Solution.zip   11 KB 1 version


  • 5.  Re: SDM 14.1.x - Javascript - require field based on category

    Posted May 16, 2018 06:11 PM

    Derek,

     

    Thanks!  This is much more straightforward than my approach. 

     

    function set_required_fields_by_category()
    {
        var zCat = document.main_form.elements["KEY.category"].value;
        var zRequester = document.main_form.elements["SET.requested_by"].value;

        if (zCat.indexOf("New Hire") != -1 && zRequester == ""){  
        detailMakeReq("requested_by", false);
        }
    }

     

    However, this has the side-effect of disabling the auto-suggest for all fields on the form.  Any ideas?

     

    J.W.



  • 6.  Re: SDM 14.1.x - Javascript - require field based on category

    Posted May 16, 2018 06:56 PM

    I don't think I'm going to be of much help there.  I assume by auto-suggest you're referring to typing in 3+ characters into a field and having it display the first 25 possible matches.  I can say that this functionality still exists for us in the detail_chg.htmpl form we modified with the code provided so I'm not sure why it wouldn't have the same effect on other detail forms.  This is on 14.1.3 for what it's worth.



  • 7.  Re: SDM 14.1.x - Javascript - require field based on category

    Posted May 16, 2018 09:50 PM

    Thanks, Derek.  I'll run it through the default form since I have been using the customers' customized versions.

     

    J.W.