on 09-30-2023 6:11 PM
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;
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();
});
});
}
})
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!")});
Is actually working, thanks for all.
Best, Santiago
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
sure this is my finally code
function sendEmail() {
}
My mistake in the end was not setting the parameters exactly as they should be.
User | Count |
---|---|
75 | |
9 | |
8 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.