DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • What is MuleSoft and Anypoint Platform Capabilities and Strengths
  • MuleSoft Integrate With ServiceNow
  • The Role of MuleSoft in Hybrid Integration: Bridging On-Premises and Cloud Environments
  • Build RAML-Based API Specification Using MuleSoft Platform

Trending

  • A Complete Guide to Modern AI Developer Tools
  • Why Documentation Matters More Than You Think
  • Scaling Mobile App Performance: How We Cut Screen Load Time From 8s to 2s
  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Integrating Salesforce APEX REST

Integrating Salesforce APEX REST

In this post, walk through the process of invoking an APEX REST method in MuleSoft and fetching account information from Salesforce using APEX code snippets.

By 
Karan Gupta user avatar
Karan Gupta
·
Mar. 26, 24 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
4.8K Views

Join the DZone community and get the full member experience.

Join For Free

In the realm of modern enterprise integration, the harmonious interaction between different systems and platforms is paramount. Salesforce, being one of the leading CRM platforms, often necessitates seamless integration with other systems to leverage its data and functionalities. MuleSoft, on the other hand, is a powerful integration platform that facilitates connecting various applications and APIs.

One common scenario in Salesforce integration is invoking APEX REST methods from MuleSoft to interact with Salesforce data. In this blog post, we'll walk through the process of invoking an APEX REST method in MuleSoft and fetching account information from Salesforce using APEX code snippets.

Why Use APEX REST Methods?

Salesforce APEX REST methods provide a flexible way to expose custom functionalities or access Salesforce data through RESTful APIs. Leveraging APEX REST methods allows for fine-grained control over what data is exposed and how it's accessed, making it a preferred choice for integrating Salesforce with external systems like MuleSoft.

Prerequisites

Before we dive into the integration process, ensure you have the following prerequisites in place:

  • Salesforce developer account: You'll need a Salesforce Developer account to create and test APEX REST methods.
  • MuleSoft Anypoint platform account: Sign up for MuleSoft Anypoint Platform to create Mule applications.

Integration Steps

1. Create APEX REST Method in Salesforce

First, let's create an APEX REST method in Salesforce to fetch account information. Here's a simple example:

Java
 
apex
@RestResource(urlMapping='/accountInfo/*')
global with sharing class AccountInfoRestController {
@HttpGet
global static Account getAccountInfoById() {
RestRequest req = RestContext.request;
String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);

// Fetch Account information by Id
Account acc = [SELECT Id, Name, Industry, Phone, BillingCity FROM Account WHERE Id=:accountId];

return acc;
}
}


In this code snippet, we define an APEX class AccountInfoRestController annotated with @RestResource to expose a REST endpoint /accountInfo/. The getAccountInfoById() method retrieves account information based on the provided Account ID.

2. Deploy APEX Class in Salesforce

Deploy the AccountInfoRestController class to your Salesforce organization.

3. Create MuleSoft Application

Create a new MuleSoft application in Anypoint Studio.

  • File --> New --> Mule Project

4. Configure HTTP Connector

Add an HTTP Listener to your Mule flow to receive incoming HTTP requests. Configure the listener with the appropriate host and port.

HTTP Listener config

5. Invoke the APEX REST Method in MuleSoft

There are 2 ways to invoke the APEX REST method in MuleSoft.

Invoke APEX REST method

5.1. Using HTTP Requester

Add an HTTP Request component after the HTTP Listener and configure it to make a GET request to the Salesforce APEX REST endpoint:

  • URL: Specify the URL of the APEX REST method in the format https://<Salesforce_Instance_URL>/services/apexrest/accountInfo/<Account_Id>. 
  • Method: Set the method to GET.
  • Headers: Include any required headers, such as authentication tokens.

Add an HTTP Request component after the HTTP Listener and configure it to make a GET request to the Salesforce APEX REST endpoint

5.2. Using Invoke APEX Rest API Connector

Add an Invoke APEX Rest API Connector after the HTTP listener and configure it to make a GET request to the Salesforce APEX REST endpoint:

  • APEX class: Specify the name of the class AccountInfoRestController
  • APEX method: This is the combination of 4 attributes: methodName^urlMapping^httpMethod^returnType, getAccountInfoById^/accountInfo^HttpGet^Account

Invoke APEX REST method


  • Salesforce configuration: Basic Authentication 
    • Username: Your login user ID
    • Password: Your login password
    • Security Token: Generate from Profile --> Settings --> Reset My Security Token

Salesforce config

6. Parse Response

After invoking the APEX REST method, parse the response body to extract account information. Use transform message to send payload in required structure to calling flow.

JSON
 
{
	"Id": "xxxxxxxxxxxxxxx",
	"Name": "Example Account",
	"Industry": "Technology",
	"Phone": "(555) 555-5555",
	"BillingCity": "San Francisco" 
} 


7. Handle Errors and Transform Data

Implement error handling and data transformation as per your application requirements.

8. Complete the Mule Flow

Complete the Mule flow with the necessary components for further processing or response generation.

Conclusion

Integrating Salesforce APEX REST methods in MuleSoft enables seamless interaction between Salesforce and other systems. By following the steps outlined in this guide, you can effectively invoke APEX REST methods from MuleSoft and retrieve account information from Salesforce. This integration approach empowers organizations to unlock the full potential of their Salesforce data within their broader application ecosystem.

Customer relationship management Enterprise integration Integration platform MuleSoft REST

Opinions expressed by DZone contributors are their own.

Related

  • What is MuleSoft and Anypoint Platform Capabilities and Strengths
  • MuleSoft Integrate With ServiceNow
  • The Role of MuleSoft in Hybrid Integration: Bridging On-Premises and Cloud Environments
  • Build RAML-Based API Specification Using MuleSoft Platform

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!