<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Laarabi mustapha on Medium]]></title>
        <description><![CDATA[Stories by Laarabi mustapha on Medium]]></description>
        <link>https://medium.com/@laarabimustapha5?source=rss-c8b8692fd7dd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*JKCobNZ_OHW-3XO_</url>
            <title>Stories by Laarabi mustapha on Medium</title>
            <link>https://medium.com/@laarabimustapha5?source=rss-c8b8692fd7dd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 13:12:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@laarabimustapha5/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Making external API calls using a Webservice in SalesForce Commerce Cloud]]></title>
            <link>https://medium.com/@laarabimustapha5/making-external-api-calls-using-a-webservice-in-salesforce-commerce-cloud-d7bd998c3bc7?source=rss-c8b8692fd7dd------2</link>
            <guid isPermaLink="false">https://medium.com/p/d7bd998c3bc7</guid>
            <category><![CDATA[sales-development]]></category>
            <category><![CDATA[salesforce]]></category>
            <category><![CDATA[b2c-marketing]]></category>
            <category><![CDATA[b2c]]></category>
            <category><![CDATA[e-commerce-solution]]></category>
            <dc:creator><![CDATA[Laarabi mustapha]]></dc:creator>
            <pubDate>Thu, 16 May 2024 16:44:52 GMT</pubDate>
            <atom:updated>2024-05-16T17:18:11.524Z</atom:updated>
            <content:encoded><![CDATA[<p>Web services in <strong>S</strong>ales<strong>f</strong>orce<strong> C</strong>ommerce <strong>C</strong>loud (<strong>SFCC</strong>) serve the purpose of connecting your storefront to external systems and services, facilitating the exchange of data over the internet. They are used to establish integration with payment systems, third-party applications, or any other web-based services that support XML-based communication protocols like SOAP or HTTP.</p><p><strong>SFCC </strong>provides a web service framework that helps manage calls to these services and analyze their performance. The framework enforces limits on the number of calls allowed within a certain time interval and on the number of failed calls, which you can configure. It also collects analytics about the web service calls.</p><p>The web service framework also provides security features for communication between your storefront and an external web service. At the transport layer, you can use certificates to identify both parties of a transaction. At the application layer, you can encrypt or sign content with another certificate.</p><p>In addition, the web service framework in SFCC includes a rate limiter and a circuit breaker. The rate limiter allows a maximum number of calls to a web service in a specified time interval. If the rate limit is reached, a <strong>ServiceUnavailableException </strong>is thrown. The circuit breaker, on the other hand, suspends calls to a web service if a certain number of calls fail within a specified time interval.</p><p>The example we will implement a webservice that will return the details of a country based on its ISO2 code. We will use the<strong> </strong><a href="https://restcountries.com/v3.1/alpha/"><strong>Rest Countries API</strong></a>, from which we will retrieve the country’s information as a JSON object. Then we will utilize this JSON to extract and display the specific information we require.</p><ul><li><strong>Step 1: Create the webservice on the Business Manager.</strong></li><li><strong>Step 2: Create the webservice’s Script.</strong></li><li><strong>Step 3: Create the form definition and form template that will take the country ISO Code.</strong></li><li><strong>Step 4: Create the controller, for the form and the result show handling.</strong></li><li><strong>Step 5: Create the Country Info display template.</strong></li></ul><h3>Step 1 : Create the webservice on the Business Manager</h3><p>In order to create the service in the Business Manager, you must follow each of these 3 actions:</p><h4>1- Create Credentials for the Webservice</h4><p>The web service credentials are used for basic authentication. It is a good practice to use a descriptive name.</p><p>We will use the <strong>http.RefArch.CountryInfo.cre</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y6cj5tffuuvYeFI3Omy9_g.png" /></figure><h4>2- Create the webservice profile</h4><p>The web service profile outlines how B2C Commerce identifies and handles a large volume of requests to and responses from the service, as well as its accessibility. Multiple services with comparable needs can utilize the identical service profile.</p><p>The same practice is recommended for naming the webservice profile, we will then choose the name</p><p><strong>http.RefArch.CountryInfo.profile</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RSiBlHBQMERuM-3lh7idKA.png" /></figure><h4>3- Create the webservice</h4><p>After we set the profile and credentials, we use them to create the webservice.</p><p>We use this pattern to name our webservices : <strong>cartridge.protocol.service.operation</strong></p><p><strong>app_myproject.http.country_info_service.get</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XkxEALN2u94I5Iwxr7slOw.png" /></figure><h3>Step 2: Create the webservice’s Script</h3><p>In order to create our webservice instance, we us the <strong>createService </strong>method from the <a href="https://salesforcecommercecloud.github.io/b2c-dev-doc/docs/current/scriptapi/html/index.html?target=class_dw_svc_LocalServiceRegistry.html"><strong>LocalServiceRegistry</strong></a> Class within the SFCC scripting API. This method takes as parameters the <strong>serviceId</strong> and the configuration object.</p><p>In the configuration object we should define the <strong>createRequest</strong>, <strong>parseResponse </strong>and <strong>mockExec</strong> functions. When our service is called using the call method, it performs the operations as defined in the Script and webservice configurations.</p><p>This will allow us to:</p><ul><li>Get service objects from the registry.</li><li>Provide input parameters for the <strong>createRequest</strong> callback used to construct the URL to call the web service.</li><li>Provide error handling based on the result of the web service call.</li><li>Handle the output of the service and prepare it for rendering.</li></ul><p>All scripts must be in a folder named <strong>scripts </strong>inside your cartridge.</p><p>In our example, we call the function “<strong>CountryInfoService</strong>”, and the file “<strong>CountryInfoService.js</strong>”:</p><pre>importPackage(dw.system)<br>importPackage(dw.svc)<br>importPackage(dw.net)<br>importPackage(dw.io)<br>function CountryInfoService() {<br>    var getCountryService = LocalServiceRegistry.createService(<br>        &quot;app_myproject.http.country_info.get&quot;,<br>        {   createRequest: function(svc) {<br>            svc.setRequestMethod(&quot;GET&quot;);<br>            },<br>            parseResponse: function(svc, output) {<br>                return output.text;<br>            },<br>            mockCall: function(svc, param) {<br>                return {<br>                    statusCode: 200,<br>                    statusMessage: &quot;Successfully&quot;,<br>                    text: &quot;MOCK RESPONSE (&quot; + svc.URL + &quot;)&quot;<br>                };<br>            }<br>        }<br>    );<br>    return getCountryService;<br>}<br>module.exports = CountryInfoService;</pre><p>Function in charge of sending the external service an <strong>HTTP GET request</strong>.</p><h3>Step 3: Create the form definition and Template</h3><p>Create a folder called Forms inside your cartridge, where all of your XML-formatted forms should be. Create a folder for the language now; if you’re using English, call it default. Next, create your form within the default folder, from where we’ll get the SO2 code.</p><h4>1- Create form definition:</h4><p>The form definition is an XML metadata file located in the <strong><em>cartridge/forms/default </em></strong>directory. It describes the fields, labels, validation rules and actions that apply when the field is used in an <strong>ISML </strong>template.</p><p>For our example, we’ll call it CountryInfoForm.xml:</p><pre>&lt;?xml version=&quot;1.0&quot;?&gt;<br>&lt;form xmlns=&quot;http://www.demandware.com/xml/form/2008-04-19&quot;&gt;<br> &lt;field formid=&quot;CountryInfoForm&quot; label=&quot;Enter the country code:&quot; type=&quot;string&quot; mandatory=&quot;true&quot; max-length=&quot;50&quot; /&gt;<br> &lt;action formid=&quot;submit&quot; valid-form=&quot;true&quot;/&gt;<br> &lt;action formid=&quot;cancel&quot; valid-form=&quot;false&quot;/&gt;<br>&lt;/form&gt;</pre><p><a href="https://developer.salesforce.com/docs/commerce/b2c-commerce/guide/b2c-forms.html"><strong><em>USE THIS URL FOR MORE INFORMATIONS ABOUT THE FORMS</em></strong></a></p><h4>2- Create form template:</h4><p>The form template is what renders the form that will receive the country code and transmit it to our webservice. Its name is <strong>CountryInfoFormTemplate.isml.</strong> It is put in the folder<strong> </strong>template/default/CountryInfo<strong>.</strong></p><pre>&lt;isdecorate template=&quot;common/layout/page&quot;&gt;<br> &lt;div class=&quot;hero slant-down login-banner&quot;&gt;<br>  &lt;h1&gt;Country Information Fetcher&lt;/h1&gt;<br> &lt;/div&gt;<br> &lt;div class=&quot;card&quot;&gt;<br>  &lt;form <br>  action=&quot;${pdict.actionUrl}&quot; <br>  class=&quot;login&quot; <br>  method=&quot;post&quot; <br>  name=${pdict.CountryInfoForm.htmlName}&gt;<br>   &lt;div class=&quot;form-group&quot;&gt;<br>    &lt;label&gt; Enter your country code : &lt;/label&gt; <br>    &lt;input type=&quot;input&quot; id=&quot;country_code&quot; class=&quot;form-control&quot; name=&quot;country_code&quot;/&gt;<br>    &lt;input type=&quot;hidden&quot; name=&quot;${pdict.csrf.tokenName}&quot; value=&quot;${pdict.csrf.token}&quot;/&gt;<br>   &lt;/div&gt;<br>   &lt;button type=&quot;submit&quot; class=&quot;btn btn-block btn-primary&quot;&gt;Submit&lt;/button&gt;<br>  &lt;/form&gt;<br> &lt;/div&gt;<br>&lt;/isdecorate&gt;</pre><h3>Step 4: Create the controller, for the form and the result show handling.</h3><p>In the folder named <strong>controllers </strong>inside our cartridge, we create our controller and name it CountryInfo.js, it will handle both the form and results.</p><pre>&#39;use strict&#39;;<br>var server = require(&#39;server&#39;);<br>var URLUtils = require(&#39;dw/web/URLUtils&#39;);<br>var csrfProtection = require(&#39;*/cartridge/scripts/middleware/csrf&#39;);<br><br>server.get(<br>  &quot;Form&quot;,<br>  server.middleware.https,<br>  csrfProtection.generateToken,<br>  function (req, res, next) {<br>    var actionUrl = URLUtils.url(https://medium.com/&#39;CountryInfo-Show&#39;); <br>    var CountryInfoForm = server.forms.getForm(&#39;CountryInfoForm&#39;); <br>    CountryInfoForm.clear();<br>    res.render(<br>      &#39;CountryInfo/CountryInfoFormTemplate&#39;, <br>      {<br>        actionUrl: actionUrl, <br>        CountryInfoForm: CountryInfoForm<br>      });<br>    next();<br>  });<br><br>server.post(<br>  &#39;Show&#39;,<br>  server.middleware.https,<br>  csrfProtection.validateAjaxRequest,<br>  function (req, res, next) {<br>    var mycountry = req.form.country_code;<br>    var Countryservice = require(&#39;../scripts/CountryInfoService.js&#39;);<br>    var myservice = Countryservice();<br>    myservice.URL += &#39;/&#39; + mycountry;<br>    var data = myservice.call();<br>    data = JSON.parse(data.object);<br>      res.render(<br>        &#39;CountryInfo/CountryInfoResultTemplate&#39;, <br>        { name: data[0].name.common,<br>          currency: Object.values(data[0].currencies)[0].name,<br>          language: Object.values(data[0].languages)[0],<br>          capital: Object.values(data[0].capital)[0],<br>          region: data[0].region<br>        });<br>      return next();  <br>    }<br>);<br>module.exports = server.exports();</pre><p>The first function call in our controller <strong>server. Get </strong>is responsible for :</p><ul><li>Setting up the initial route (Form) when a user accesses a specific URL, typically for selecting a country.</li><li>Generates a CSRF token for security.</li><li>Clears a form related to country selection.</li><li>Renders an HTML template to display the form and process user input for selecting a country.</li></ul><p>The second method call <strong>server.post</strong> is responsible for:</p><ul><li>Handling the data sent via a POST request to the ‘Show’ route.</li><li>Before processing the request, it ensures two security measures: Check that the request is over HTTPS. Performs CSRF (Cross-Site Request Forgery) protection.</li><li>Extracting data from the submitted form. Specifically, it looks for a field named ‘country_code’ in the form data</li><li>Contacting the external service to fetch information about a country ‘<strong>CountryInfoService</strong>’ .</li><li>Constructing the URL for the external service using the country name obtained from the form data.</li><li>Receiving a response from the external service, which contains information about the requested country.</li><li>Parsing the response data, presumably in JSON format.</li><li>Rendering a template ‘<strong>CountryInfoResultTemplate</strong>’ with information obtained from the external service. This rendered template displays details such as the country name, currency, and language.</li></ul><h3>Step 5: Create the Country Info display template.</h3><p>In the same folder with the form template, we create the result template to display the country information “<strong>CountryInfoResultTemplate.isml</strong>“:</p><pre>&lt;isdecorate template=&quot;common/layout/page&quot;&gt;<br>    &lt;div class=&quot;learning&quot; style=&quot;margin-left: 70px;&quot;&gt;<br>        &lt;p style=&quot;font-size: 30px; font-weight: bold;&quot;&gt;${pdict.name}&lt;/p&gt;<br>        &lt;p style=&quot;margin-left: 20px;&quot;&gt;- Currency: ${pdict.currency}&lt;/p&gt;<br>        &lt;p style=&quot;margin-left: 20px;&quot;&gt;- Language: ${pdict.language}&lt;/p&gt;<br>        &lt;p style=&quot;margin-left: 20px;&quot;&gt;- Capital: ${pdict.capital}&lt;/p&gt;<br>        &lt;p style=&quot;margin-left: 20px;&quot;&gt;- Region: ${pdict.region}&lt;/p&gt;<br>    &lt;/div&gt;<br>&lt;/isdecorate&gt;</pre><h3>Final Step: Testing the webservice</h3><blockquote>The URL used to access the form of the webservice has the following pattern : <a href="https://instance_name.dx.commercecloud.salesforce.com/on/demandware.store/Sites-YOURSITE-Site/en_US/NAME_OF_CONTROLLER-ROUTE_NAME"><strong>https://INSTANCE_NAME.dx.commercecloud.salesforce.com/on/demandware.store/Sites-YOURSITE-Site/en_US/NAME_OF_CONTROLLER-ROUTE_NAME</strong></a><br><strong>INSTANCE_NAME</strong>: Your instance name<br><strong>YOURSITE</strong>: The specific site<br><strong>NAME_OF_CONTROLER</strong>: The controller name ‘<strong>CountryInfo</strong>’<br><strong>ROUTE_NAME</strong>: The name of the route ‘<strong>Form</strong>’</blockquote><p><a href="https://instance_name.dx.commercecloud.salesforce.com/on/demandware.store/Sites-RefArch-Site/en_US/CountryInfo-Form"><strong>https://INSTANCE_NAME.dx.commercecloud.salesforce.com/on/demandware.store/Sites-RefArch-Site/en_US/CountryInfo-Form</strong></a></p><p>The provided URL will direct you to the form interface, where you can input the desired country ISO code and initiate the service :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WlpJMp7ON2AhYZGfexZEwA.png" /></figure><p>We utilized the country <strong>ISO </strong>code ‘<strong>MA</strong>’ and obtained the subsequent outcome:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/284/1*Wo1e-5YExlWiXGZwvRutpA.png" /></figure><blockquote>By following these steps, you can seamlessly integrate <strong>external APIs</strong> into your <strong>Salesforce Commerce Cloud storefront</strong>, expanding its functionality and enhancing user experience. The example provided demonstrates the process from setting up web services to displaying fetched data. Now, with a solid understanding of how to make external API calls using a webservice in Salesforce Commerce Cloud, you’re well-equipped to tackle more complex integrations and customization within your e-commerce platform. <strong>Happy coding!</strong></blockquote><p><a href="https://medium.com/@firarianas/consuming-the-open-commerce-api-from-salesforce-commerce-cloud-4c31e1b5e4a6">Want to expand your Salesforce Commerce Cloud skills? Check out ‘Consuming the Open Commerce API’ for more insights!</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d7bd998c3bc7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>