Show/Hide section based on selection of "group of check

Posted by Paulh0763 on 31-Jul-2015 11:10

I have an "Implementations" tab on the Account record that has a field called "Products" (group of checkboxes field). On that tab I have sections that I want to show/hide based on the checkbox selected and obviously if more than one is selected to show those sections as well. Here is my page level script, and it does work for individual checkbox selections but not for multiple. 

<script>
document.getElementById("products").checked = type1;
function type1(){
var type1 = rbf_getPicklistCodes("products")
if(type1 == 'P'){
hideAllSection();
rbf_showOrHideSection(rbf_getSectionIdByTitle("Payroll"), true);
rbf_showOrHideSection(rbf_getSectionIdByTitle("Time and Attendance"), false);
rbf_showOrHideSection(rbf_getSectionIdByTitle("Benefits"), false);
}
if(type1 == 'B'){
hideAllSection();
rbf_showOrHideSection(rbf_getSectionIdByTitle("Payroll"), false);
rbf_showOrHideSection(rbf_getSectionIdByTitle("Time and Attendance"), false);
rbf_showOrHideSection(rbf_getSectionIdByTitle("Benefits"), true);
}
if(type1 == 'TA'){
hideAllSection();
rbf_showOrHideSection(rbf_getSectionIdByTitle("Payroll"), false);
rbf_showOrHideSection(rbf_getSectionIdByTitle("Time and Attendance"), true);
rbf_showOrHideSection(rbf_getSectionIdByTitle("Benefits"), false);
}
}
function hideAllSection()
{
rbf_showOrHideSection(rbf_getSectionIdByTitle("Payroll"), false);
rbf_showOrHideSection(rbf_getSectionIdByTitle("Time and Attendance"), false);
rbf_showOrHideSection(rbf_getSectionIdByTitle("Benefits"), false);
}
</script>

I have tried if(type1 == 'P' && type1 == 'B') but that does not work. 

I have the following on the NEW page properties "Onload" to hide the section initially

rbf_showOrHideSection(rbf_getSectionIdByTitle("Payroll"), false);
rbf_showOrHideSection(rbf_getSectionIdByTitle("Time and Attendance"), false);
rbf_showOrHideSection(rbf_getSectionIdByTitle("Benefits"), false);
type();

I have the type1(); on the EDIT and VIEW page properties "Onload" and I also have it on the EVENTS of the field level OnClick.

Any assistance would be appreciated and of course if there is another approach I would appreciate input on that as well.

Thanks!

Paul

All Replies

Posted by jsniemi79 on 31-Jul-2015 11:48

The rbf_getPicklistCodes will return a comma separated list of values when multiple values are selected.  You would need to parse that value into multiple variables or look for a substring in the variable in order to determine if the value exists.

Another option would be to use indexOf to see if it exists and then exclude it based on the answer.  This would only work if you codes are completely unique since you have some single character values.

var n = type1.indexOf("P");

The variable "n" would return the position of the first occurrence of "P" and would have a value of -1 if the value was not found.

Posted by Paulh0763 on 31-Jul-2015 14:04

Thanks for the input jsniemi79 - can you provide me with a snippet of code using my example? My scripting is not so advanced.

Thanks :)

Posted by jsniemi79 on 31-Jul-2015 14:48

Not a problem.  I put this together and tried it out and it appears to do what I think you want.  Good luck

<script>
function type1(){

var type1 = rbf_getPicklistCodes("products")

var payrollExist = type1.indexOf("P");
var benefitsExist = type1.indexOf("B");
var timeExist = type1.indexOf("TA");

var showPayroll = false;
var showBenefits = false;
var showTime = false;

if(payrollExist != -1){
showPayroll = true;
} else {
showPayroll = false;
}

if(benefitsExist != -1){
showBenefits = true;
} else {
showBenefits = false;
}

if(timeExist != -1){
showTime = true;
} else {
showTime = false;
}

setSectionVisibility(showPayroll, showBenefits, showTime);
}

function setSectionVisibility(a,b,c) {

rbf_showOrHideSection(rbf_getSectionIdByTitle("Payroll"), a);
rbf_showOrHideSection(rbf_getSectionIdByTitle("Benefits"), b);
rbf_showOrHideSection(rbf_getSectionIdByTitle("Time and Attendance"), c);

}

</script>

This thread is closed