@pjcaracuel_2349 wrote:
Hi.
I am very sorry, I have not posted for a long time and I have forgotten the good practices.
Your code has been very useful to me but the error persists.
It is strange but if I run it in the console, only javascript works correctly however when the script is run it doesn't.Script code:
addWaterMark.js
var userlogged = userhome.properties.name; try { var pdfDocNode = document; ver step = "100!; logger.system.out("PDFDocNodeRef: "+ pdfDocNode.nodeRef+" | PDFDocName: "+pdfDocNode.name); logger.system.out("LoggedInUser: "+ userlogged); //Assuming you have an image named after the logged in user somewhere in repo. e.g.: 'admin.jpg' var query = 'TYPE:\"cm:content\" AND @cm\\:name:"'+ userlogged + '.png"'; //get the image exactly with matching name var searchQuery = { query: query, language: "fts-alfresco" }; logger.system.out("Executing SearchQuery: " + query); var imagenodes = search.query(searchQuery); var imagenodeForWatermarking = imagenodes[0]; logger.system.out("ImageNodeRef: "+ imagenodeForWatermarking.nodeRef+" | ImageName: "+imagenodeForWatermarking.properties["cm:name"]); //Inserta el gráfico en el pedido en función del firmante al que corresponda var watermark_action = actions.create("pdf-watermark"); watermark_action.parameters.inplace = true; //watermark_action.parameters["destination-folder"] = doc.parent; watermark_action.parameters["watermark-type"] = "image"; //watermark_action.parameters["watermark-image"] = eval(nodes[0]); watermark_action.parameters["watermark-image"] = imagenodeForWatermarking.nodeRef; //watermark_action.parameters["watermark-text"] = "Lorem Ipsum"; //watermark_action.parameters["watermark-font"] = "Helvetica"; //watermark_action.parameters["watermark-size"] = "12"; watermark_action.parameters.page = "all"; watermark_action.parameters["watermark-depth"] = "over"; watermark_action.parameters.position = "manual"; watermark_action.parameters["location-x"] = step; watermark_action.parameters["location-y"] ="1"; watermark_action.execute(pdfDocNode); pdfDocNode.save(); logger.system.out("***************************Incluida firma en : " + pdfDocNode.name + " " + step); } catch (ex) { logger.system.out("Exception occurred: " + ex.message); }Log Error:
PDFDocNodeRef: workspace://SpacesStore/fa7e24d6-d178-4bea-b814-fdf661f4f9fe | PDFDocName: 5766774840.pdf LoggedInUser: rcanovas Executing SearchQuery: TYPE:"cm:content" AND @cm\:name:"rcanovas.png" Exception occurred: Cannot read property "nodeRef" from undefinedJavascript Console Code:
var userlogged = "rcanovas"; try { var pdfDocNode = document; var step = "100"; logger.log("PDFDocNodeRef: "+ pdfDocNode.nodeRef+" | PDFDocName: "+pdfDocNode.name); logger.log("LoggedInUser: "+ userlogged); //Assuming you have an image named after the logged in user somewhere in repo. e.g.: 'admin.jpg' var query = 'TYPE:\"cm:content\" AND @cm\\:name:"'+ userlogged + '.png"'; //get the image exactly with matching name var searchQuery = { query: query, language: "fts-alfresco" }; logger.system.out("Executing SearchQuery: " + query); var imagenodes = search.query(searchQuery); var imagenodeForWatermarking = imagenodes[0]; logger.log("ImageNodeRef: "+ imagenodeForWatermarking.nodeRef+" | ImageName: "+imagenodeForWatermarking.properties["cm:name"]); //Inserta el gráfico en el pedido en función del firmante al que corresponda var watermark_action = actions.create("pdf-watermark"); watermark_action.parameters.inplace = true; //watermark_action.parameters["destination-folder"] = doc.parent; watermark_action.parameters["watermark-type"] = "image"; //watermark_action.parameters["watermark-image"] = eval(nodes[0]); watermark_action.parameters["watermark-image"] = imagenodeForWatermarking.nodeRef; //watermark_action.parameters["watermark-text"] = "Lorem Ipsum"; //watermark_action.parameters["watermark-font"] = "Helvetica"; //watermark_action.parameters["watermark-size"] = "12"; watermark_action.parameters.page = "all"; watermark_action.parameters["watermark-depth"] = "over"; watermark_action.parameters.position = "manual"; watermark_action.parameters["location-x"] = step; watermark_action.parameters["location-y"] ="1"; watermark_action.execute(pdfDocNode); pdfDocNode.save(); logger.log("***************************Incluida firma en : " + pdfDocNode.name + " " + step); } catch (ex) { logger.logt("Exception occurred: " + ex.message); }JavaScript Console Log
PDFDocNodeRef: workspace://SpacesStore/a46d1938-4cb5-4da2-963a-1ed91300ea2f | PDFDocName: 5766774840.pdf LoggedInUser: rcanovas ImageNodeRef: workspace://SpacesStore/7b48a76d-ebfb-44ea-9a18-9e32540d250b | ImageName: rcanovas.png ***************************Incluida firma en : 5766774840.pdf 100using userhome.properties.name to get username logged is ok because it shown in the log.
I feel lost because I don't understand the behavior of the query. It works in the console but not in the script
"userhome" gives you the info for current user’s Home Space. "person" root object is the correct way to get anything pertaining to a user, however- can you tell where exactly you are trying to execute the "addWaterMark.js" ? Are you creating a repository webscript (based on naming of the file it can't be used as webscript) ? or rulescript (possible to use the 'addWaterMark.js' file as rule script or can be loaded in jsconsole as well by uploading it to Repository > Data Dictionary > Scripts folder. When you upload it, make sure the content / mime type is set to Mimetype: Java Script
, changing it in Edit Properties if needed)?
It should work in jsconsole as well as a script executed either via rules or via jsconsole. If i compare your code (script vs jsconsole) i see some syntactical issues with it. See the one highlighted in red above.
ver step = "100!; --> var step = "100";
Result of the query seems null, could be because no image exist with the out of the 'userlogged ' variable or output itself is null:
var query = 'TYPE:\"cm:content\" AND @cm\\:name:"'+ userlogged + '.png"'; that is what this error indicates: Exception occurred: Cannot read property "nodeRef" from undefined
I suggest you use var userlogged = person.properties.userName; to get the name of currently logged in user and make sure a ".png" file exists in the repository with name e.g.: "admin.png"
pdfDocNode.save(); --> Not required, you are executing a backend action and not modifying the node in script. Backend action is doing the job.
In jsconsole code you have hardcoded userlogged and step variable is also correct.
var userlogged = "rcanovas";
var step = "100";
Corrected code below addWaterMark.js:
var userlogged = person.properties.userName; try { var pdfDocNode = document; var step = "100"; logger.system.out("PDFDocNodeRef: "+ pdfDocNode.nodeRef+" | PDFDocName: "+pdfDocNode.name); logger.system.out("LoggedInUser: "+ userlogged); //Assuming you have an image named after the logged in user somewhere in repo. e.g.: 'admin.jpg' var query = 'TYPE:\"cm:content\" AND @cm\\:name:"'+ userlogged + '.png"'; var searchQuery = { query: query, language: "fts-alfresco" }; logger.system.out("Executing SearchQuery: " + query); var imagenodes = search.query(searchQuery); var imagenodeForWatermarking = imagenodes[0]; logger.system.out("ImageNodeRef: "+ imagenodeForWatermarking.nodeRef+" | ImageName: "+imagenodeForWatermarking.properties["cm:name"]); var watermark_action = actions.create("pdf-watermark"); watermark_action.parameters.inplace = true; watermark_action.parameters["watermark-type"] = "image"; watermark_action.parameters["watermark-image"] = imagenodeForWatermarking.nodeRef; watermark_action.parameters.page = "all"; watermark_action.parameters["watermark-depth"] = "over"; watermark_action.parameters.position = "manual"; watermark_action.parameters["location-x"] = step; watermark_action.parameters["location-y"] ="1"; watermark_action.execute(pdfDocNode); } catch(ex) { logger.system.out("Exception occurred: " + ex.message); }
I would suggest debugging your code and check for basic syntactical errors as well instead.
In the end, it was foolishness.
Permissions!!! When I run de JavaScript Console, I'm with Admin user. The script is run by the user.
Thanks!!
Great, at least you figured out. good luck @pjcaracuel_2349
Ask for and offer help to other Alfresco Content Services Users and members of the Alfresco team.
Related links:
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.