+ Start a Discussion
Vincent van Drunen LittelVincent van Drunen Littel 

Creating a add button that calls a VF page with a related list where I could add account names to a related list on Accounts

Is a bit hard to word what I wanted in the titel... 
Long story short, in my org.  when adding new accounts I need to add competition of the account. These competitors can already be existing accounts within my org. 
To facilitate and speed up process I need to create a button that summons a Pop-up window with a list of all existing accounts where I would be able to check all accounts appropriate, then be able by using a "add" button that will pull all the checked info to my newly created account under competition. 

If you need more info please let me know...

I have tried many different ways and have not been able to get at the right place. 

If anyone would be able to help me on this one I would be very very very happy as this is a major problem.
Best Answer chosen by Vincent van Drunen Littel
Krishna SambarajuKrishna Sambaraju
Here is the sample page and controller. You can change the object names to fit your requirement.

Visualforca page:

<apex:page controller="SelectCompetitorsController" sidebar="false" showHeader="false">
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" />
<script type="text/javascript">
    $ = jQuery.noConflict();
    function SelectAccounts(chk, accountId)
        {
            var accountIds = $('input[id$="accountIds"]');
            var selectedAccounts = accountIds.val();
            if (chk.checked == true)
            {
                if (selectedAccounts == '')
                {
                    selectedAccounts = accountId;
                }
                else
                {
                    selectedAccounts = selectedAccounts + ',' + accountId;
                }
            }
            else 
            {
                var accountIndex = selectedAccounts.indexOf(accountId);
                var accountToRemove = '';

                if (selectedAccounts.length == accountId.length) {
                    accountToRemove = accountId;
                }
                else {
                    if (accountIndex == 0){
                        accountToRemove = accountId+ ',';
                    }
                    else{
                        accountToRemove = ',' + accountId;
                    }
                }
            
                selectedAccounts = selectedAccounts.replace(accountToRemove, '');
            }
            
        accountIds.val(selectedAccounts);
       // alert (accountIds.val());
    }
</script>
    <apex:form >
        <apex:inputHidden value="{!selectedAccountIds}" id="accountIds"/>
        <apex:pageBlock title="Select Competitors">
            <apex:pageBlockTable value="{!accounts}" var="acc">
                <apex:column headerValue="Select" width="100px;">
                    <apex:inputCheckbox onclick="javascript:SelectAccounts(this, '{!acc.Id}');"/>
                </apex:column>
                <apex:column headerValue="Account Name" value="{!acc.Name}"/>
            </apex:pageBlockTable>
            <apex:commandButton value="Add Competitors" action="{!addCompetitors}"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class SelectCompetitorsController {

    public List<Account> accounts {get; set;}
    Map<Id, Account> accountMap {get; set;}
    public string selectedAccountIds {get; set;}
    string parentAccountId;
    public SelectCompetitorsController()
    {
        parentAccountId = ApexPages.currentPage().getParameters().get('accId');
        accountMap = new Map<Id, Account>([select Id, Name from Account]);
        accounts = accountmap.Values();
    }
    
    public PageReference addCompetitors() {
        List<string> accountIds = selectedAccountIds.split(',');
        List<Competitor__c> competitors = new List<Competitor__c>();
        
        for (string s : accountIds)
        {
            Competitor__c competitor = new Competitor__c(Account__c = parentAccountId, Name = accountMap.get(s).Name);
            competitors.add(competitor);
        }
        if (competitors.size() > 0)
        {
            insert competitors;
        }
        string strPageRef = '"javascript:window.close();"';
        return new PageReference(strPageRef);
    }
}

You need add a list button on your competitor object and in the Behaviour section select "Execute JavaScript" and use the following code in text area below.

window.mypopup = window.open('/apex/SelectCompetitors?accId={!Account.Id}','mypopup','top=150,left=200,location=1,status=1,scrollbars=1, width=500,height=600'); 
window.onfocus = function() { 
if (window.mypopup.closed) { 
window.location.reload(); 

};

Hope the above solution works for you.

All Answers

sandeep sankhlasandeep sankhla
Hi,

You can simply create a VF page where you can show all account with chcekbox using wrapper class so you can select them..
then you can have a button which will open this VF page as popup and when you select all the accounts then it will save those accounts to current account related list...

Let me knwoif you are facing any issue in implementing this..

Thanks,
Sandeep
Vincent van Drunen LittelVincent van Drunen Littel

@sandeep sankhla, 

I have been trying to create a VF page using examples found here but i'm new to developing and haven't been successfull so far. 

For example I found this article : https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=906F0000000BKaUIAW

I also found https://developer.salesforce.com/page/Wrapper_Class
Which will pull up the accounts.  I did try to fuse/ create the VF page i want but didn't succeed. 
 

next to that I need to have those selected from this VF page be added to a related list such as this one. 
(example, have a button "add Competition" which opens the VF pages, within this VF page you select by check box then press "add" and thsoe selected are added to the pic below)
User-added image
This is a related list within Accounts object.

I hope i make sense and you understand what i need, i'm still new :/ 

Thank you very much for helping me on this!!!
Vincent

Krishna SambarajuKrishna Sambaraju
Here is the sample page and controller. You can change the object names to fit your requirement.

Visualforca page:

<apex:page controller="SelectCompetitorsController" sidebar="false" showHeader="false">
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" />
<script type="text/javascript">
    $ = jQuery.noConflict();
    function SelectAccounts(chk, accountId)
        {
            var accountIds = $('input[id$="accountIds"]');
            var selectedAccounts = accountIds.val();
            if (chk.checked == true)
            {
                if (selectedAccounts == '')
                {
                    selectedAccounts = accountId;
                }
                else
                {
                    selectedAccounts = selectedAccounts + ',' + accountId;
                }
            }
            else 
            {
                var accountIndex = selectedAccounts.indexOf(accountId);
                var accountToRemove = '';

                if (selectedAccounts.length == accountId.length) {
                    accountToRemove = accountId;
                }
                else {
                    if (accountIndex == 0){
                        accountToRemove = accountId+ ',';
                    }
                    else{
                        accountToRemove = ',' + accountId;
                    }
                }
            
                selectedAccounts = selectedAccounts.replace(accountToRemove, '');
            }
            
        accountIds.val(selectedAccounts);
       // alert (accountIds.val());
    }
</script>
    <apex:form >
        <apex:inputHidden value="{!selectedAccountIds}" id="accountIds"/>
        <apex:pageBlock title="Select Competitors">
            <apex:pageBlockTable value="{!accounts}" var="acc">
                <apex:column headerValue="Select" width="100px;">
                    <apex:inputCheckbox onclick="javascript:SelectAccounts(this, '{!acc.Id}');"/>
                </apex:column>
                <apex:column headerValue="Account Name" value="{!acc.Name}"/>
            </apex:pageBlockTable>
            <apex:commandButton value="Add Competitors" action="{!addCompetitors}"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class SelectCompetitorsController {

    public List<Account> accounts {get; set;}
    Map<Id, Account> accountMap {get; set;}
    public string selectedAccountIds {get; set;}
    string parentAccountId;
    public SelectCompetitorsController()
    {
        parentAccountId = ApexPages.currentPage().getParameters().get('accId');
        accountMap = new Map<Id, Account>([select Id, Name from Account]);
        accounts = accountmap.Values();
    }
    
    public PageReference addCompetitors() {
        List<string> accountIds = selectedAccountIds.split(',');
        List<Competitor__c> competitors = new List<Competitor__c>();
        
        for (string s : accountIds)
        {
            Competitor__c competitor = new Competitor__c(Account__c = parentAccountId, Name = accountMap.get(s).Name);
            competitors.add(competitor);
        }
        if (competitors.size() > 0)
        {
            insert competitors;
        }
        string strPageRef = '"javascript:window.close();"';
        return new PageReference(strPageRef);
    }
}

You need add a list button on your competitor object and in the Behaviour section select "Execute JavaScript" and use the following code in text area below.

window.mypopup = window.open('/apex/SelectCompetitors?accId={!Account.Id}','mypopup','top=150,left=200,location=1,status=1,scrollbars=1, width=500,height=600'); 
window.onfocus = function() { 
if (window.mypopup.closed) { 
window.location.reload(); 

};

Hope the above solution works for you.
This was selected as the best answer
Vincent van Drunen LittelVincent van Drunen Littel

I would like to thank Krishna for all the work and effort he put in to help me with this big problem.
Amazing dedication to a problem that has nothing to do with him! 

You dont know how thankful I am for the help... this man deserves a reward!
Krishna SambarajuKrishna Sambaraju
Hi Vincent,

I recommend you to go through the Force.com fundamentals, which will help you in understanding the basics. Here is the link to the document.
https://resources.docs.salesforce.com/sfdc/pdf/salesforce_creating_on_demand_apps.pdf.