You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python从Salesforce API获取超200条自定义对象记录

Fixing the SalesforceMalformedRequest Error with FIELDS(CUSTOM) and Large Record Sets

Hey there! Let's tackle that error you're seeing when trying to pull all 1044 records from your ag1__c custom object.

Why You're Getting This Error

Salesforce imposes a strict limit when using the FIELDS(CUSTOM) (or FIELDS(ALL)) function: you can't set a LIMIT higher than 200 in a single query, even if you use query_all() under the hood. query_all() handles pagination for large result sets, but the FIELDS() function itself blocks any request with a LIMIT over 200.

Solution 1: Batch Your Queries with OFFSET

Since we can't pull more than 200 records at once with FIELDS(CUSTOM), we'll loop through batches of 200 using OFFSET to fetch all records incrementally. Here's how to modify your code:

from simple_salesforce import Salesforce
import pandas as pd

uname = 'USER_NAME'
passwd = 'PASSWORD'
token = 'SECURITY_TOKEN'

sfdc_query = Salesforce(username=uname, password=passwd, security_token=token)

# First get the total number of records to know how many batches we need
count_query = "SELECT COUNT(Id) FROM ag1__c"
count_result = sfdc_query.query(count_query)
total_records = count_result['records'][0]['expr0']

all_records = []
batch_size = 200
offset = 0

while offset < total_records:
    # Build the SOQL for each batch
    soql = f"SELECT FIELDS(CUSTOM) FROM ag1__c LIMIT {batch_size} OFFSET {offset}"
    batch_result = sfdc_query.query(soql)
    all_records.extend(batch_result['records'])
    # Move to the next batch
    offset += batch_size

# Convert to DataFrame
sfdc_df = pd.DataFrame(all_records)
print(sfdc_df)

Solution 2: Explicitly List Custom Fields (Better for Performance)

If your ag1__c object doesn't have hundreds of custom fields, it's more efficient to explicitly list the fields you need instead of using FIELDS(CUSTOM). This avoids the 200-record limit entirely, and query_all() can pull all records in one go (handles pagination automatically).

First, we'll fetch the list of custom fields for ag1__c, then build our SOQL:

from simple_salesforce import Salesforce
import pandas as pd

uname = 'USER_NAME'
passwd = 'PASSWORD'
token = 'SECURITY_TOKEN'

sfdc_query = Salesforce(username=uname, password=passwd, security_token=token)

# Get all custom fields for ag1__c
object_description = sfdc_query.ag1__c.describe()
custom_fields = [field['name'] for field in object_description['fields'] if field['custom']]

# Add Id (usually useful to include even if it's standard)
custom_fields.append('Id')

# Build SOQL with explicit fields
soql = f"SELECT {', '.join(custom_fields)} FROM ag1__c"
sfdc_rec = sfdc_query.query_all(soql)

sfdc_df = pd.DataFrame(sfdc_rec['records'])
print(sfdc_df)

Which Solution Should You Use?

  • Use Solution 1 if you have a huge number of custom fields and don't want to list them all.
  • Use Solution 2 for better performance and fewer API calls (since you're not looping through batches).

内容的提问来源于stack exchange,提问作者Rahulrvz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 17:30:40