How to invoke Salesforce Apex REST using MuleSoft?

How to invoke Salesforce Apex REST using MuleSoft?

Flow:

Listener Configuration:

Transform Message Configuration:

Invoke Apex Rest Method Configuration:

Apex Class:

@RestResource(urlMapping='/MuleSoft/*')  
global with sharing class MuleSoftHandler {  
	
	@HttpPost  
	global static String callEmail( String msgbody, String subject ) {  
	  
		system.debug( 'Inside the callEmail' );  
	  
		try {  
  
			Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();       
			mail.setTargetObjectId( UserInfo.getUserId() );         
			mail.setSaveAsActivity( false );  
			mail.setSubject( 'Important: ' + subject );            
			mail.setHtmlBody( 'From MuleSoft<br/>' + msgbody );          
			Messaging.sendEmail( new Messaging.SingleEmailMessage[] { mail } );           
			return 'Email Sent successfully';  
			  
		} catch( Exception e ) {  
		  
			return e.getMessage();  
			  
		}  
		  
	}  
	  
} 

Output:

4 thoughts on “How to invoke Salesforce Apex REST using MuleSoft?

  • Hi,

    I am trying the same but in my case the Apex method don't have any parameter explicitly defined. It is taking the Parameters from its API Query Parameters. Like this:
    > String vParameter = RestContext.request.params.get('Parameter');
    Will Your configuration remains same in this scenario as well?
    I am asking this because I implemened the configuration in exact same way How this blog defines, but here I am getting an error. Here:
    Error Statement: Missing Value at 26 [character 27 line 1]
    Error Type: Salesforce:Invalid_Response

    It looks like some how this error is linked with the "Apex Class Method Name" field in the Invoke Apex Rest Method component, but I am not getting what it is.
    Can you provide some tip for this?

    Thanks and Regards,
    Rahul Trivedi

  • Hi,

    The Apex Class Method Name is OK?? I am getting an error: "Invalid field value: ^/MuleSoft^HttpPost^String"
    The name proposed by Studio is: "callEmail (callEmail^/MuleSoft/*^HttpPost^String^msgbody=String, subject=String)"

    Thanks

Leave a Reply