function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nicolás KacowiczNicolás Kacowicz 

Insert data into Salesforce

Hello, I'm just starting to use Salesforce.
Let's say I've got a website with a form to insert a Contact. After the contact is inserted on my web, I want to send a JSON to Salesforce so this Contact is inserted on the Contact Object.

I don't quite understand what I need on my website to communicate with Salesforce.

Thanks!!
Best Answer chosen by Nicolás Kacowicz
NForceNForce
@Nicolas: No, AnimalLocator is a get method to received data from heroku app. 
SALESFORCE:
@RestResource(urlMapping='/contacts/v1')
global with sharing class{

@HttpPost
global static void createContact(String fname, string lname){
Contact c = new contact(firstname=fname, lastname=lname);

insert c; //creates a contacts
}
}
}
in website
authenticate salesforce and invoke this endpoint

Thanks,
NForce

All Answers

NForceNForce
HI Nicolas,
# Create an endpoint in salesforce(Rest resource with put method) which receives input from website
# Authenticate salesforce in the website logic and hit endpoint with the contact data.

Thanks,
Nicolás KacowiczNicolás Kacowicz
I've done this example of the Trailhead: https://trailhead.salesforce.com/modules/apex_integration_services/units/apex_integration_rest_callouts
The only difference would be that I need to insert this data, right?

My class was:

public class AnimalLocator {
    
    public static String getAnimalNameById (Integer id) {
        
        String retName;
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + id);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) {
            // Deserializes the JSON string into collections of primitive data types.
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            // Cast the values in the 'animals' key as a list
            Map<String,Object> animal = (Map<String,Object>)results.get('animal');
            retName = (String)animal.get('name');
        }
        return retName;
    }
}
NForceNForce
@Nicolas: No, AnimalLocator is a get method to received data from heroku app. 
SALESFORCE:
@RestResource(urlMapping='/contacts/v1')
global with sharing class{

@HttpPost
global static void createContact(String fname, string lname){
Contact c = new contact(firstname=fname, lastname=lname);

insert c; //creates a contacts
}
}
}
in website
authenticate salesforce and invoke this endpoint

Thanks,
NForce
This was selected as the best answer
Tuan LuTuan Lu
You might want to check out WebToLead too. Its a lot simpler and it safeguards you from creating a lot of duplicate or unqualified contacts by using a Lead queue.
Nicolás KacowiczNicolás Kacowicz
@Tuan Thanks for answering. But let's say I don't want to insert a Contact but rather insert an Account or some data into a custom Object. It really doesn't help me the WebToLead feature, does it?
Nicolás KacowiczNicolás Kacowicz
I've been readying and have some doubts how to connect things.

I created this class:

@RestResource(urlMapping='/Contact')
global with sharing class AccountPostTest {
    @HttpPost
    global static ID createContact(String firstName, String lastName, String phoneNumber, String email) {
        Contact c = new Contact();
        c.FirstName = firstName;
        c.LastName = lastName;
        c.Phone = phoneNumber;
        c.Email = email;
        
        insert c;
        return c.Id;
    }
}

Then I go to https://workbench.developerforce.com/restExplorer.php and send a contact like this:

{
  "firstName" : "myName",
  "lastName" : "myLastName",
  "phoneNumber" : "1234567",
  "email" : "myName@company.com"
}

This works perfectly. But what changes do I have to do if I want this to be done in a website?
For example I have a form, I press submit and I get this JSON. I want it to be redirect to my class so I can insert it.

Any ideas?

Thanks again
NForceNForce
HI Nicolas,
On click of submit button on the website, build the logic to send info to this endpoint.

Thanks.
NForce
Tuan LuTuan Lu
You can create an after insert/update trigger on LeadStatus to create your custom object.