How to create records from Apex Restful Service in Salesforce

By | January 17, 2022

In this blog, we will learn how to use Apex Restful service to create salesforce records. We will use Workbench to call the apex restful service to perform this procedure. 

Step 1. Firstly, Login to the Workbench by using your salesforce org. For instance, use this link to login into Workbench

Login-workbench
Login-workbench

Now, select the Environment and API version of your Org, then Click on the Login with Salesforce, as you can see in the above image. After that, you get one login screen for salesforce org. After that, just provide your Salesforce credentials and click on login as shown below image.

Salesforce-login
Salesforce-login

Now it will ask for “Allow Access”, click on “Allow”.

  • Firstly, after Login Select Utilise -> Rest Explorer.
  • Secondly, select Post and put this in the path box (/services/apexrest).
  • Further, now provide the body to create records and click on Execute button as shown in the below image.
Request for Apex Rest Service
Request for Apex Rest Service

For example, to create a record in our salesforce org, we have to handle the request with the help of the apex class, as given below.

@RestResource(urlmapping='/CreateAccountData')
global with sharing class  CreateAccountWithRest {
     @HttpPost
    global static String CreateAccount() 
    { 
        RestRequest     request    = RestContext.request;
        RestResponse    response   = RestContext.response;         
        TestAccountwrapper wrpData = new TestAccountwrapper(); 
        wrpData = (TestAccountwrapper)System.JSON.deserialize(request.requestBody.toString(), TestAccountwrapper.class); 
        System.JSONGenerator js = JSON.createGenerator(true);
        js.writeStartObject();
        try{
            Account acc = new Account();
            acc.Name = wrpData.name; 
            insert acc; 
            js.writeStringField('Status', 'Success');
            js.writeIdField('Id', acc.Id);
        }
             catch(Exception e){ 
            js.writeStringField('Status', 'Error');
            js.writeStringField('Error',e.getMessage());
            }
       	js.writeEndObject();
        	String result = js.getAsString();
        	return result; 
     } 
    global class TestAccountwrapper{
        global TestAccountwrapper(){}
    	global String Name;  
    	global Integer Phone;  
    	global String Website;    
    } 
}

As a result, the above class will get the request and create Record with the given condition. To clarify it will return the Id of the Record created, as shown in the below image.

Response-to-create-record
Response-to-create-record

Also, Check the trailhead module for REST Service. Using the details above the record is created in the salesforce org. If you still have concerns, you can write them to us, and we will reach out to you with the solution for the same.

We hope you may find this blog resourceful and helpful. If you still have concerns and need more help, please contact us at salesforce@greytrix.com.

About Us
Greytrix – a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.

Greytrix has some unique solutions for Cloud CRM such as Salesforce Sage integration for Sage X3Sage 100 and Sage 300 (Sage Accpac). We also offer best-in-class Cloud CRM Salesforce customization and development services along with services such as Salesforce Data MigrationIntegrated App developmentCustom App development and Technical Support to business partners and end users.
Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU™ integration for Sage ERP – Salesforce is a 5-star rated app listed on Salesforce AppExchange.

The GUMU™ Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.

For more information on our Salesforce products and services, contact us at salesforce@greytrix.com. We will be glad to assist you.

Related Posts