Salesforce Query Limits and Best practices to use minimum api calls

Author - Tanuj Sharma
2.05.2018
|
0 Comments
|

The maximum number of records that we can retrieve using a single Salesforce API call is 2000. Below are some points using which we can reduce our API calls to not to cross Salesforce API call limits:

* Instead of using Salesforce operations like update(), upsert() for each record you can process records in batches. Processing records one at a time affects performance and will also consume a lot of API calls. So batch your changes in an array and pass that array in a single call. For examples suppose you have a number of opportunities to create in salesforce so instead of using API call for creating each opportunity you can batch an array of opportunities and pass it in single API call.

* Suppose you have many related records (parents and their child records) and you want to delete the parent as well as children records. In this case instead of deleting child records one by one you can directly delete the parent. It will delete all child records also that are related to it. For cascade delete we need Master-detail relationship between parent to child objects. If there is a master-detail relationship in which a parent object is master and child is its detail and then if we try to delete that parent then its related child records also get deleted. If we don’t want to delete child records on deletion of parents then we can use look-up relationship. And if we have to delete only children then we can delete it one by one.

You can also use Salesforce composite resources to make multiple requests using a single REST API.

1. Composite/batch/: You can use composite batch resources when you have many independent REST API calls that don’t depend on each other. To make a batch resource call issue a POST instance endpoint/services/data/API version/composite/batch/ with a request body that contains an array of REST API subrequests. For example:

Request:

"batchRequests": [{
        "method": "GET",
        "url": "v34.0/sobjects/Contact/00252000005dqHkABK?fields=Name,Phone",

    },
    {
        "method": "GET",
        "url": "v34.0/sobjects/Opportunity/0061I000005e5D7?fields=Opportunity Name,Stage",

    }
]

Response:

{
    "hasErrors": false,
    "results": [{
            "statusCode": 200,
            "result": {
                "attributes": {
                    "type": "Contact",
                    "url": "/services/data/v34.0/sobjects/Contact/00252000005dqHkABK"
                },
                "Name": "Tanuj",
                "Phone": "3475983498"
            }
        },
        {
            "statusCode": 200,
            "result": {
                "attributes": {
                    "type": "Opportunity",
                    "url": "/services/data/v34.0/sobjects/Opportunity/0061I000005e5D7"
                },
                "Opportunity Name": "Salesforce Opportunity",
                "Stage": "Closed Won"
            }
        }
    ]
}

So by using this, you can perform multiple independent operations in a single API call.

2. Composite/tree/: You can use composite tree resources to create multiple object records of the same type along with child records. For example :

Request:

"records": [{
    "attributes": {
        "type": "Account",
        "referenceId": "ref1"
    },
    "name": "WebnersAccount1",
    "numberOfEmployees": "100",
    "industry": "Banking"
}, {
    "attributes": {
        "type": "Account",
        "referenceId": "ref2"
    },
    "name": "WebnersAccount2",
    "numberOfEmployees": "250",
    "industry": "Banking"
}]

Response :

{
"hasErrors": false,
	"results": [{
		"referenceId": "ref1",
		"id": "001D000000K0fXOIAZ"
	}, {
		"referenceId": "ref2",
		"id": "003D000000QV9n2IAD"
	}]
}

It uses two single-record SObject trees to create two unrelated Account records, with each Account record having no child records.

And below is an example of request body that uses one SObject tree to create a single Account record along with two child contact records:

Request:

"records": [{
    "attributes": {
        "type": "Account",
        "referenceId": "ref1"
    },
    "name": "WebnersAccount",
    "Contacts": {
        "records": [{
            "attributes": {
                "type": "Contact",
                "referenceId": "ref2"
            },
            "lastname": "abc",
            "email": "xyz@salesforce.com"
        }, {
            "attributes": {
                "type": "Contact",
                "referenceId": "ref3"
            },
            "lastname": "dfgf",
            "email": "xyz@salesforce.com"
        }]
    }
}]

Response :

	{
	    "hasErrors": false,
	    "results": [{
	        "referenceId": "ref1",
	        "id": "001D000000K0fXOIAZ"
	    }, , {
	        "referenceId": "ref2",
	        "id": "003D000000QV9n2IAD"
	    }, {
	        "referenceId": "ref3",
	        "id": "003D000000QV9n3IAD"
	    }]
	}

3. Composite/: You can use composite resource when you need to take input from one subrequest and use it in another subrequest. We need to provide referenceId for each subrequest so we can use this reference ID to pass output from one subrequest to input of another.

For example:

Request:

"compositeRequest": [{
    "method": "POST",
    "url": "/services/data/v38.0/sobjects/Account",
    "referenceId": "newAccount",
    "body": {
        "Name": "New Account"
    }
}, {
    "method": "POST",
    "url": "/services/data/v38.0/sobjects/Contact",
    "referenceId": "newContact",
    "body": {
        "LastName": "New Contact",
        "AccountId": "@{newAccount.id}"
    }
}]

Response:

{
    "body": {
        "id": "001R00000033I6AIAU",
        "success": true,
        "errors": []
    },
    "httpHeaders": {
        "Location": "/services/data/v38.0/sobjects/Account/001R00000033I6AIAU"
    },
    "httpStatusCode": 201,
    "referenceId": "newAccount"
}

In this example request body will create an Account record and then creates a Contact record using the ID of the created account.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *