Why Isn’t My Salesforce Connect Adapter Paginating?

Spread the love

Question

I’m using a custom Salesforce Connect adapter to fetch messages from Microsoft Graph API, but I’m facing two issues: excessive initialization of the DataSource.Connection and fetching all records instead of paginating correctly. Despite setting up pagination with queryMoreToken, all records are being retrieved, and the list view continues fetching data as the user scrolls. How can I resolve this issue and properly paginate the data?

Answer

The issue you’re encountering with excessive initialization of the connection class and full data retrieval despite implementing pagination likely stems from the way Salesforce Connect handles pagination and connection instantiation.

Server-Driven Paging vs. Client-Driven Paging:

In Salesforce, server-driven paging means that once a queryMoreToken is returned by the method, Salesforce will continue calling the method until all data is retrieved. This is why your entire dataset is being fetched at once, even when the UI should only require the first 50 records. The issue occurs because Salesforce automatically expects all records to be retrieved, not just the initial batch.

To resolve this, you need to tell Microsoft Graph not to return the @odata.nextLink (or queryMoreToken) once the required number of records has been retrieved. This can be controlled through the Microsoft Graph API by using the $top and $skip parameters to manage the size and offset of the data being fetched, as shown below:

Integer top = BATCH_SIZE > queryContext.maxResults 
    ? queryContext.maxResults : BATCH_SIZE;
Integer skip = queryContext.offset;
List<OutlookData> rawRecords = new List<OutlookData>();
result.Rows = new List<Map<String, Object>>();
do {
    rawRecords = callMicrosoftGraphApi(top, skip);
    result.Rows.addAll(mapData(rawRecords));
    skip += top;
} while(rawRecords.size() == top);

This approach ensures that only the required number of records are fetched, reducing unnecessary calls to Microsoft Graph and preventing Salesforce from re-fetching the same data. You can use the queryContext.offset as the skip parameter in the Microsoft API, and queryContext.maxResults will map to the top parameter. This way, Salesforce can handle pagination more efficiently without retrieving the entire dataset upfront.

Excessive DataSource.Connection Initializations:

As for the frequent initialization of DataSource.Connection, this is typically observed when rendering list views in Salesforce. Although this behavior may seem excessive, it usually doesn’t cause significant delays unless the number of initializations is extremely high. One way to mitigate this is by reducing the number of times the connection is initialized, especially if you’re using batch sizes efficiently. You can consider using a client-side approach for pagination if server-driven paging proves to be inefficient for your use case.

Client-Side Paging:

If you want to avoid these issues entirely, client-side paging might be a more straightforward solution. In this model, you would no longer rely on the queryMoreToken and instead control pagination with the offset and maxResults parameters. By doing so, Salesforce will make fewer calls to the server and will only request the records needed for display.

In summary, to fix your issues:

  1. Consider switching to client-side pagination by using offset and maxResults.
  2. If using server-side pagination, ensure that Microsoft Graph does not return unnecessary @odata.nextLink tokens after the required data is retrieved.
  3. Monitor the connection initialization behavior, and consider adjusting batch sizes to optimize performance.

Job-Oriented Salesforce Training with 100% Money Back Guarantee

Our Salesforce Course is thoughtfully designed to provide a comprehensive understanding of the Salesforce platform, equipping you with the essential skills needed to thrive in the CRM industry. The program includes core modules such as Salesforce Admin, Developer, and AI, integrating foundational knowledge with hands-on training. By engaging in practical assignments and real-world projects, you’ll gain the expertise to confidently tackle complex business challenges using Salesforce solutions. Our seasoned instructors ensure you acquire both technical skills and industry-relevant insights to excel in the Salesforce ecosystem.

Beyond technical proficiency, Salesforce training in Hyderabad offers personalized mentoring, certification support, and interview preparation to enhance your career prospects. You’ll have access to extensive study materials, hands-on project opportunities, and dedicated guidance throughout your learning journey. By the end of the program, you’ll be well-prepared for certification exams and equipped with the real-world skills and problem-solving capabilities that employers seek. Start your Salesforce career with us and open the door to countless opportunities. Enroll for a Free Demo today!

Open Chat
1
Dear Sir/Madam
How can I help you?