1

I need to create lead records when a non-salesforce piece of software sends a POST request with data to my org. How is it done?

4

There are multiple steps involved for REST in salesforce.

  1. Create a Connected App from create apps option in salesforce.
  2. With this App you will have Client Id, secret key , user Name and Password for Oauth Authentication for token.
  3. Create an Apex class to authenticate for token initially using :

    https://login.salesforce.com/services/oauth2/token

  4. You can build a REST URL in salesforce using @RestResource in apex. This URL can be used by any third party applications to POST JSON formatted data.

URL for Application is : https://yourinstance.salesforce.com/services/apexrest/LeadCreation

Here is a sample REST URL build using apex:

@RestResource(urlMapping='/LeadCreation/*')
global with sharing class RESTLeadController {

@HttpPost  
  global static String createNewLead(String leadName, String leadNumber) {
     System.debug('Lead Name: '+leadName);
     System.debug('Lead Number: '+leadNumber);
     try
     {
         Lead ld = new Lead();
         ld.Name = leadName;
         ld.lead_Number__c = leadNumber;
         insert ld;
         return 'Lead has been inserted succesfully: '+ld.Id;
     }
     catch(DMLException de)
     {
        return de.getDmlMessage(0);
     }

     return null;
  }
}

Follow this blogs:

http://salesforcekings.blogspot.com/2015/12/how-to-test-restful-web-services-in.html

http://www.oyecode.com/2014/08/start-building-your-own-rest-api-in.html

3
  • the frustrating thing is trying to figure out how to to authenticate for token from the third party side...
    – Tyler Zika
    Sep 26 '16 at 23:49
  • if you don't want to authenticate just create a site and have this apex class over there and do a POST to that site URL and it may work.(just a guess) Sep 26 '16 at 23:59
  • @Tyler Zika hopefully this can help you? I first use Postman to authenticate. Once you have it in Postman, it's easier to transfer that to your app. I created 2 blog posts for this: blog.mkorman.uk/… and also blog.mkorman.uk/integrating-net-and-salesforce-part-1-rest-api (which may be helpful if the external app is in C#)
    – mkorman
    Sep 27 '16 at 9:18
-1

One way to achieve is this to create a VF which will handle the functionality you want to implement, on the data received by the POST functionality and expose this VF using QUICK ACTION/GLOBAL ACTION.

Controller

@RestResource(urlMapping='/POSTHandler') 
global class POSTHandler { 
    pubilc POSTHandler(){} 
    @HttpPost global static Void HandleInput(String Value) {
         // Code Statement to process the JSON input. 
         system.debug(Value); 
    } 
} 

VF Page

<apex:page action="HandleInput" controller="POSTHandler" /> 

Associate this VF to a Global Action and call this Setup using the URL

<-SFDC_ORG_URL->/services/data/v28.0/quickActions/POSTHandle

6
  • Can I get a code example? I can't seem to find an example anywhere.
    – Tyler Zika
    Sep 26 '16 at 23:24
  • Controller @RestResource(urlMapping='/POSTHandler') global class POSTHandler { pubilc POSTHandler(){} @HttpPost global static Void HandleInput(String Value) { // Code Statement to process the JSON input. system.debug(Value); } } VF Page <apex:page action="HandleInput" controller="POSTHandler" /> Associate this VF to a Global Action and call this Setup using the URL <SFDC_ORG_URL>/services/data/v28.0/quickActions/POSTHandler <br/> Sorry new to SE, cant edit the code. ps: Code is not tested. it is a vague idea.
    – blackwind
    Sep 27 '16 at 1:16
  • 1
    @blackwind I don't think this is suitable for the part of the question that states that data is coming from a third party system (i.e. not from within the Salesforce UI). Also, code snippets in comments is near impossible to read. This is one of the things that you should edit your post to include.
    – Derek F
    Sep 27 '16 at 2:03
  • Hi @DerekF, thanks for the tip, i have updated the code. Also i have used the above method to send the data from 3rd party application to the sales force org.
    – blackwind
    Sep 27 '16 at 2:15
  • @blackwind the vf page in your example is called POSTHandle? Is the quickActions, in the url example, the same quick actions used in SF1?
    – Tyler Zika
    Sep 30 '16 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.