{"id":3233,"date":"2018-06-04T07:09:54","date_gmt":"2018-06-04T07:09:54","guid":{"rendered":"http:\/\/www.greytrix.com\/blogs\/salesforce\/?p=3233"},"modified":"2024-05-31T09:30:19","modified_gmt":"2024-05-31T09:30:19","slug":"creating-an-webservice-in-salesforce-and-using-rest-api-to-externally-access-it","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2018\/06\/04\/creating-an-webservice-in-salesforce-and-using-rest-api-to-externally-access-it\/","title":{"rendered":"Creating Webservices using sites in Salesforce and access as REST API"},"content":{"rendered":"<p>The Salesforce REST API is a powerful web service that lets you integrate Salesforce applications using standard HTTP methods. This can then be used to <a href=\"https:\/\/www.greytrix.com\/product\/gumu\/salesforce-sage-erp-integration\">integrate Salesforce with Sage ERP<\/a>. With help of Apex REST, we can expose the data from <a href=\"https:\/\/www.greytrix.com\/product\/crm-development\/salesforce-com\">Salesforce<\/a> orgs and we can also fetch data from other systems which hits the particular webservice(URL).Since we are exposing the data to public access with this approach so be cautious with security setups in Salesforce org.\u00a0<\/p>\n<p>We will use Salesforce Site as our webservice where most of the action will happen. For our demo purpose, we will create a Custom object named \u201cDemo\u201d. Once the object is created using the below code.<\/p>\n<div id=\"attachment_3236\" style=\"width: 806px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2018\/06\/1.-Creating-Demo-Object.jpg\"><img fetchpriority=\"high\" decoding=\"async\" aria-describedby=\"caption-attachment-3236\" class=\"size-full wp-image-3236\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2018\/06\/1.-Creating-Demo-Object.jpg\" alt=\"Creating Demo Object\" width=\"796\" height=\"329\" \/><\/a><p id=\"caption-attachment-3236\" class=\"wp-caption-text\">Creating Demo Object<\/p><\/div>\n<div id=\"attachment_3237\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2018\/06\/2.-Apex-Class-code-for-Demo-Controller.jpg\"><img decoding=\"async\" aria-describedby=\"caption-attachment-3237\" class=\"size-full wp-image-3237\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2018\/06\/2.-Apex-Class-code-for-Demo-Controller.jpg\" alt=\"Apex Class code for Demo Controller\" width=\"800\" height=\"616\" \/><\/a><p id=\"caption-attachment-3237\" class=\"wp-caption-text\">Apex Class code for Demo Controller<\/p><\/div>\n<p>Now, create a site in Salesforce, if you are new and if you need not know how to create a site in Salesforce then refer this <a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2016\/08\/18\/easily-creating-force-com-sites\/\">link<\/a>. Once your site is created in its \u201cPublic Access Settings\u201d then add the above created Class into \u201cEnabled Apex Classes\u201d and save it. Then at profile level just enable the Read, Create, Edit and Delete access to the \u201cDemo\u201d object. Now, that the configuration and coding are completed, its time to test the REST API.<\/p>\n<p>From your site section select the URL of your newly created site and append it with \u201cservices\/apexrest\/\u201d and URLMapping mentioned in the code at the end. Your URL must look something like this-<br \/>https:\/\/&lt;&gt;\/services\/apexrest\/Demo\/<\/p>\n<p>Now, let&#8217;s add a record from an external system to Salesforce using REST API, for this, we will be using Google&#8217;s ARC (Advanced REST Client). In the Header section Add content type as JSON and then click add body and use the below JSON as body and use \u201cPOST\u201d Method to post data from ARC to Salesforce.<br \/>JSON-<br \/><strong>{<br \/>\u201cName\u201d: \u201cREST Demo\u201d<br \/>}<\/strong><\/p>\n<p>Once you execute the request then you should receive status code 200 denoting success and in response, you should receive the record id of the record just created.<\/p>\n<div id=\"attachment_3235\" style=\"width: 832px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2018\/06\/3.-Response-that-record-is-created.jpg\"><img decoding=\"async\" aria-describedby=\"caption-attachment-3235\" class=\"size-full wp-image-3235\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2018\/06\/3.-Response-that-record-is-created.jpg\" alt=\"Response that record is created\" width=\"822\" height=\"559\" \/><\/a><p id=\"caption-attachment-3235\" class=\"wp-caption-text\">Response that record is created<\/p><\/div>\n<p>To verify you can simply use the \u201cGET\u201d method to fetch the data and see for yourself.<\/p>\n<p>So, in this way you can simply test the REST API from Salesforce.<\/p>\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\">\n<li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2018\/06\/04\/salesforce-platform-events-what-use\/\" target=\"_blank\" rel=\"noreferrer noopener\">Salesforce Platform Events \u2013 What are they &amp; how to use<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2018\/05\/29\/docusign-integration-with-salesforce-using-docusign-apis-part-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">DocuSign Integration with Salesforce using DocuSign API\u2019s-Part 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2018\/05\/23\/docusign-integration-with-salesforce-using-docusign-apis-part-1\/\" target=\"_blank\" rel=\"noreferrer noopener\">DocuSign Integration with Salesforce using DocuSign API\u2019s-Part 1<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2017\/10\/06\/how-to-rename-the-field-label-in-salesforce-for-managed-package\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to rename the Field label in Salesforce for managed package<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The Salesforce REST API is a powerful web service that lets you integrate Salesforce applications using standard HTTP methods. This can then be used to integrate Salesforce with Sage ERP. With help of Apex REST, we can expose the data from Salesforce orgs and we can also fetch data from other systems which hits the\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2018\/06\/04\/creating-an-webservice-in-salesforce-and-using-rest-api-to-externally-access-it\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[15,61,285,838,840,837],"class_list":["post-3233","post","type-post","status-publish","format-standard","hentry","category-salesforce-srv","tag-apex-development","tag-creating-endpoint-in-salesforce","tag-public-access-settings","tag-salesforce-remote-site","tag-webservice-method","tag-webservice-url"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/3233","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=3233"}],"version-history":[{"count":4,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/3233\/revisions"}],"predecessor-version":[{"id":8441,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/3233\/revisions\/8441"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=3233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=3233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=3233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}