sexta-feira, 1 de novembro de 2019

WhatsApp Integration with SAP S/4HANA Cloud

Hello Everyone,
Welcome to my another interesting Blog “WhatsApp Integration with SAP S/4HANA Cloud” Well this blog i am writing to clear the confusion many people have in previous blog. This blog is going to be pure technical and i will try to explain the differences in terms of setting up for SAP S/4HANA Cloud and SAP S/4HANA On-Premise.

What is the difference between this blog and previous blog?
Well this is valid question for all the readers because why you would spend some time on this where you have already spent time in previous blogThere is a  difference and the difference is it is SAP S/4HANA cloud and here we don’t expose Odata service for purchase requisition and purchase order like we do in SAP S/4HANA On-Premise system. This blog is also going to clear lots of doubts of readers. I hope you would enjoy this.
Those people who are very new to SAP S/4HANA Cloud, Might have started asking many question to themselves, normally when i started my journey in SAP S/4HANA Cloud i had also many question when it comes to Integrating some thing with SAP S/4HANA Cloud. Most of the questions are
As this blog i am writing to clear most of the doubt, so normally questions are relevant to Integrating ‘SAP S/4HANA Cloud with WhatsApp’. lets Answer one by one.
Is there any standard api’ s available in SAP S/4HANA Cloud?
Yes there are N-number of standard API’s available for SAP S/4HANA Cloud, all you need to is go to SAP API Hub and search with SAP S/4HANA Cloud. You will get well documented swagger based api documentation with how to consume. If your use case falls under any of those standard api available, you can directly consume them as per your requirement.
Where do i start?
Well i assume you already have gone through the previous blog and you are interested to know what are the integration options are available for SAP S/4HANA Cloud, Then you must go through this blog.
How i will create custom  OData service  in S/4HANA Cloud?
Should i go with Extensibility?
Its always depends case to case, if your requirement is very standard and for those there are standard API’s are available in SAP API Hub, It’s always better to go with API no extensibility is required but if your requirement you are not able to fulfill with standard API then you definitely approach for different extensibility options available (Custom CDS Views, S/4HANA Cloud SDK etc.).
Please Have a look at Extensibility Explorer for more information.
Custom SAP S/4HANA Cloud API?
You can build custom java or node.js rest API using SAP S/4HANA Cloud SDK. Please have a look at official tutorial for SAP S/4HANA Cloud SDK.

Do I need SAP Cloud Connector for this?

No you don’t need SAP Cloud Connector if you are using SAP S/4HANA Public Cloud.

Then how the Technical Architecture will be looking like, What are the component we will be using for SAP S/4HANA Cloud.

Almost Everything same the difference is SAP S/4HANA Part and you can see Cloud Connector and API Management is not required.
So we will focus on how we are going to create OData service in SAP S/4HANA Cloud and consume it in Node.js Application running on SAP Cloud Platform. And rest of the steps are same as previous blog. Below is high lever overview how we can expose custom cds view based odata service and then consume in SAP Cloud Platform Node.js Application.

Like previous blog we will break this whole integration into small pieces.

Step 1. Identify CDS Views and Create Custom CDS Views
Go to Extensibility Group in Fiori launchpad and Click on Custom CDS View Tiles
Click on Create
Give Name, Label, check on external API then click on Add Primary Data Source
Now identify the CDS view and add it for field selection
Select the Fields you want to display
Now Save Draft and Publish the CDS Views.
Similarly Perform for Purchase order, Finally both should be Published and look like below.

Step 2: Create Custom Communication Scenario and Arrangement
Now go back to Fiori Launchpad and Click on Custom Communication Scenario
Click on New and Add the Communication Scenario Name
Now Add two CDS views we have created in Step 1 as Inbound Service and Publish
Now go to Communication management group and click on maintain communication user tiles to maintain Inbound communication user
Once Communication user is maintained, now create communication system with S/4HANA Cloud host (Remember it is own system). 
Now Assign the communication user as inbound user which we have created in last step
Now maintain Communication Arrangement
Assign the communication scenario and communication system which we have created in earlier step. Save it. Now your custom OData service is ready.
Step 3: Designing the skill of chatbot in SAP Cai. Check this amazing tutorial
Step 4. Creating  node.js app which will be interacting with S/4HANA Cloud based Custom Odata Service. Sorry for publishing only Purchase Requisition Status function, i wanted my reader to try something by themselves 😛
  app.post('/PrStatus', (req,res) => {
    const PurchaseReqNo = req.body.PurchaseReqNo;
    var demourl = "https://myXXXXXX-api.s4hana.ondemand.com/sap/opu/odata/sap/YY1_PURCHASEREQSTATUS_CDS/YY1_purchaseReqStatus(PurchaseRequisition="+"'"+PurchaseReqNo+"'"+",PurchaseRequisitionItem='00010')?$format=json";
var headers = {
       'Authorization':    'Basic QVYYQkE6UzRoYJU965450ZWdyYXRpb24xMiE=',
       'Content-Type':     'application/json',
       'Accept': 'application/json'
   };
   var options = { method: 'GET',
     url: demourl,
     headers: headers 
      };
      request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            // Print out the response body
            console.log(body)
            var resultData = JSON.parse(body);
            var prStatus;
            switch(resultData.d.ProcessingStatus) {
              case 'N':
                prStatus = 'Not Edited'
                break;
              case 'B':
                prStatus = 'PO Created'
                break;
              case 'A':
                prStatus = 'RFQ Created'
                break;
              case 'K':
                prStatus = 'Contract Created'
                break;
              case 'L':
                prStatus = 'Scheduling Agreement Created'
                  break;
              case 'S':
                prStatus = 'Service Entry Sheet Created'
                  break;
              case 'D':
                prStatus = 'Deployment STR'
                  break;
              case 'E':
                prStatus = 'RFQ sent to external system'
                  break;
              default:
                prStatus = 'undefined'
            }
             if(resultData.d.PurchaseRequisition !== ''){
               var MaterialDesc= resultData.d.PurchaseRequisitionItemText;
                var PoNumber = resultData.d.PurchasingDocument;
                old_content = 'Current Purchase Requisition Status is:'+' '+prStatus+ ' '+'for '+MaterialDesc+' and PO number is '+' '+PoNumber;
                var content_txt = old_content
                  res.send({
          replies: [{
            type: 'text',
            content: content_txt,
          }], 
          conversation: {
            // memory: { key: 'value' }
          }
        })
            }else {
                res.send({
                      replies: [{
                        type: 'text',
                        content: 'Invalid Requisition number',
                      }], 
                      conversation: {
                        // memory: { key: 'value' }
                      }
                    })
            }
          } else {
            res.send('Error happened')
        }
    })
})
Step 5. Deploying the node.js app we created in last step into SAP Cloud Platform CF account.
Step 6. Use the deployed application URL with proper path as web hook of skill in SAP CAI.
Step 7. Test the bot using SAP CAI.
for 5, 6, 7 check my previous blog.
Step 8. Create a free account in twilio.
Step 9. Enable the Whatsapp channel.
Step 10. Create a Twilio function to interact with SAP CAI using SAP CAI SDK.
Step 11.Deploy the function and use function Url as Webhook of WhatsApp channel.
for 8, 9, 10, 11 Please follow the 8 9 10 11 step of previous blog.
Now You Can Test it . I have Attached the working Video. If you like this blog please like comment and share which will definitely help me to motivate writing more blogs.
Live Demo

Regards,
Sudip

4 comentários: