{"id":6569,"date":"2022-01-17T15:31:00","date_gmt":"2022-01-17T15:31:00","guid":{"rendered":"https:\/\/www.greytrix.com\/blogs\/salesforce\/?p=6569"},"modified":"2022-01-16T15:32:17","modified_gmt":"2022-01-16T15:32:17","slug":"how-to-create-records-from-apex-restful-service-in-salesforce","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2022\/01\/17\/how-to-create-records-from-apex-restful-service-in-salesforce\/","title":{"rendered":"How to create records from Apex Restful Service in Salesforce"},"content":{"rendered":"\n<p>In this blog, we will learn how to use <strong>Apex Restful service<\/strong> to create salesforce records. We will use Workbench to call the apex restful service to perform this procedure.&nbsp;<\/p>\n\n\n\n<p>Step 1. Firstly, Login to the Workbench by using your salesforce org. For instance, use this link to <a href=\"https:\/\/workbench.developerforce.com\/login.php?startUrl=%2FrestExplorer.php\" target=\"_blank\" rel=\"noopener\">login into Workbench<\/a><\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/1.Login-workbench.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/1.Login-workbench.jpg\" alt=\"Login-workbench\"><\/a><\/center>\n<font size=\"2\"><center><i>Login-workbench<\/i><\/center><\/font>\n\n\n\n<p>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.<\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/2.Salesforce-login.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/2.Salesforce-login.jpg\" alt=\"Salesforce-login\"><\/a><\/center>\n<font size=\"2\"><center><i>Salesforce-login<\/i><\/center><\/font>\n\n\n\n<p>Now it will ask for &#8220;Allow Access&#8221;, click on &#8220;Allow&#8221;.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Firstly, after Login Select <strong>Utilise -> Rest Explorer<\/strong>.<\/li><li>Secondly, select Post and put this in the path box (<strong>\/services\/apexrest<\/strong>).<\/li><li>Further,\u00a0now provide the body to create records and click on Execute button as shown in the below image.<\/li><\/ul>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/3.Request-to-create-record.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/3.Request-to-create-record.jpg\" alt=\"Request for Apex Rest Service\"><\/a><\/center>\n<font size=\"2\"><center><i>Request for Apex Rest Service<\/i><\/center><\/font>\n\n\n\n<p>For example,\u00a0to create a record in our salesforce org, we have to handle the request with the help of the apex class, as given below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@RestResource(urlmapping='\/CreateAccountData')\r\nglobal with sharing class  CreateAccountWithRest {\r\n     @HttpPost\r\n    global static String CreateAccount() \r\n    { \r\n        RestRequest     request    = RestContext.request;\r\n        RestResponse    response   = RestContext.response;         \r\n        TestAccountwrapper wrpData = new TestAccountwrapper(); \r\n        wrpData = (TestAccountwrapper)System.JSON.deserialize(request.requestBody.toString(), TestAccountwrapper.class); \r\n        System.JSONGenerator js = JSON.createGenerator(true);\r\n        js.writeStartObject();\r\n        try{\r\n            Account acc = new Account();\r\n            acc.Name = wrpData.name; \r\n            insert acc; \r\n            js.writeStringField('Status', 'Success');\r\n            js.writeIdField('Id', acc.Id);\r\n        }\r\n             catch(Exception e){ \r\n            js.writeStringField('Status', 'Error');\r\n            js.writeStringField('Error',e.getMessage());\r\n            }\r\n       \tjs.writeEndObject();\r\n        \tString result = js.getAsString();\r\n        \treturn result; \r\n     } \r\n    global class TestAccountwrapper{\r\n        global TestAccountwrapper(){}\r\n    \tglobal String Name;  \r\n    \tglobal Integer Phone;  \r\n    \tglobal String Website;    \r\n    } \r\n}\r<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/4.Response-to-create-record.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2022\/01\/4.Response-to-create-record.jpg\" alt=\"Response-to-create-record\"><\/a><\/center>\n<font size=\"2\"><center><i>Response-to-create-record<\/i><\/center><\/font>\n\n\n\n<p>Also, Check the trailhead module for <a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/apex_integration_services\/apex_integration_webservices\" target=\"_blank\" rel=\"noopener\">REST Service.<\/a> 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.<\/p>\n\n\n\n<p>We hope you may find this blog resourceful and helpful. If you still have concerns and need more help, please contact us at&nbsp;<a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>.<\/p>\n\n\n\n<p style=\"text-align: justify\"><b>About Us<\/b><\/br>\n<p><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> \u2013 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.<br><br> Greytrix has some unique solutions for Cloud CRM such as <a href=\"\">Salesforce Sage integration<\/a> for <a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/integration\/\">Sage X3<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-100-integration\/\">Sage 100<\/a> and <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-300-integration\/\">Sage 300 (Sage Accpac)<\/a>. We also offer best-in-class Cloud CRM <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Salesforce customization and development services<\/a> along with services such as Salesforce <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/data-migration-support\/\">Data Migration<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Integrated App development<\/a>, Custom App development and Technical Support business partners and end users. Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU&#x2122; integration for Sage ERP \u2013 Salesforce is a 5-star rated app listed on <a href=\"https:\/\/appexchange.salesforce.com\/appxListingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noopener\">Salesforce AppExchange<\/a>.<br> The GUMU&#x2122; 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.<br><br> For more information on our Salesforce products and services, contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>. We will be glad to assist you.<\/p>\n\n\n\n<p><strong>Related Posts<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2022\/01\/10\/how-to-create-a-quote-template-in-salesforce\/\" data-type=\"URL\" data-id=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2022\/01\/10\/how-to-create-a-quote-template-in-salesforce\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>How To Create A Quote Template In Salesforce?<\/strong><\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/09\/22\/how-to-use-email-template-in-apex-code\/\" target=\"_blank\"><strong>How to Use Email Template in Apex Code<\/strong><\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/08\/16\/clone-record-of-any-object-using-flows-in-salesforce\/\" target=\"_blank\"><strong>Clone Record of Any Object using Flows in Salesforce<\/strong><\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/08\/14\/how-to-avoid-null-pointer-exception-by-using-safe-navigator-operator\/\" target=\"_blank\"><strong>How to Avoid Null Pointer Exception By Using Safe Navigator Operator (?.)<\/strong><\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/08\/17\/how-to-create-a-dynamic-multi-filter-object-in-salesforce-part-i\/\" target=\"_blank\"><strong>How to create a dynamic multi-filter object in Salesforce-Part I<\/strong><\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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.&nbsp; Step 1. Firstly, Login to the Workbench by using your salesforce org. For instance, use this link to login into Workbench Login-workbench Now, select the\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2022\/01\/17\/how-to-create-records-from-apex-restful-service-in-salesforce\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[13,859,14,15,18,1248,1123,1245,1246,1247,367,1244,1249,544],"class_list":["post-6569","post","type-post","status-publish","format-standard","hentry","category-salesforce-srv","tag-apex","tag-apex-class","tag-apex-classes","tag-apex-development","tag-apex-methods","tag-create","tag-record","tag-rest","tag-restful","tag-restresource","tag-salesforce","tag-service","tag-urlmapping","tag-workbench"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=6569"}],"version-history":[{"count":1,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6569\/revisions"}],"predecessor-version":[{"id":6574,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6569\/revisions\/6574"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=6569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=6569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=6569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}