Expand my Community achievements bar.

Use of Expr in Schema. How to calculate a field using a JS function created in JavaScript Codes

Avatar

Level 7

Hello, I've a JavaScript Code called hash, with a function named toHash

I want to add it to a Data Schema, so, using the attribute "expr", can calculate the value of a field (For example, send @email to the function and write in a field the return)

 

How should I do it? 

@_Manoj_Kumar_ 

Topics

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

8 Replies

Avatar

Community Advisor

Hi @CampaignerForLife,

as far as I know, expr can only be used to call SQL functions, not JavaScript functions. For SQL methods, you can see this documentation link on how to add custom SQL functions:

https://experienceleague.adobe.com/docs/campaign-classic/using/configuring-campaign-classic/api/addi...

 

To achieve a use case that requires to calculate a hash via JavaScript, you might be better off calling it inside a workflow and run the function on a per recipient basis, that would be a combination of other workflow activities such as query, javascript, and update data activity.

 

OR, you can also do all that inside a single javascript activity - but it will have its own drawbacks. The more recipients you process in one go, the more memory space the js consumes, and can also stall your entire platform.

 

So, I would go with the first option - use query, js and update data activity and loop over all these until you process all recipients.

 

 

Regards,

Ishan

Avatar

Level 7

But, adding an SQL function, would I be able to use a JS code at some point?

Avatar

Employee Advisor

@CampaignerForLife ,

To add the JavaScript code to a data schema and use the toHash function to calculate the value of a field, you can follow these steps:

Define your data schema: Create a JSON schema that describes the structure of your data. For example:

{

  "type": "object",

  "properties": {

    "email": {

      "type": "string"

    },

    "hashedEmail": {

      "type": "string",

      "expr": "toHash(@email)"

    }

  }

}

Define the toHash function: Implement the toHash function in JavaScript. Here's an example of how you can create a simple hash function using the built-in crypto module in Node.js:

const crypto = require('crypto');

function toHash(value) {

  const hash = crypto.createHash('sha256');

  hash.update(value);

  return hash.digest('hex');

}

 

Make sure to customize the hash function according to your specific requirements. You can use different algorithms or libraries depending on your needs.

Implement the data processing logic: Parse the data according to the schema and calculate the hashed value using the toHash function. Here's an example of how you can accomplish this using Javascript:

 

const data = {

  "email": example@example.com

};

const hashedEmail = toHash(data.email);

data.hashedEmail = hashedEmail;

console.log(data);

By running the above code, you should see the output with the original email and the hashed email:

{

  "email": example@example.com,

  "hashedEmail": "ac3584e0b2248d12d79211e3a7808626e8c9b8a6f2b229c60d0b792c8197810e"

}

 

You can modify this code according to your specific use case and the environment in which you are working.

Avatar

Level 7

Could you go deeper? I don't understand how to use this stuff in an expr="" in a data schema

Avatar

Employee Advisor

@CampaignerForLife,

Certainly!

When defining a data schema with the expr attribute, you can use it to specify an expression that will be evaluated to determine the value of a field. The expression can reference other fields in the data object and perform calculations or transformations on them.

Here's an example of how you can use the expr attribute to calculate a hashed value for the email field:

{

  "type": "object",

  "properties": {

    "email": {

      "type": "string"

    },

    "hashedEmail": {

      "type": "string",

      "expr": "toHash(@email)"

    }

  }

}

 

In this example, the hashedEmail field has an expr attribute set to "toHash(@email)".

This expression uses the toHash function to calculate the hash value based on the value of the email field.

To make this work, you'll need to define the toHash function separately.

 

The evaluateExpression function is a helper function that replaces the field references (e.g., @email) in the expression with their corresponding values from the data object. It then evaluates the modified expression using eval().

Finally, we calculate the hashed email value by evaluating the expression in the data schema using the evaluateExpression function.

When you run this code, you should see the output with the original email and the calculated hashed email:

{

  "email": example@example.com,

  "hashedEmail": hashed_example@example.com

}

 

Remember to replace the hashing logic in the toHash function with your actual hashing implementation. This was just a simplified example to demonstrate the concept.

Avatar

Community Advisor

Hi @CampaignerForLife ,

 

What kind of hashing algorithm would you be using?

 

because you can able to do it in 3 ways as per below link.

 

https://experienceleaguecommunities.adobe.com/t5/adobe-campaign-classic-questions/sha256-and-sha512-...

 

The general syntax to use expr in the Data Schema attributes would be as shown below(This example is for  SHA256 hash for a given UTF8 string) :- 

 


<attribute desc="Desc of attribute" expr="Sha256Digest(@email)"
label="Encrypted identifier of email " name="encryptemail" type="string" />

 

Also below is the documentation link for the list of functions in Adobe classic campaign, where hash functions are also given,

 

https://experienceleague.adobe.com/docs/campaign-standard/using/managing-processes-and-data/filterin...

 

Regards,

Pravallika.

 

I will give you an example of what I want:
I've a JavaScript Code with a function: example(string), that returns another string.


Is there any way to add to the data schema expr="example(@email)", so the value is set using that function?

 

Thank you!!

@akshaaga @isahore @_Manoj_Kumar_ 

Avatar

Administrator

Hi @CampaignerForLife,

Were you able to resolve this query with the help of the given solutions or do you still need more help here? In case the given solutions were helpful, then kindly choose the one that helped you the most as the 'Correct Reply'.
Thanks!



Sukrity Wadhwa