Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards
SOLVED

Having while implementing this Javascript code in Adobe Campaign

Avatar

Level 1

// Variables
var FILE_PREFIX = "Mecca_Games_hashed_emails_";
var FILE_EXTENSION = ".csv";
var FILE_DIRECTORY = "C:\\Rank Sharing Folder\\MSAds\\";
// var SERVER_UPLOAD_URL = "https://duprdcpmgtstdstor01.blob.core.windows.net/msadsfiletransfer/"; // Replace with actual endpoint

// Generate timestamp and filename
var now = new Date();
var isoTimestamp = now.toISOString().replace(/:/g, "-");
var filenamePart = FILE_PREFIX + isoTimestamp + FILE_EXTENSION;
var filename = FILE_DIRECTORY + filenamePart;

// Query extracted data
var url = 'C:\\Rank Sharing Folder\\MSAds\\'; // Replace with your actual endpoint
var params = {
schema: vars.targetSchema,
operation: 'select',
select: ['@email']
};

fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(params)
})
.then(function(response) { return response.json(); })
.then(function(data) {
// handle your data here
console.log(data);
})
.catch(function(error) {
console.error('Error:', error);
});

var records = query.ExecuteQuery();

// Build CSV content
var CSV_HEADER = "EMAIL,\n";
var csvContent = CSV_HEADER;
for (var i = 0; i < records.length; i++) {
var record = records[i];
var email = record.email; // or record.@email.toString() in some environments
var hashedEmail = digestStrSha256(email);
csvContent += hashedEmail + "\n";
}

// Write to file
var file = new File(filename);
file.open("w");
file.write(csvContent);
file.close();
logInfo("File Written: " + filename);

// Send file to server (Node.js-style HTTP POST)
var uploadUrl = 'https://prodftp.rank.com'; // Use HTTPS for secure transfer
var fileInput = document.querySelector('input[type="file"]'); // Your file input element
var formData = new FormData();
formData.append('file', fileInput.files[0]); // 'file' is the form field name
var options = {
method: 'POST',
body: formData,
// Do NOT set Content-Type; the browser will set it to multipart/form-data with boundary
};
fetch(uploadUrl, options)
.then(function(response) { return response.json(); })
.then(function(data) {
// handle your response here
console.log(data);
})
.catch(function(error) {
console.error('Error:', error);
});

logInfo("File Written: \n" + filename);

kindly check why this is not working on Adobe Campaig 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @GirishwarsinghCh

 

Great, happy to hear this. If you need more things, just let me know. Could you please click the “Correct Reply” button?

 

Thanks,

 

Celia

View solution in original post

3 Replies

Avatar

Community Advisor

Hi @GirishwarsinghCh

 

It might not be working cause you are mixing Js functions with Adobe Campaign server side J-S. Try using xtk:queryDef for queries, file for writing under var, and httpClientRequest for uploads. Give it a try with this code:

 

// --- Configuration (using Adobe Campaign accessible directories)
var OUT_DIR = "/var/msads/";
var FILE_PREFIX = "Mecca_Games_hashed_emails_";
var FILE_EXTENSION = ".csv";

// Create the output directory if it doesn’t exist
var dir = new File(OUT_DIR);
if (!dir.exists) dir.mkdir();

// Build the filename with timestamp
var timestamp = new Date().toISOString().replace(/:/g, "-");
var filenamePart = FILE_PREFIX + timestamp + FILE_EXTENSION;
var fullPath = dir.path + filenamePart;

// --- Extract email data via xtk:queryDef
var query = xtk.queryDef.create(
  <queryDef schema={vars.targetSchema} operation="select">
    <select>
      <node expr="@email"/>
    </select>
  </queryDef>
);
var result = query.ExecuteQuery();

// --- Generate CSV file with hashed emails
var file = new File(fullPath);
file.open("w");
file.writeln("EMAIL");

for each (var row in result..row) {
  var email = (row.@email.toString() || "").toLowerCase().replace(/^\s+|\s+$/g, "");
  if (email) {
    var hashedEmail = digestStrSha256(email);
    file.writeln(hashedEmail);
  }
}
file.close();
logInfo("File successfully written: " + fullPath);

// --- Optional: Upload the file via HTTP (multipart/form-data)
// Recommend using a 'Transfer file' activity if available
try {
  var http = new HttpClientRequest("https://prodftp.rank.com");
  http.method = "POST";
  var boundary = "----ACC" + String(Math.random()).substr(2);
  http.header["Content-Type"] = "multipart/form-data; boundary=" + boundary;

  var content = loadFile(fullPath, "utf-8");
  var body =
    "--" + boundary + "\r\n" +
    'Content-Disposition: form-data; name="file"; filename="' + filenamePart + '"\r\n' +
    "Content-Type: text/csv\r\n\r\n" +
    content + "\r\n" +
    "--" + boundary + "--";

  http.body = body;
  http.execute();

  logInfo("File uploaded successfully. Response: " + http.responseCode + " — " + http.responseText);
} catch (e) {
  logError("File upload failed: " + e);
}

 

Some API documentation that might help you: 

SOAP CALLS 

DIGESTSTRSHA256 

 

Best, 

Celia

Avatar

Level 1

Hello @ccg1706,

Thank you for your help.

 

I did try your code and its working perfectly fine.

Avatar

Correct answer by
Community Advisor

Hi @GirishwarsinghCh

 

Great, happy to hear this. If you need more things, just let me know. Could you please click the “Correct Reply” button?

 

Thanks,

 

Celia