MuleSoft
Upserting Null values to Salesforce using MuleSoft

Upserting Null values to Salesforce using MuleSoft

Pre-requisites

  • We need to have an Anypoint Studio should be setup.
  • We need to have Salesforce developer account.
  • Create a Salesforce custom object with some fields, as per the requirement.
  • Click on create in the home section, which is on the right-hand side. Then click on custom object, then fill-in the name of the custom object. Then click on fields and relationship, then select the fields with the respective type of information.

  • In this scenario, we’ll be having 3 fields i.e. Id c, Name c, and Price__c fields are having text as the type of information.

  • We need to keep the security token handy for that, click on profile and then click on setting which is on the right-hand top corner of the screen and type security token in the quick find section, and click on reset security token.
  • The security token will be sent to the registered mailId.

Upserting Null values to Salesforce

  • When building Salesforce integrations with MuleSoft, one challenge developers often encounter is handling null values during upserts.
  • By default, Salesforce ignores nulls when updating records, leaving old values untouched. This can lead to data inconsistencies between systems.
  • Upsert Operation: The upsert operation is a combination of insert and update.
    • If the external ID or matching criteria exists, Salesforce updates the record.
    • If not, Salesforce creates a new record.
  • Why This Matters :- Consider a Salesforce Account record that already has a Phone number. If your source system sends an update with “Phone”: null, Salesforce will simply ignore the null and keep the old Phone value.
  • But sometimes we need the field cleared β€” for example, if the phone number is no longer valid. That’s why explicit null handling is essential.
  • To ensure that null values are properly applied during an upsert, Salesforce requires explicit handling of nulls. This means the integration layer (such as MuleSoft) must send the field with a null value in the request payload. When configured correctly, Salesforce will update the record by setting that field to null, rather than retaining the previous value.
  • In this blog, I am going to demonstrate how to configure MuleSoft to explicitly handle null values during upsert operations so that Salesforce fields are actually cleared instead of retaining old data.

Steps:

  1. To begin, a new Mule project needs to be created. Then, a HTTP listener component dragged and dropped into the project from the HTTP module. Configure the listener, and set the host to All interface (0.0.0.0), port number as 8081. Set the path as /nullValUpsert.

  1. Following the addition of the listener component, include two loggers to mark the beginning and the end of the flow. By logging the message β€œStart of flow” ++ (flow.name as String), in expression mode.

  1. Next, drag and drop the “Upsert” component from the Salesforce module onto the canvas and configure it as demonstrated below. In this example, we are working with the custom object β€œobject__c”, and the connector configuration references a secure properties file for enhanced security

  1. Next, we will add a Transform Message component to map the incoming payload into Salesforce fields and handle nulls by using a DataWeave expression with the fieldsToNull.property read, they are transformed into binary format.
  2. The setup is complete. We’ll deploy the application, by passing the environment variables and key for the secure properties.
  1. Next, prepare a JSON array with multiple records (id, name, and price) and send it in the request body to insert those records into the Salesforce custom object object__c.
  2. Open Salesforce Workbench, log in with your Salesforce credentials, go to Queries β†’ SOQL Query, and run SELECT Id, Name, Price__c FROM object__c to verify whether the records have been inserted successfully.
  3. Next, send an update request with a payload where one record has fields set to null and another record has updated values. For example, record id=6 is sent with name=null and price=null, which clears the fields in Salesforce. Meanwhile, record id=7 is updated with name=”headset” and price=2000. This demonstrates how Salesforce handles null values vs. updated values during an upsert operation.
  4. Now, go to Salesforce Workbench, run the same SOQL query as before, and check the records. You will notice that the record updated with null values no longer holds its previous values β€” instead, those fields are stored as null in Salesforce

πŸš€ Ready to Build Smarter Salesforce Integrations?

Handling null values incorrectly can silently break your data consistency. Don’t let integration gaps impact your business decisions.

At TGH, we specialize in enterprise-grade MuleSoft and Salesforce integrations β€” ensuring accurate data sync, clean field updates, and scalable API-led connectivity.

Whether you’re optimizing existing integrations or building from scratch, our experts can help you implement secure, future-ready solutions.

πŸ“ž +91 88106 10395
🌐 https://techygeekhub.com/contact-us

Talk to our integration specialists today and make your Salesforce data work flawlessly.

Author

TGH Software Solutions Pvt. Ltd.

Leave a comment

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