Hi all, I have this situation where I create a sav...
# suitescript
h
Hi all, I have this situation where I create a saved search inside a Map/Reduce script and then do "task.create" to create a CSV file in a cabinet folder. Then i do "file.load" to load the csv file and do email.send to attach that csv. I'm able to view the file under the Communication tab of Customer just fine, and actually receives the email and view the csv attachment in sandbox environment. However, when I deploy to Production, the recipients only see a blank file. What am I missing here? TIA
Copy code
let mySearch = task.create({
    taskType: task.TaskType.SEARCH,
 });
mySearch.savedSearchId = customStatement;
 mySearch.filePath = filePath;
let taskId = mySearch.submit();

let taskStatus = task.checkStatus(taskId);

let fileId = taskStatus.fileId;

let csvFile = file.load({ id: fileId});

let myMergerResult = render.mergeEmail({
templateId: CONSTANTS.Template,
 entity: {
      type: 'customer',
      id: Number(custId)
   }
  });
 email.send({
    author: CONSTANTS.author,
    recipients: ARContacts,
    cc: carbonCopyEmailList,
    bcc: [CONSTANTS.scriptAuthor],
    subject: myMergerResult.subject,
    body: myMergerResult.body,
    attachments: [csvFile],
    relatedRecords: {
          entityId: Number(custId)
    }
  });
a
if it works fine in sandbox but not in production its unlikely to be an issue with the code.
the file in the file cabinet is fine in prod? and you can view the attachment in the messages tab in the NS UI? ... are you getting the bcc of the email in prod? is it also an empty attachment for you?
w
Since the search-task async compared to the M/R, there's no guarantee that the file is complete when you send the email.
r
A simple solution for this is instead of creating csv through the task module, just simply create the csv in MR , using file.create and send the email. You don't even need to save it in filecabinet unless the use case requires it to be saved. As the email attachment will get saved regardless.
h
@Anthony OConnor That’s correct. I bcc’ed myself in Prod and I received an blank file as well. I don’t have that issue in Sandbox
@Watz That’s what I’m afraid too. Is there a way to delay email.send until the file is complete?
@raghav do we have to do concatenate columns in file.create?
w
Not really, but you can attach a script to the task to be executed once it is completed. That script could send the email.
Or run the search synchronous in the m/r and put the csv together yourself before sending the email.
h
@Watz Thanks for the idea, i’m going to try that
r
I will give the code for creating a CSV once I am back on PC. Probably in a few hours. You just have to create a n array where columns are indexes of the array. And to separate each row you just add an enter (/n) at the end of each row.
thankyou 1
Copy code
var csvHeader = ["Header1","Header2","Header3","Header4"];
csvContent += csvHeader;
search loop {
    csvContent += "\r\n";
    var csvRow = new Array();
    var cell1 = result.getValue();
    var cell2 = result.getValue();
    var cell3 = result.getValue();
    var cell4 = result.getValue();
    // please note if any of the cell data has a possible chance of having a comma inside it, then you need to wrap it first in "" or you can replace comma with something else like .replace(/,/g, ';') else it will split the data into 2 different columns

    if(cell1.indexOf(",")!=-1) {
    cell1 = '"'+cell1+'"';
    }
    csvRow.push(cell1,cell2,cell3,cell4);  
    csvContent += csvRow.join();
}
var csvFile = file.create({
    name: "xyz.csv",
    fileType: file.Type.CSV,
    contents: s_csvContent,
    encoding: file.Encoding.UTF8
});
email.send({
    author: 1234,
    recipients: 5678,
    subject: emailSubject,
    body: emailBody,
    attachments: [csvFile],
    relatedRecords: null,
    cc: null
});
thanks 1
r
You can await completion of the async task by checking the task status iteratively. Not the most elegant solution, obviously. I haven't found a better way, but I'm all ears.
w
@reptar while(not complete) { checkStatus() }? Sounds like it would burn through execution count limit pretty fast.
r
No, it's a property of the the task object. No governance.
It's terrible performance and a bad design, but doesn't impact governance
w
Execution count is a different limit then governance
r
I've never exceeded the instruction count. I've done this exporting about a million lines.
To be clear, I think @raghav’s solution is what you would want to do here, but for massive result sizes, the search task works when searching fails.
Or @Watz ' MR idea
👍 1
m
r
I figured there must have been an intended way to do this
w
That was what I meant by: “Not really, but you can attach a script to the task to be executed once it is completed.”