function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Maria22Maria22 

Automatic select checkbox if other checkbox is selected based on certain conditions

Hi Experts,
I have a VF page for creation of an address from account.I mean we have Address as a related list on Account and in that related list we have a Crete New button which when clicked will opens up a VF page where user can fill details and Save records.
Every thing is working fine till now except one condition where I stuck badly.
Requirement
We have 2 checkboxes on VF page called as "Primary" and "Mailing". An account can have more than 1 addresses but at one time only 1 address can have Primary checkbox as checked and only one address has Mailing as checked.Suppose an account has 4 addresses in which Address 1 has Primary checkbox checked,Address 2 and 3 doesn't have either of the two checkbox checked,Addreress 4 has Mailing checkbox selected.
Now I have a requirement when already an address exits with both Flags "Current Residence(API Name-Primary__c) and Mailing(API Name-Mailing__c)" checked and then user is trying to create another address and if select "Current Residence" checkbox then "Mailing" checkbox will be selected automatically.I achieved this through the logic written behind Save button.When user tries to create another address and select Current Residence checkbox and clicks on Save button then "Mailing" checkbox is getting saved automatically upon saving of record. But business doesn't need thi solution after hitting Save button.What they need is to check the "Mailing" checkbox automatically once user is trying to create another address and check the "Current Residence" and that account already has one address where both flags were checked automatically


Below is the part of the code from Controlller which I did intially behind Save button
global Address__c addr{get; set;} 
List<Address__c> addrList = [select id, Mailing__c, Primary__c, (select Type__c, Primary__c from Address_Types__r) from Address__c 
                                     where Account__c=:acc.Id and Primary__c = true
                                     order by LastModifiedDate desc limit 1];

if (!addrList.isEmpty()) {
            primaryAddress = addrList[0];
}
 public PageReference Save()
    {   

if(primaryAddress != null && addr.Primary__c==true && primaryAddress.Mailing__c==true){
             
            addr.Mailing__c=true;
        }
}

But due to business requiremet they dont need behind Save button rather they need onclick of the checkbox itself.

Below is the part from my VF page
 
<apex:page id="thePage" standardController="Account" extensions="VFC_NewAddress" standardStylesheets="true" >
<style>
        body .bPageBlock .pbBody .grey .pbSubheader{
            background-color:#FFDADA;
        }
        body .label{
            padding-left: 2px;
            font-size: 91%;
            font-weight: bold;
        } 
        body .btn{
        }  
    </style>

<apex:pageBlockButtons id="btnid">  
                <apex:commandButton action="{!save}" value="Save" id="doSave" styleClass="btn" onclick="return validate();"/>
                <apex:commandButton action="{!cancel}" value="Cancel" onclick="return handleConsole();"/>
</apex:pageBlockButtons>
 <apex:outputPanel styleClass="grey" layout="block" id="panelId">
              
 <apex:pageBlockSection title="Information" columns="1" id="pageBlockSecId">
<apex:pageBlockSectionItem id="item20" >
                        <apex:outputText value="Current Residence" ></apex:outputText>
                        <apex:inputField value="{!addr.Primary__c}" id="primary"/>
                    </apex:pageBlockSectionItem>
                    
                    <apex:pageBlockSectionItem id="item100">
                        <apex:outputText value="Mailing" ></apex:outputText>
                        <apex:inputField value="{!addr.Mailing__c}" id="mailing"/>
                    </apex:pageBlockSectionItem>
                    
                </apex:pageBlockSection> 
</apex:outputPanel>
            
        </apex:pageBlock>
        
    </apex:form>
</apex:page>

Below is the sample screenshot
User-added imageOnce user selects Current Residence checkbox then Primary checkbox will be automatically selected without hitting Save button.Also this needs to be happen only when the account has such an address where both flag are on same address.Obviously because at any time only one address can have Current Residenec as checked or Primary as checked or may have both checked.

So as a business user the requirement should look like below
GIVEN user in Salesforce updated existing address to be the new Current Residence
WHEN user selects the Current Resident flag on existing address
AND account has an address where both Current Residenece and Mailing are checked
THEN the system automatically ticks the Mailing flag when Current Residence flag is selected on new address
    AND system does NOT allow Mailing to be unchecked
    AND if user tries they see message displayed on screen: 
'One active address must always be marked as Mailing.  Kindly select a different address to be Mailing or if customer has withdrawn consent please update their consent page.’

I didnt paste complete VF page and controller only those seems to be relevant as per my question.So I posted only relevant part from my code.

I hope I have provided detailed infor regarding my issue and somebody will definitely help me

Kindly help me

Many thanks in advance

Thanks & Regards,
Maria
Britto Fernandez 2Britto Fernandez 2
Dear Maria,

Our timezone is GMT+5.30 Hrs. It would take more effort to adopt above code and execute at our end. Would you please skype/email us at madhukar.reddy@heptarc.com and it will be easier to work on code in your org by sharing screen.