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
Thomas CailletThomas Caillet 

Button to update child accounts on case

Hello everybody,

I'd like to know if it's possible to create a button on cases that open new window which list all child accounts related to case's account ?
And after to select any accounts, I need to update a field in these accounts.

Thanks for your answers
Thomas
Best Answer chosen by Thomas Caillet
Amit Singh 1Amit Singh 1
Thomas, use below code,
public class AccountSelectClassController{

    public AccountSelectClassController(ApexPages.StandardController controller) {
		caseList = [Select id, AccountId From Case Where Id=:ApexPages.currentPage.getParameters().get('id')];
		System.debug('#### '+caseList);
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account Id=:caseList[0].AccountId
                OR ParentId=:caseList[0].AccountId]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
	
	}
    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Account> selectedAccounts{get;set;}
    public List<Case> caseList {get; set;}
 
    /*public AccountSelectClassController(){
        caseList = [Select id, AccountId From Case Where Id=:ApexPages.currentPage.getParameters().get('id')];
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account Id=:caseList[0].AccountId
                OR ParentId=:caseList[0].AccountId]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
    }*/
 
public void processSelected() {
    selectedAccounts = new List<Account>();
 
        for(wrapAccount wrapAccountObj : wrapAccountList) {
            if(wrapAccountObj.selected == true) {
                selectedAccounts.add(wrapAccountObj.acc);
            }
        }
        if(selectedAccounts!=null && selectedAccounts.size()>0){
            for(Account a : selectedAccounts){
                // your logic for updating the Account
                a.Termination_Case__c = caseList[0].id;
            }
        }
        if(selectedAccounts!=null && selectedAccounts.size()>0)update selectedAccounts;
    }
 
    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapAccount {
        public Account acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapAccount(Account a) {
            acc = a;
            selected = false;
        }
    }
}

Thanks,
Amit

All Answers

Amit Singh 1Amit Singh 1

Hi Thomas,

Yes, it is possible you can create a custom button which will open a VF page. For this, you need to use the concept of the wrapper class.

For detailed information visit the given link.

 http://www.sfdcpoint.com/salesforce/wrapper-class-in-apex/

Hope this helps for you :)

Thanks,
Amit Singh.

Thomas CailletThomas Caillet
Thanks Amit, I will try this and let you know

Thomas
Thomas CailletThomas Caillet
Hi Amit,

I try the code you send to me and I need you help for two things :

User-added image
- I build a button on case layout and I just need to see when I click on this button the account related to this case and his childs accounts (based on the hierarchy)
- Second thing, when I click on the button on screenshot, I want to update a case lookup field on all accounts selected pages.

Thanks
Thomas 
Amit Singh 1Amit Singh 1
Hi Thomas,

If you do not mind will you please be more specific on your requirement. What you want yo achieve?

1 - You want to show the list of All account and it's Chile accounts related to the Case?
2 - Please elaborate more this.
 
Thanks,
AMit.
Thomas CailletThomas Caillet
Thanks Amit,

1- Yes exactly, I need to have a button on case layout which list the account related to this case and all his childs accounts.
2- Yes sure : when I have this list, I need to select accounts that I want and then when I click on the button (on visualforce page), I need to update a lookup field related to all accounts selected.

Tell me if it's not clear.

Thomas
Amit Singh 1Amit Singh 1
Hi Thomas,

Do you have any lookup field in Account OBJECT which you need to update with Case ID?

Also, please share your apex class so that I could help and modify the class.

Thanks :)
Amit Singh.
Thomas CailletThomas Caillet
Yes I have a lookup field in account object that I need to update with Case ID names Termination_Case__c

Sure this my visualforce page : 

<apex:page Controller="AccountSelectClassController" sidebar="false">
    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Update Contract Termination Case" action="{!processSelected}" rerender="table2"/>
            </apex:pageBlockButtons>
 
            <apex:pageblockSection title="All Accounts" collapsible="false" columns="2">
 
                <apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" id="table" title="All Accounts">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column value="{!accWrap.acc.Name}" />
                    <apex:column value="{!accWrap.acc.BillingState}" />
                    <apex:column value="{!accWrap.acc.Phone}" />
                </apex:pageBlockTable>
 
                <apex:pageBlockTable value="{!selectedAccounts}" var="c" id="table2" title="Selected Accounts">
                    <apex:column value="{!c.Name}" headerValue="Account Name"/>
                    <apex:column value="{!c.BillingState}" headerValue="Billing State"/>
                    <apex:column value="{!c.Phone}" headerValue="Phone"/>
                </apex:pageBlockTable>
 
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
 
</apex:page>

and the class :

public class AccountSelectClassController{

    public AccountSelectClassController(ApexPages.StandardController controller) {

    }

 
    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Account> selectedAccounts{get;set;}
 
    public AccountSelectClassController(){
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account limit 10]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
    }
 
    public void processSelected() {
    selectedAccounts = new List<Account>();
 
        for(wrapAccount wrapAccountObj : wrapAccountList) {
            if(wrapAccountObj.selected == true) {
                selectedAccounts.add(wrapAccountObj.acc);
            }
        }
    }
 
    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapAccount {
        public Account acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapAccount(Account a) {
            acc = a;
            selected = false;
        }
    }
}
Amit Singh 1Amit Singh 1
Hi Thomas,

Have you tried your code for your logic?

Also, I have made some tweaks on VF page and Apex Class use the below code and use code snippet for pasting the code for increasing the readability of the code. 

VF Page code:
<apex:page Controller="AccountSelectClassController" sidebar="false">
    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("inputId");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Update Contract Termination Case" action="{!processSelected}" rerender="table2"/>
            </apex:pageBlockButtons>
 
            <apex:pageblockSection title="All Accounts" collapsible="false" columns="2">
 
                <apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" id="table" title="All Accounts">
                    <apex:column >
                        <!-- <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet> -->
                        <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column value="{!accWrap.acc.Name}" />
                    <apex:column value="{!accWrap.acc.BillingState}" />
                    <apex:column value="{!accWrap.acc.Phone}" />
                </apex:pageBlockTable>
 
                <!-- <apex:pageBlockTable value="{!selectedAccounts}" var="c" id="table2" title="Selected Accounts">
                    <apex:column value="{!c.Name}" headerValue="Account Name"/>
                    <apex:column value="{!c.BillingState}" headerValue="Billing State"/>
                    <apex:column value="{!c.Phone}" headerValue="Phone"/>
                </apex:pageBlockTable> -->
 
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
 
</apex:page>

Apex Class:
public class AccountSelectClassController{

    public AccountSelectClassController(ApexPages.StandardController controller) {}
    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Account> selectedAccounts{get;set;}
    public List<Case> caseList {get; set;}
 
    public AccountSelectClassController(){
        caseList = [Select id, AccountId From Case Where Id=:ApexPages.currentPage.getParameters().get('id')];
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account Id=:caseList[0].AccountId
                OR ParentId=:caseList[0].AccountId]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
    }
 
public void processSelected() {
    selectedAccounts = new List<Account>();
 
        for(wrapAccount wrapAccountObj : wrapAccountList) {
            if(wrapAccountObj.selected == true) {
                selectedAccounts.add(wrapAccountObj.acc);
            }
        }
        if(selectedAccounts!=null && selectedAccounts.size()>0){
            for(Account a : selectedAccounts){
                // your logic for updating the Account
                a.Termination_Case__c = caseList[0].id;
            }
        }
        if(selectedAccounts!=null && selectedAccounts.size()>0)update selectedAccounts;
    }
 
    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapAccount {
        public Account acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapAccount(Account a) {
            acc = a;
            selected = false;
        }
    }
}
 Use the above code and let me know the outcomes :)

Thanks,
Amit
Thomas CailletThomas Caillet
Thanks Amit, I have this error : Error: Compile Error: Method does not exist or incorrect signature: ApexPages.currentPage.getParameters() at line 10 column 62
And at line 13 of your class you forgot a WHERE attribute :)

Thomas
Amit Singh 1Amit Singh 1
Let me know if this is working and make answer as best answer :)
e
Thanks,
Amit
Thomas CailletThomas Caillet
That doesn't work unfortunatly could you help me with my error ?
Thanks 
Thomas
Amit Singh 1Amit Singh 1
Thomas,

Use below code 
ApexPages.currentPage().getParameters().get('id')

This must do the trick :)
Thanks
Thomas CailletThomas Caillet
Thanks I can save the code but could you tell me why I can't use it when I try to create case button ??
Amit Singh 1Amit Singh 1
Thomas, You need to create a custom button which will be responsible for opening your VF page. Your button will execute an Onclick JavaScript. 
let me know if this makes sense OR elaborate more on your point.

Thanks.
Thomas CailletThomas Caillet
Amit, I create the button but I don't have my list of the accounts :

User-added image
Amit Singh 1Amit Singh 1
Thomas Can you share me your button code. Also, make sure case is related to account.

Thanks,
Thomas CailletThomas Caillet
Oh maybe not because I just call my visualforce page : window.location = 'apex/TEST';
"TEST" is the name of my visualforce page.

Sorry Amit I'm novice on apex :)

Thomas
Amit Singh 1Amit Singh 1
Thomas, create a Detail Page button. Follow the below steps for creating the custom Detail page button.

1 - Setup -> Case -> Button, Links and Action -> Create Button or Link.
2 - Give Name and Label of the button. 
3 - Select Detail page button for "Display Type".
4 -  You can adjust height and width of VF page as per your requirement.
window.open('/apex/TEST?ids='+{!Case.Id},'_blank','height=336,width=1150,left=100, top=260,location=no,resizable=yes,toolbar=no,status=no,menubar=no,scrollbars=1', 1);
5 - Got to case layout and add the button you created on the detail page. 

User-added image
 
Cheers :)
 
Thanks,
Amit
 
Thomas CailletThomas Caillet
I have this pop up when I click on my button :(

User-added image
Amit Singh 1Amit Singh 1
Thomas, Edit your button as per the given image.

Also, modify the VF page as per given.
<apex:page standardController="Case"  extensions="AccountSelectClassController" sidebar="false">

User-added image

Hope this will help :)
Thanks,
Amit.
Thomas CailletThomas Caillet
Thanks the page is open now but I don't have any result :/

User-added image

Thanks Amit
Amit Singh 1Amit Singh 1
Thomas, make sure if Any Account is related to case or Not.
Thomas CailletThomas Caillet
Yes I'm sure, and the account related to the case is a parent account with childs accounts.

Thomas 
Amit Singh 1Amit Singh 1
try with below code and check debug log if there is caseList or Not.
public class AccountSelectClassController{

    public AccountSelectClassController(ApexPages.StandardController controller) {
		caseList = [Select id, AccountId From Case Where Id=:ApexPages.currentPage.getParameters().get('id')];
		System.debug('#### '+caseList)
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account Id=:caseList[0].AccountId
                OR ParentId=:caseList[0].AccountId]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
	
	}
    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Account> selectedAccounts{get;set;}
    public List<Case> caseList {get; set;}
 
    /*public AccountSelectClassController(){
        caseList = [Select id, AccountId From Case Where Id=:ApexPages.currentPage.getParameters().get('id')];
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account Id=:caseList[0].AccountId
                OR ParentId=:caseList[0].AccountId]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
    }*/
 
public void processSelected() {
    selectedAccounts = new List<Account>();
 
        for(wrapAccount wrapAccountObj : wrapAccountList) {
            if(wrapAccountObj.selected == true) {
                selectedAccounts.add(wrapAccountObj.acc);
            }
        }
        if(selectedAccounts!=null && selectedAccounts.size()>0){
            for(Account a : selectedAccounts){
                // your logic for updating the Account
                a.Termination_Case__c = caseList[0].id;
            }
        }
        if(selectedAccounts!=null && selectedAccounts.size()>0)update selectedAccounts;
    }
 
    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapAccount {
        public Account acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapAccount(Account a) {
            acc = a;
            selected = false;
        }
    }
}

Thanks :)
Amit Singh 1Amit Singh 1
try with below code and check debug log if there is caseList or Not.
public class AccountSelectClassController{

    public AccountSelectClassController(ApexPages.StandardController controller) {
		caseList = [Select id, AccountId From Case Where Id=:ApexPages.currentPage.getParameters().get('id')];
		System.debug('#### '+caseList)
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account Id=:caseList[0].AccountId
                OR ParentId=:caseList[0].AccountId]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
	
	}
    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Account> selectedAccounts{get;set;}
    public List<Case> caseList {get; set;}
 
    /*public AccountSelectClassController(){
        caseList = [Select id, AccountId From Case Where Id=:ApexPages.currentPage.getParameters().get('id')];
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account Id=:caseList[0].AccountId
                OR ParentId=:caseList[0].AccountId]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
    }*/
 
public void processSelected() {
    selectedAccounts = new List<Account>();
 
        for(wrapAccount wrapAccountObj : wrapAccountList) {
            if(wrapAccountObj.selected == true) {
                selectedAccounts.add(wrapAccountObj.acc);
            }
        }
        if(selectedAccounts!=null && selectedAccounts.size()>0){
            for(Account a : selectedAccounts){
                // your logic for updating the Account
                a.Termination_Case__c = caseList[0].id;
            }
        }
        if(selectedAccounts!=null && selectedAccounts.size()>0)update selectedAccounts;
    }
 
    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapAccount {
        public Account acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapAccount(Account a) {
            acc = a;
            selected = false;
        }
    }
}

Thanks :)
Amit Singh 1Amit Singh 1
This worked ?
Thomas CailletThomas Caillet

Hi Amit,

Sorry for yesterday, I left my desk :)
I tried your code but I have an error line 6 : Error: Compile Error: unexpected token: 'if' at line 6 column 8

Thomas

Amit Singh 1Amit Singh 1
Thomas, use below code,
public class AccountSelectClassController{

    public AccountSelectClassController(ApexPages.StandardController controller) {
		caseList = [Select id, AccountId From Case Where Id=:ApexPages.currentPage.getParameters().get('id')];
		System.debug('#### '+caseList);
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account Id=:caseList[0].AccountId
                OR ParentId=:caseList[0].AccountId]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
	
	}
    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Account> selectedAccounts{get;set;}
    public List<Case> caseList {get; set;}
 
    /*public AccountSelectClassController(){
        caseList = [Select id, AccountId From Case Where Id=:ApexPages.currentPage.getParameters().get('id')];
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account Id=:caseList[0].AccountId
                OR ParentId=:caseList[0].AccountId]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
    }*/
 
public void processSelected() {
    selectedAccounts = new List<Account>();
 
        for(wrapAccount wrapAccountObj : wrapAccountList) {
            if(wrapAccountObj.selected == true) {
                selectedAccounts.add(wrapAccountObj.acc);
            }
        }
        if(selectedAccounts!=null && selectedAccounts.size()>0){
            for(Account a : selectedAccounts){
                // your logic for updating the Account
                a.Termination_Case__c = caseList[0].id;
            }
        }
        if(selectedAccounts!=null && selectedAccounts.size()>0)update selectedAccounts;
    }
 
    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapAccount {
        public Account acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapAccount(Account a) {
            acc = a;
            selected = false;
        }
    }
}

Thanks,
Amit
This was selected as the best answer
Thomas CailletThomas Caillet
Same like yesterday line 8 : I don't need to add WHERE statement after "from account" ??
Amit Singh 1Amit Singh 1
Thomas Sorry, add where statement :)

Thanks 
Thomas CailletThomas Caillet
That works Amit. Thanks for your time I really appreciate.
I let you know if i need something else

Thomas