AnsweredAssumed Answered

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

Question asked by J_W on May 14, 2018
Latest reply on May 17, 2018 by J_W

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.

Outcomes