3

I am creating a custom button on a custom object A. The custom object A has multiple accounts related to it. Custom object A has a lookup relation to Account.

In the button logic I am using Button or Link URL option to set the url of button. I wish to pass all the IDs of accounts which are part of custom object A. like:

https://test.salesforce.com/apex/AVisualForcePage?accid="all account id should be passed here"&scontrolCaching=1&sfdc.override=1....

I am trying to figure out how all the account id's(accid parameter) can be passed in this URL. The Javascript code in my Visualforce page has this function to parse the accid parameter:

Question: how can I pass multiple accountid values in the same parameter dynamically? Can you use a custom field on custom object A to store all the id's and set it in URL parameter?

// init method to load appropriate javascript!
            var init = function() {

                // adds custom contains method
                addContainsToString();
                // loads the tree based on JSON data
                loadTree({!jsonData});
                var key = getUrlParameter("accid");
                nodeSelection(key);


 var getUrlParameter = function getUrlParameter(sParam) {
            var sPageURL = decodeURIComponent(window.location.search.substring(1)),
                sURLVariables = sPageURL.split('&'),
                sParameterName,
                i;

            for (i = 0; i < sURLVariables.length; i++) {
                sParameterName = sURLVariables[i].split('=');

                if (sParameterName[0] === sParam) {
                    return sParameterName[1] === undefined ? true : sParameterName[1];
                }
              }
           }
2
  • 1
    Since you tagged this as visualforce and apex, have you considered using a standardSetController to accomplish this? That should be easier than trying to force a query string parameter to hold multiple values.
    – Derek F
    May 27 '16 at 20:46
  • I have'nt tried using standardSetController for this. However if use standard set controller how can I set the var key = getUrlParameter("accid"); in the Javascript code? My visualforce page logic is very tightly coupled with this Javascript code and I have to set the accid in nodeSelection(key); function. Any direction is highly appreciated! Thanks
    – SfdcBat
    May 27 '16 at 20:54
2

If you want all child Account records, just pass the parent Id and query for the children in your constructor. Or you can pass in the Account Id itself and query for the siblings.

public List<Account> children { get; private set; }
public MyExtension(ApexPages.StandardController controller)
{
    // if using parent
    children = [SELECT ... FROM Account WHERE Parent__c = :controller.getId()];
}
public MyExtension(ApexPages.StandardController controller)
{
    // if using Account
    Account record = [SELECT Parent__c FROM Account WHERE Id = :controller.getId()];
    children = [SELECT ... FROM Account WHERE Parent__c = :record.Parent__c];
}
3
  • Thanks Adrian. It is certainly the way but I am wondering how do I use this childeren id in my Javascript code. In javascript - var key = getUrlParameter("accid"); nodeSelection(key); - this part is used execute some logic. I need to pass each accid of the children. Can you please let me know how can I achieve that?
    – SfdcBat
    May 27 '16 at 20:59
  • @SfdcBat It is unclear what you are trying to accomplish in your JS, but you should be able to merge in the array of {!children} or {!siblings}, if you prefer.
    – Adrian Larson
    May 27 '16 at 21:00
  • Ok so here is what my javascript does - I have overriden the edit functionality of a page on object A(which has lookup to accounts). The page which I am editing has all accounts displayed in a tree form with check boxes. A dynaTree is used to do that. What my javascript does is to it sets the accountid using - nodeSelection(key); , since VF page mimics the edit functionality , my edit page should have those account selected(which are already the parent of custom object A) when I edit the page. For that I need to pass all the acctid to nodeSelection(key);
    – SfdcBat
    May 27 '16 at 21:08
1

Adding on to what Adrian mentioned, you can try below to see the different results. If you are using a detail button on the object you'll have the id in the URL for it, so then you can query for the accounts based on that. I wouldn't look into passing the accIds into the url.

global class OjbectACtrl {

    private final ObjectA__c objectA;
    public List<Account> accounts {get; set;}

    public OjbectACtrl(ApexPages.StandardController controller) {       
        objectA = (ObjectA__c)controller.getRecord();
        accounts = [SELECT Id, Name FROM Account WHERE ObjectA__c = :controller.getId()];
    }

    @RemoteAction
    global static List<Account> getAccounts(String recordId) {
        return [SELECT Id, Name FROM Account WHERE ObjectA__c = :recordId];
    }

    // If you need more control of what gets return you can use a wrapper   
   global class AccountWrapper {

       public String accId {get; set;}
       public String accountName {get; set;}

        public AccountWrapper(String accId, String accountName) {
            this.accId = accId;
            this.accountName = accountName;
        }
    }

    @RemoteAction
    global static List<AccountWrapper> getAccountWrapper(String recordId) {
        List<AccountWrapper> aws = new List<AccountWrapper>();
        List<Account> accounts = [SELECT Id, Name FROM Account WHERE ObjectA__c = :recordId];
        for (Account a: accounts) {
            AccountWrapper aw = new AccountWrapper(a.Id, a.Name);
            aws.add(aw);
        }    
        return aws;
    }
}

Then on the VF page:

<apex:page standardController="ObjectA__c" extensions="OjbectACtrl" >

<script>
// This is a string
var accounts = '{!accounts}';
function getAccounts() {
    var recordId='{!$CurrentPage.Parameters.id}'
    Visualforce.remoting.Manager.invokeAction('OjbectACtrl.getAccounts',
        recordId,                                      
        function(result, event) {
            if (event.status) {
                console.log('Account List Result as object array');
                console.log(result);
                // This will be valid JSON
                console.log(JSON.stringify(result));
            } else {
                console.log(event.message);
            }
        }, {
        escape: true
    });                                      
}
function getAccountWrapper() {
    var recordId='{!$CurrentPage.Parameters.id}'
    Visualforce.remoting.Manager.invokeAction('OjbectACtrl.getAccountWrapper',
        recordId,                                      
        function(result, event) {
            if (event.status) {
                console.log('Account Wrapper Result as object array');
                console.log(result);
                // This will be valid JSON
                console.log(JSON.stringify(result));
            } else {
                console.log(event.message);
            }
        }, {
        escape: true
    });                                      
}
</script>
</apex:page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.