Expand my Community achievements bar.

SOLVED

need to calculate sum of one column values in adobe campaign classic

Avatar

Level 5

 

 

 

 

Hello all,I am stuck in a requirement where i have to calculate sum of one column in adobe campaign classic ,please suggest how can i get below result as output-specifically last line in display logs after executing query or enrichment activity

 

ClassificationTotal RecipientTotal ContractDiff
61005050
150050450
3800100700
9600200400
4900500400
Total1900900 
Topics

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

1 Accepted Solution

Avatar

Correct answer by
Level 3

Hi @Shruti1 ,

You can use the following code after your enrichment activity inside a JavaScript activity. Ensure that all the necessary fields are created and have alias names, so you can reference these alias names in the query. Make sure to adjust the field names in the code to match the actual field names in your schema or enrichment.

// Define the schema name from the target schema
var schemaName = vars.targetSchema.substr(vars.targetSchema.indexOf(":") + 1);

// Create the query definition to select required fields
var query = xtk.queryDef.create(
<queryDef schema={vars.targetSchema} operation="select">
<select>
<node expr="@totalRecipient"/>
<node expr="@totalContract"/>
<node expr="@classification"/>
</select>
</queryDef>
);

// Execute the query
var result = query.ExecuteQuery();

// Initialize variables to store the sums
var totalRecipient = 0;
var totalContract = 0;
var totalDiff = 0;

// Loop through each record in the result set
for each (var rcp in result.recipient) {
// Sum TotalRecipient and TotalContract
totalRecipient += rcp.TotalRecipient;
totalContract += rcp.TotalContract;
// Calculate the difference and sum it
totalDiff += rcp.TotalRecipient - rcp.TotalContract;
}

// Log the calculated totals
logInfo("Total Recipient Sum: " + totalRecipient);
logInfo("Total Contract Sum: " + totalContract);
logInfo("Total Diff Sum: " + totalDiff);

 Thanks

 

View solution in original post

1 Reply

Avatar

Correct answer by
Level 3

Hi @Shruti1 ,

You can use the following code after your enrichment activity inside a JavaScript activity. Ensure that all the necessary fields are created and have alias names, so you can reference these alias names in the query. Make sure to adjust the field names in the code to match the actual field names in your schema or enrichment.

// Define the schema name from the target schema
var schemaName = vars.targetSchema.substr(vars.targetSchema.indexOf(":") + 1);

// Create the query definition to select required fields
var query = xtk.queryDef.create(
<queryDef schema={vars.targetSchema} operation="select">
<select>
<node expr="@totalRecipient"/>
<node expr="@totalContract"/>
<node expr="@classification"/>
</select>
</queryDef>
);

// Execute the query
var result = query.ExecuteQuery();

// Initialize variables to store the sums
var totalRecipient = 0;
var totalContract = 0;
var totalDiff = 0;

// Loop through each record in the result set
for each (var rcp in result.recipient) {
// Sum TotalRecipient and TotalContract
totalRecipient += rcp.TotalRecipient;
totalContract += rcp.TotalContract;
// Calculate the difference and sum it
totalDiff += rcp.TotalRecipient - rcp.TotalContract;
}

// Log the calculated totals
logInfo("Total Recipient Sum: " + totalRecipient);
logInfo("Total Contract Sum: " + totalContract);
logInfo("Total Diff Sum: " + totalDiff);

 Thanks