Lately I've been following several threads about SAP Cloud Platform Integration (CPI). The information I have gained from following these threads has helped me to improve my skills when working on SAP projects, and it has helped me develop solutions that I have regularly been able to use. I believe that this exchange of information is what makes this SAP community a valuable repository of knowledge and useful solutions to be shared among its members.
However, I have noticed very few posts that discuss JavaScript. In the
2021 Stack Overflow Developer Survey, JavaScript was found to be the most commonly used programming language, with nearly 65% of respondents using JavaScript, compared to around 35% of respondents using Java. Unsurprisingly, the number of articles and questions about JavaScript, and the number of code examples using JavaScript, is almost double those referring to Java. Perhaps one explanation for the considerable difference in the number of users of JavaScript and Java is the large number of frameworks released in recent years such as:
jQuery,
Angular,
EmberJs, VueJs,
Reactjs, and, of course, the well-known (notorious)
NodeJs. All these tools use JavaScript as a base, and this has certainly contributed to the popularity of JavaScript.
Stackoverflow survey 2021 of the most popular technologies programming scripting and markup languages Font: https://insights.stackoverflow.com/survey/2021#section-most-popular-technologies-programming-scripti...
So why is it that, even though JavaScript is more commonly used than Java, most SAP CPI developers seem to prefer to use Groovy script, the Java-like syntax language built for the Java platform? Undoubtedly, Groovy script is a good option, and is rich in resources for use in SAP CPI. However, I keep asking myself why developers use Groovy as a default. Why do they rarely use JavaScript in projects, or even share articles presenting solutions that use this fantastic language? In order to understand this, I had some discussions with fellow developers
Jairo Canuto,
daniel.graversen,
mlonghi-altea and
Fatih Pense, all of whom have vast experience and domain knowledge on the subject. I asked why Groovy is more popular in the SAP CPI community.
According to Mr
Jairo Canuto:
- Many programmers originate from Java.
- Around 2004 when the SAP Netweaver product was released, many Java developers started migrating to SAP for the purpose of to develop Webdynpro Java and also for integration with SAP Exchange Infrastructure - XI 2.0.
- This, in turn, uses the Eclipse IDE.
Given Groovy is a Java-like syntax language and, as Mr Canuto explained, many SAP developers were originally Java developers, this explains in part why Groovy is popular with SAP CPI developers.
Mr
daniel.graversen pointed out there a number of reasons that might explain the lack of popularity of Javascript for CPI:
- People coming from a small Java background do not need to change the way they have been coding.
- There are more libraries that can be added to the function.
- Most examples and standard content are in Java (a kind of Catch 22).
- It could be faster since it is more native to the CPI runtime.
- Uncertainty about how long JavaScript will be supported.
- The availability of developers who can support it.
According to Mr
mlonghi-altea, an Italian developer:
- Groovy is more popular because it's faster, it's lighter and allows you to do more. By this he means you can write yourself an external .jar library and import it.
- Groovy is very versatile, you can use it to edit the payload as you like and also access log and CPI core instructions.
Indeed, the option to import Jar libs is a great facility. Also, the online tool is such a reliable resource, as it allows us to run tests on
Groovy.
Mr
Fatih Pense, said a number of reasons come to mind:
- SAP integration people are already familiar with Java. SAP PI/PO, which is still widely used, needs you to write Java mappings/functions from time to time.
- CPI itself is based on a Java framework.
- Working with XML is one of the powerful sides of Groovy (I was skeptical at first).
- Javascript works fine as a language (it might be missing new language changes, but it evolves fast), but you can't use every package in NPM. This is not Node.js the runtime, it is just JS the language support.
Personally, I believe that another reason why JavaScript is not more popular with SAP CPI developers is the version of JavaScript supported by SAP CPI. Unfortunately, the most recent version of JavaScript that can be used in SAP CPI does not allow you to use modern standards, such as those in
ECMA Script 6. This is not necessarily a big drawback, but if JavaScript ES6 were supported it would provide a number of enhancements that would make coding JavaScript simpler, including Arrow functions and improved array processing using methods such as Array.find() and Array.findIndex().
Have a look at the example below:
const nameStudents = [
{name: 'Grant', schoolId: 1, country: "Br", score: 10, age: 26},
{name: 'Tulio', schoolId: 3, country: "BR", score: 25, age: 22},
{name: 'Paolo', schoolId: 2, country: "IT", score: 48, age: 55}]
const schools = [
{id: 1, institution: 'IBM', active: true, since: 1950},
{id: 2, institution: 'SAP', active: true, since: 1950},
{id: 3, institution: 'TechM', active: false, since: 1950}]
//Return the SUM of students where the scores is an integer division or Age >=25
// -> From the result above, select where school is active
// -> double the score
// -> print SUM the values and students list
//old fashioned
var sum = 0;
var listAproval = [];
var output = {}
var listAprovalSchool = [];
for (var i = 0; i < nameStudents.length; i++) {
if (nameStudents[i].score % 2 === 0 || nameStudents[i].age >= 25) {
for (var j = 0; j < schools.length; j++) {
if (schools[j].id === nameStudents[i].schoolId && schools[j].active === true) {
sum = sum + nameStudents[i].score * 2;
listAproval.push({
'fullname': `${nameStudents[i].name} - ${nameStudents[i].country}`,
'yearBirthday': new Date().getFullYear() - nameStudents[i].age
})
}
}
}
}
output['sum'] = sum
output['studentList'] = listAproval
console.log(output)
//Using ECMAScript 2015
let students = nameStudents.filter(({score, age}) => score % 2 === 0 || age >= 25)
.map(({name, country, age, schoolId, score}) => {
let school = schools.find(({id, active}) => id === schoolId && active === true);
if (!school) return null;
return {
fullname: `${name} - ${country}`,
yearBirthday: new Date().getFullYear() - age,
score
};
}).filter(Boolean);
let sum = students.reduce((total, {score}) => total + score * 2, 0);
let output = {
sum,
studentList: students
};
console.log(output);
From a personal point of view, I believe that SAP updates for the SAP BTP product will soon appear, and more modern JavaScript version standards, especially those defined by the
TC39 Committee, will be implemented, as this organization releases the latest updates regarding JS annually.
Finally, the vast majority of interfaces developed with SAP CPI follow the SOAP standard, which favors the use of Groovy, since it brings a certain ease of processing XML through its parse patterns. However, we also cannot fail to notice that many of the non-SAP interfaces use the JSON standard, an acronym for JavaScript Object Notation, in which JS proves to be a more favorable and advantageous language to use.
My intention in writing this article is only to start a discussion about the use of Groovy and JavaScript in SAP CPI. It is not my intention to make performance comparisons between the two, as both languages have positives and negatives. However, it would be interesting to start seeing new articles about JavaScript.
So, please join the discussion by leaving your comments about your experiences using JavaScript or Groovy with SAP CPI, which script you prefer, and why. Hopefully, by promoting such a discussion, we can learn from each other and expand our knowledge of both JavaScript and Groovy.