cancel
Showing results for 
Search instead for 
Did you mean: 

Call a function from my cap v4 backend from my sapui5 application

Thiagoc789
Participant
0 Kudos

Hello experts, I am about to achieve my goal of sending an email from my sapui5 application, using the url

https://port4004-workspaces-ws-sws9w.us10.applicationstudio.cloud.sap/my-evaluations/sendmail(sender='evaluaciones@isc.com.ec',to='zantiago_estudia@hotmail.com',subject='Asunto',body='cuerpo')

I am managing to send the email, the problem is that it was left in the odata v4 and in my sapui5 application I am using v2, I already have the v4 model in my application but since v4 I do not know how to call this function since it does not support callFunction, how could I call this function? Or would it be better to convert it so I can use my function from v2. Thank you so much

Schema_srv.js

const cds = require("@sap/cds");const nodemailer = require("nodemailer");<br>module.exports = cds.service.impl(async function () { this.on("sendmail", async (req) => { if (!req.data.sender) { return req.error("You must specify a sender"); } if (!req.data.to) { return req.error("You must specify a recipient"); } if (!req.data.subject) { return req.error("You must specify a subject"); } if (!req.data.body) { return req.error("You must specify a body"); }<br>try { const transporter = nodemailer.createTransport({ host: "smtp.office365.com", port: 587, secure: false, auth: {....} }); // use sendmail as you should use it in nodemailer const result = await transporter.sendMail({ from: req.data.sender, to: req.data.to, subject: req.data.subject, text: req.data.body, }); return result; } catch (error) { //LOG.info(error); return req.error(error); } });})


Schema.cds

using {app.evaluator as my} from '../db/schema';<br>service MyEvaluationsService { entity Season as projection on my.Season; entity Evaluation as projection on my.Evaluation; entity EvaluationFormat as projection on my.EvaluationFormat; entity EvaluationRecord as projection on my.EvaluationRecord; entity Attendances as projection on my.Attendance; entity User as projection on my.User; entity Signatures as projection on my.Signature; entity Empleado as projection on my.Empleado; entity Cargo as projection on my.Cargo; entity Dependencias as projection on my.Dependencias; entity Career_Plan as projection on my.Career_Plan; entity Preparacion as projection on my.Preparacion; entity Empleados_Preparacion as projection on my.Empleados_Preparacion; function sendmail(sender : String, to : String, subject : String, body : String) returns String;<br>}


server.js

const cds = require("@sap/cds");const cov2ap = require("@sap/cds-odata-v2-adapter-proxy");<br>cds.on("bootstrap", (app) => app.use(cov2ap()));<br>module.exports = cds.server;
View Entire Topic
martinfrick
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi thiagoc789,

should look similar to the following in OData V4. Actually based on a great sample that was posted on Stack Overflow (see here). Worked for me!

Best,

Martin

return Controller.extend("sap.sample.ui.controller.SampleView", {
    onPressSendMail : function() {
return new Promise((resolve, reject) => { const sender = this.getView().byId('sender').getSelectedItem().getKey(); const oActionODataContextBinding = this.oDataModel.bindContext("/sendmail(…)"); oActionODataContextBinding.setParameter("sender", sender); oActionODataContextBinding.execute().then(()=> { const oActionContext = oActionODataContextBinding.getBoundContext(); console.log(oActionContext.getObject()); MessageToast.show("Email successfully sent!")
resolve(); });
}); } })
Thiagoc789
Participant
0 Kudos

I'm currently trying it this way but I get this error

var oDataModel = that.getView().getModel("my-evaluations") console.log(oDataModel) const oActionODataContextBinding = oDataModel.bindContext("/sendmail(…)"); console.log(oActionODataContextBinding) oActionODataContextBinding.setParameter("sender", " evaluaciones@isc.com.ec") oActionODataContextBinding.setParameter("to", " zantiago_estudia@hotmail.com") oActionODataContextBinding.setParameter("subject", "Asunto") oActionODataContextBinding.setParameter("body ", "body")
oActionODataContextBinding.execute().then(() => { const oActionContext = oActionODataContextBinding.getBoundContext(); console.log(oActionContext.getObject()); sap.MessageToast.show("Email successfully sent!")});
Thiagoc789
Participant

Is actually working, thanks for all.

Best, Santiago

martinfrick
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi thiagoc789,

It's quite the coincidence that I found myself needing the same code snippet yesterday afternoon for a different project, and I encountered the exact same error (binding must be deferred). I would greatly appreciate any insights you might have on how you managed to resolve it. Unfortunately, I haven't had the opportunity to delve deeper into the issue just yet, but I suspect it could be tied to the model configuration. I'm confident that sharing your solution could be beneficial to others who come across the same predicament.

Best regards and thanks in advance,

Martin

Thiagoc789
Participant

sure this is my finally code
function sendEmail() {

return new Promise((resolve, reject) => { var oSelectedItem2 = that.getView().byId("careerPlanComboBox").getSelectedItem(); var oSelectedObject2 = oSelectedItem2.getBindingContext().getObject(); var careerPlan = oSelectedObject2.Description var nombre = "Evaluacion " + oSelectedObject.description+" "+sEmployeeCedula+" "+sEmployeeName+" "+sEmployeeLastName+".pdf" var body = "Adjunto se encuentra su Evaluación de Desempeño de la Temporada "+ oSelectedObject.description+" en el Plan de Carrera " + careerPlan var body2 = "Le recordamos que estos correos son únicamente informativos, favor no responder." var body3 = "Saludos" var bodyText = body + "\n\n" + body2 + "\n\n" + body3; const oActionODataContextBinding = oDataModel.bindContext("/sendmail(...)"); oActionODataContextBinding.setParameter("sender", "evaluaciones@isc.com.ec") oActionODataContextBinding.setParameter("to", 'zantiago_estudia@hotmail.com') //CAMBIAR A empleadoCorreo cuando se finalicen pruebas oActionODataContextBinding.setParameter("subject", "Resultado de la Evaluación de Desempeño") oActionODataContextBinding.setParameter("body", bodyText) oActionODataContextBinding.setParameter("adj", base64Only) oActionODataContextBinding.setParameter("nombre", nombre)
oActionODataContextBinding.execute().then(function () { resolve(); }).catch(function (error) { reject(new Error("Error al enviar el email.")); }); });

}

My mistake in the end was not setting the parameters exactly as they should be.