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
sfdc_newbie17sfdc_newbie17 

Passing multiple id's in parameter for a SOQL query

Hi all

Very new to apex development but looking to pass through multiple known id's to a custom VF page to return details of accounts details.

Is this possible and does anyone have any examples.

Thanks in advance
Best Answer chosen by sfdc_newbie17
SwethaSwetha (Salesforce Developers) 
HI ,

Yes, it is possible to pass multiple IDs to a custom Visualforce page to return details of accounts. You can do this by using the ids parameter in the page's URL. The ids parameter should be a comma-separated list of account IDs. For example, the following URL would pass the IDs of accounts 0012345678901234, 0012345678901235, and 0012345678901236 to the page:
 
/apex/MyCustomPage?ids=0012345678901234,0012345678901235,0012345678901236
Once the page has received the IDs, you can use them to query the Account object. For example, the following code would query the Account object for all accounts with the IDs that were passed to the page:
public List<Account> getAccountDetails(List<Id> accountIds) {
    return [SELECT Id, Name, Industry, Phone FROM Account WHERE Id IN :accountIds];
}
Here is an example of a custom Visualforce page that can be used to display the details of accounts that have been passed to the page:
<apex:page controller="YourController">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockTable value="{!accountDetails}" var="account">
                <apex:column value="{!account.Name}" />
                <apex:column value="{!account.Industry}" />
                <apex:column value="{!account.Phone}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>


Controller class
public class YourController {
    public List<Account> accountDetails { get; set; }

    public YourController() {
        List<Id> accountIds = ApexPages.currentPage().getParameters().get('ids').split(',');
        accountDetails = getAccountDetails(accountIds);
    }

    public List<Account> getAccountDetails(List<Id> accountIds) {
        return [SELECT Id, Name, Industry, Phone FROM Account WHERE Id IN :accountIds];
    }
}

If this information helps, please mark the answer as best. Thank you
 

All Answers

SwethaSwetha (Salesforce Developers) 
HI ,

Yes, it is possible to pass multiple IDs to a custom Visualforce page to return details of accounts. You can do this by using the ids parameter in the page's URL. The ids parameter should be a comma-separated list of account IDs. For example, the following URL would pass the IDs of accounts 0012345678901234, 0012345678901235, and 0012345678901236 to the page:
 
/apex/MyCustomPage?ids=0012345678901234,0012345678901235,0012345678901236
Once the page has received the IDs, you can use them to query the Account object. For example, the following code would query the Account object for all accounts with the IDs that were passed to the page:
public List<Account> getAccountDetails(List<Id> accountIds) {
    return [SELECT Id, Name, Industry, Phone FROM Account WHERE Id IN :accountIds];
}
Here is an example of a custom Visualforce page that can be used to display the details of accounts that have been passed to the page:
<apex:page controller="YourController">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockTable value="{!accountDetails}" var="account">
                <apex:column value="{!account.Name}" />
                <apex:column value="{!account.Industry}" />
                <apex:column value="{!account.Phone}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>


Controller class
public class YourController {
    public List<Account> accountDetails { get; set; }

    public YourController() {
        List<Id> accountIds = ApexPages.currentPage().getParameters().get('ids').split(',');
        accountDetails = getAccountDetails(accountIds);
    }

    public List<Account> getAccountDetails(List<Id> accountIds) {
        return [SELECT Id, Name, Industry, Phone FROM Account WHERE Id IN :accountIds];
    }
}

If this information helps, please mark the answer as best. Thank you
 
This was selected as the best answer
sfdc_newbie17sfdc_newbie17
Many thanks for this Swetha. I did manageto get this working with single id's but now working with multiple.

How would I go about writing a test class for this so I could migrate to production?
SwethaSwetha (Salesforce Developers) 
Try below
@isTest
private class YourControllerTest {
    @isTest
    static void testGetAccountDetails() {
        // Create test accounts
        List<Account> testAccounts = new List<Account>();
        testAccounts.add(new Account(Name = 'Test Account 1', Industry = 'Test Industry 1', Phone = '1234567890'));
        testAccounts.add(new Account(Name = 'Test Account 2', Industry = 'Test Industry 2', Phone = '9876543210'));
        insert testAccounts;

        // Create test page parameters
        ApexPages.currentPage().getParameters().put('ids', String.join(new List<Id>{testAccounts[0].Id, testAccounts[1].Id}, ','));

        // Instantiate the controller
        YourController controller = new YourController();
        List<Account> accountDetails = controller.getAccountDetails(new List<Id>{testAccounts[0].Id, testAccounts[1].Id});

        // Assert the results
        System.assertEquals(2, accountDetails.size());
        System.assertEquals('Test Account 1', accountDetails[0].Name);
        System.assertEquals('Test Industry 1', accountDetails[0].Industry);
        System.assertEquals('1234567890', accountDetails[0].Phone);
        System.assertEquals('Test Account 2', accountDetails[1].Name);
        System.assertEquals('Test Industry 2', accountDetails[1].Industry);
        System.assertEquals('9876543210', accountDetails[1].Phone);
    }
}