Blog

Working with Salesforce sObjects with Composite Connector Part I

Sivendu SasidharanWritten by Sivendu Sasidharan

With MuleSoft Salesforce Integration and Anypoint Studio, every record in Salesforce is natively represented as an sObject in Apex. Each Salesforce record is represented as an sObject before it is inserted into Salesforce. Likewise, when persisted records are retrieved from Salesforce, they’re stored in an sObject variable. Standard and custom object records in Salesforce map to their sObject types in Apex. Here we will look at some common sObject type names in Apex used for standard objects.

SObject

Every record in Salesforce is natively represented as an sObject in Apex. Each Salesforce record is represented as an sObject before it is inserted into Salesforce. Likewise, when persisted records are retrieved from Salesforce, they’re stored in an sObject variable. Standard and custom object records in Salesforce map to their sObject types in Apex. Here are some common sObject type names in Apex used for standard objects.

  • Account
  • Contact
  • Lead
  • Opportunity

If you’ve added custom objects in your organization, use the API names of the custom objects in Apex. For example, a custom object called MainframeAccounts corresponds to the MainFrameAccounts__c sObject in Apex.

Working with SObject Tree

An sObject tree is a collection of nested, parent-child records with a single root record.

Creates one or more sObject trees with root records of the specified type with Create sobject tree

Creates one or more sObject trees with root records of the specified type

Add Salesforce Composite Connector from exchange

  • Used for creating one or more SObject trees having root records of the specified type.
  • SObject Tree is a collection of nested, parent-child records with a single root record.
  • In the request we should provide type and reference ID of each record

SObject Tree

The request can contain the following:

  • Up to a total of 200 records across all trees
  • Up to five records of different types
  • SObject trees up to five levels deep

Dataweave

%dw 2.0%dw 2.0

output application/java

---

payload.records map ((record , indexOfRecord) -> {
    "attributes": {
        "type": record.attributes.objectType,
        "referenceId": record.attributes.referenceId
    },
    "Name": record.Name,
    "Phone": record.Phone,
    "Website": record.Website,
    "NumberOfEmployees": record.NumberOfEmployees as Number,
    ("ChildAccounts": {
        "records": record.ChildAccounts.records map ((record01, indexOfRecord01) -> {
            "attributes": {
                "type": record01.attributes.objectType,
                "referenceId": record01.attributes.referenceId
            },
            "Name": record01.Name,
            "Phone": record01.Phone,
            "Website": record01.Website,
            "NumberOfEmployees": record01.NumberOfEmployees as Number
        })
    }),
    "Contacts": {
        "records": record.Contacts.records map ((record01, indexOfRecord01) -> {
            "attributes": {
                "type": record01.attributes.objectType,
                "referenceId": record01.attributes.referenceId
            },
            "LastName": record01.LastName,
            "Email": record01.Email,
            "Title": record01.Title
        })
    }
})

Request Body

The request body contains a records collection that includes sObject trees. Each record will have following properties

  • Attributes: Indicate attribute of a particular record
  • Has two sub properties
  • type: record’s type (example: Account)
  • referenceId: Reference ID for this record. Must be unique in the context of the request and start with an alphanumeric character.
  • Object fields: Field and value for this record
  • Child Relationships: Indicate child relationship of this record, example adding Contact (child) object inside Account(parent)

A JSON Request Example for creating Account object and two contact objects within Account

{
    "records":
    [
        {
            "attributes":
            {
                "type": "Account",
                "referenceId": "ref1"
            },
            "Phone": "4321098765",
            "Website": "www.salesforce.com",
            "Name": "Mule Account",
            "Contacts":
            {
                "records":
                [
                    {
                        "attributes":
                        {
                            "type": "Contact",
                            "referenceId": "ref2"
                        },
                        "Email": "test@salesforce.com",
                        "Title": "President",
                        "LastName": "Rush",
                        "FirstName": "Jake"
                    },
                    {
                        "attributes":
                        {
                            "type": "Contact",
                            "referenceId": "ref3"
                        },
                        "Email": "test123@salesforce.com",
                        "Title": "Administrator",
                        "LastName": "Devis",
                        "FirstName": "Jane"
                    }
                ]
            }
        }
    ]
}

Response Body

  • If successful, the response will contain the IDs of the created records
  • If any error occurs when creating a record, none of the records from the requests will be processed and the response will have only the reference ID of the record that caused the error and the error information.
  • Response will have two properties
  • hasErrors: true if an error occurred while creating a record; false otherwise
  • results:
    • Contains reference ID and new record ID of each record (upon success)
    • Contains only the reference ID of the record that caused the error, error status code, error message, and fields related to the error (upon failure)

Response Example

{
    "hasErrors": false,
    "results":
    [
        {
            "referenceId": "ref1",
            "id": "0012x00000ELhrzAAD"
        },
        {
            "referenceId": "ref2",
            "id": "0032x00000AKPESAA5"
        },
        {
            "referenceId": "ref3",
            "id": "0032x00000AKPETAA5"
        }
    ]
}
Account and Contact object created in Salesforce

Account and Contact object