Wednesday 23 December 2020

Composite Graph API

 Hi,

Here we are going to learn what is Composite Graph API and how it works.

What is Composite Graph API:

  • It gives us the ability to execute multiple API requests in a single API call.
  • We can perform CRUD operations on a large number of related sObjects in a single API call, eliminating the need to orchestrate those relationships in custom code or perform multiple round trips to the server.
  • This increases the subrequest limit from 25 to 500, and also provides a number of optimizations that ensure records are processed efficiently and operations are rolled back if any steps are not completed.
  • Grouping collections of sObjects into graphs lets us do more with a single API call.

  • We can construct multiple graphs of related sObjects to insert multiple records within 
  • the same transaction. 
  • We can also access current records and then add records to them using reference ids.

How to Organize Subrequests Within a Graph:

  • The body of a Composite Graph API request consists of a number of graphs, each of which may contain multiple composite subrequests. 
  • Think of each graph as its own grouping of related sObject records.
Sample:

{
  "graphs": [
    {
      "graphId": "graphId",
      "compositeRequest": [
        compositeSubrequest1, 
        compositeSubrequest2, 
        ...]
    },
    {
      "graphId": "graphId2",
      "compositeRequest": [
        compositeSubrequest3, 
        compositeSubrequest4, 
        ...]
    }
  ]
}

Example:
{
  "graphs": [
    {
      "graphId": "graph1",
      "compositeRequest": [
        {
          "method": "PATCH",
          "url": "/services/data/v50.0/sobjects/Account/ExternalAcctId__c/ID12345",
          "referenceId": "newAccount",
          "body": {
            "Name": "Trailblazers",
            "Website": "TrailblazerOutfiters.com"
          }
        },
        {
          "method": "POST",
          "url": "/services/data/v50.0/sobjects/Order__c",
          "referenceId": "newOrder1",
          "body": {
            "Account__c": "@{newAccount.id}"
          }
        },
        {
          "method": "POST",
          "url": "/services/data/v50.0/sobjects/OrderItem__c",
          "referenceId": "newProduct1",
          "body": {
            "Order__c": "@{newOrder1.id}",
            "Product__c": {
              "External_Id__c": "EB1213"
            },
            "Qty_L__c": "1",
            "Price__c": "500"
          }
        }
      ]
    }
  ]
}

Here
  • Each subrequest contains a referenceId that can be used to relate records that follow the subrequest.
  • In the example above, the Account record is upserted and the referenceId is set to newAccount.
  • In the subrequest that follows, an order record is inserted and the Account__c field value is set to @{newAccount.Id}
  • which references the account that was just inserted.
  • Then that order record's referenceId (newOrder1) is used for the Order__c field value (@{newOrder1.id}).
curl Request:

curl --request POST \
--header "Authorization: Bearer token" \
--header "Content-Type: application/json" \
--data @data.json \
instance.salesforce.com/services/data/vXX.X/composite/graph

Sample HTTP Request:

String baseURL = URL.getSalesforceBaseUrl().toExternalForm(); 
String compositeGraphURL = baseURL + '/services/data/v50.0/composite/graph/';  
  
HttpRequest request = new HttpRequest();  
request.setMethod('POST');   
request.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());        
request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); 
request.setHeader('Content-Type', 'application/json');
 //Here we are getting data in JSON format from static resource

StaticResource resource = [
  SELECT Id, Body 
  FROM StaticResource 
  WHERE Name = 'graphs' 
  LIMIT 1
];
String body = resource.Body.toString();
  
request.setBody(body);  
request.setEndpoint(compositeGraphURL);  
String prettyResponse = '';
try {  
  Http http = new Http();   
  HttpResponse response = http.send(request);  
  if (response.getStatusCode() == 200 ) {  
    prettyResponse = JSON.serializePretty( JSON.deserializeUntyped(response.getBody()) );  
  } else {  
    System.debug(' response ' + response.getBody() );  
    throw new CalloutException( response.getBody() );  
  }   
} catch( System.Exception e) {  
  System.debug('ERROR: '+ e);  
  throw e;  
}  
System.debug('Response: ' + prettyResponse );




No comments:

Post a Comment

How to include a screen flow in a Lightning Web Component

 Hi, Assume  you have a flow called "Quick Contact Creation" and API Name for the same is "Quick_Contact_Creation". To i...