December 1, 2019

How to invoke Salesforce Apex REST using MuleSoft?

Flow:



Listener Configuration:





Transform Message Configuration:



Invoke Apex Rest Method Configuration:




Apex Class:


  1. @RestResource(urlMapping='/MuleSoft/*')  
  2. global with sharing class MuleSoftHandler {  
  3.     
  4.     @HttpPost  
  5.     global static String callEmail( String msgbody, String subject ) {  
  6.       
  7.         system.debug( 'Inside the callEmail' );  
  8.       
  9.         try {  
  10.   
  11.             Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();       
  12.             mail.setTargetObjectId( UserInfo.getUserId() );         
  13.             mail.setSaveAsActivity( false );  
  14.             mail.setSubject( 'Important: ' + subject );            
  15.             mail.setHtmlBody( 'From MuleSoft<br/>' + msgbody );          
  16.             Messaging.sendEmail( new Messaging.SingleEmailMessage[] { mail } );           
  17.             return 'Email Sent successfully';  
  18.               
  19.         } catch( Exception e ) {  
  20.           
  21.             return e.getMessage();  
  22.               
  23.         }  
  24.           
  25.     }  
  26.       
  27. }  

Output:



2 comments:

  1. 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

    ReplyDelete
    Replies
    1. Check the HTTP method in your class. You have to use the same in the Configuration

      Delete