Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Inbound action data

Deepthi13
Tera Expert

Hi Team, 

I am trying to set the data from inbound action in to description and (name-value pair) field with the data sent from in the excel. but description and name-value pair key both are not working properly. please suggest 
Script:

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
    var emailBody = email.body_text;
    gs.log("@@@1 Raw Email Body: " + emailBody);
    var rows = emailBody.split('&');
    var rowCount = 0;
    var disclaimer = "The contents of this e-mail and any attachment(s) may contain confidential or privileged information for the intended recipient(s)."; // Adjust this to match the start of your disclaimer text
    // Extract header row
    var headerRow = rows[0].split('\t');
    for (var i = 1; i < rows.length; i++) { // Start loop from i = 1 to skip the header row
        if (rows[i].indexOf(disclaimer) !== -1) {
            // Skip rows containing disclaimer text
            gs.log("Skipping row containing disclaimer: " + rows[i]);
            continue;
        }
        var row = rows[i].split('\t');
        var scTask = new GlideRecord('sc_task');
        scTask.initialize();
        var uDataString = ''; // String to store name-value pairs for u_data field
        var description = ''; // String to store description details for the task
        // Construct the description by pairing each header name with its corresponding value in the row
        for (var j = 0; j < headerRow.length; j++) {
            var headerName = headerRow[j];
            var value = row[j];
            // Format: HeaderName : Value on a new line
            uDataString += headerName + ' : ' + value + '\n';
            description += headerName + ' : ' + value + '\n';
        }
scTask.short_description = 'Task for Ercu';
        scTask.u_data = uDataString.trim(); // Set the u_data field with the name-value pairs as a string
        scTask.description = description.trim(); // Set the description field with row and header details
        scTask.insert();
        rowCount++;
    }
    gs.log("@@@ Total Rows Processed: " + rowCount);
})(current, event, email, logger, classifier);

The email excel format i am sending:

 

Application Id

Title

First Name

Surname

Internal/External

NI Number

Start Date

Pension Employer Contribution

Pension Employee Contribution

&

1

Mr

Sam

Ham

Internal

1023

11-11-2023

2232

2223

&

2

Ms

Abel

Tutor

External

222

14-12-2023

1232

1234

&

3

Mr

Ryan

Kelly

External

231

15-01-2023

2314

1221

&

 

7 REPLIES 7

Deepthi13
Tera Expert

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

 

    var emailBody = email.body_text;

    gs.log("@@@1 Raw Email Body: " + emailBody);

 

    var rows = emailBody.split('&');

    var rowCount = 0;

    var disclaimer = "The contents of this e-mail and any attachment(s) may contain confidential or privileged information for the intended recipient(s)."; // Adjust this to match the start of your disclaimer text

    // Extract header row

    var headerRow = rows[0].split('\t');

 

    for (var i = 1; i < rows.length; i++) { // Start loop from i = 1 to skip the header row

 

        if (rows[i].indexOf(disclaimer) !== -1) {

            // Skip rows containing disclaimer text

            gs.log("Skipping row containing disclaimer: " + rows[i]);

            continue;

        }

 

        var row = rows[i].split('\t');

        var scTask = new GlideRecord('sc_task');

        scTask.initialize();

        var description = '';

 

        // Construct the description by pairing each header name with its corresponding value in the row

        for (var j = 0; j < headerRow.length; j++) {

            description += headerRow[j] + ' : ' + row[j] + '\n'; // Format: HeaderName : Value

        }

 

        scTask.short_description = 'Task for Ercu';

        scTask.description = description.trim(); // Set the description field with row and header details

        scTask.insert();

        rowCount++;

    }

 

    gs.log("@@@ Total Rows Processed: " + rowCount);

 

})(current, event, email, logger, classifier);

 

 

 

 

Application Id

Title

First Name

Surname

Internal/External

NI Number

Start Date

Pension Employer Contribution

Pension Employee Contribution

&

1

Mr

Sam

Ham

Internal

1023

11-11-2023

2232

2223

&

2

Ms

Abel

Tutor

External

222

14-12-2023

1232

1234

&

3

Mr

Ryan

Kelly

External

231

15-01-2023

2314

1221

&

 

 

 

 

 

 

 

 

 

Amit Gujarathi
Giga Sage
Giga Sage

HI @Deepthi13 ,
I trust you are doing great.

Here's a revised version of your script with added comments for clarity:

(function runAction(current, event, email, logger, classifier) {
    var emailBody = email.body_text;
    gs.log("@@@1 Raw Email Body: " + emailBody);
    var rows = emailBody.split('&');
    var rowCount = 0;
    var disclaimer = "The contents of this e-mail..."; // Disclaimer text

    // Extract header row
    var headerRow = rows[0].split('\t');
    for (var i = 1; i < rows.length; i++) {
        if (rows[i].indexOf(disclaimer) !== -1) {
            gs.log("Skipping row containing disclaimer: " + rows[i]);
            continue;
        }
        var row = rows[i].split('\t');
        if (row.length < headerRow.length) {
            gs.log("Skipping incomplete row: " + rows[i]);
            continue; // Skip rows with fewer columns than headers
        }
        var scTask = new GlideRecord('sc_task');
        scTask.initialize();
        var uDataString = '';
        var description = '';

        for (var j = 0; j < headerRow.length; j++) {
            var headerName = headerRow[j];
            var value = row[j] || ''; // Handle missing values
            uDataString += headerName + ' : ' + value + '\n';
            description += headerName + ' : ' + value + '\n';
        }

        scTask.short_description = 'Task for Ercu';
        scTask.u_data = uDataString.trim();
        scTask.description = description.trim();
        scTask.insert();
        rowCount++;
    }
    gs.log("@@@ Total Rows Processed: " + rowCount);
})(current, event, email, logger, classifier);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



(function runAction(/* GlideRecord */ current, /* GlideRecord */ event, /* EmailWrapper */ email, /* ScopedEmailLogger */ logger, /* EmailClassifier */ classifier) {
    var emailBody = email.body_text;
    var rows = emailBody.split('&');
    var disclaimer = "The contents of this e-mail and any attachment(s) may contain confidential or privileged information for the intended recipient(s)."; // Adjust this to match the start of your disclaimer text
    var headerRow = rows[0].split('\t'); // Split by tab character assuming columns are separated by tabs
    for (var i = 1; i < rows.length; i++) {
        if (rows[i].indexOf(disclaimer) != -1) {
            continue;
        }
        var row = rows[i].split('\t'); // Split by tab character for each row to get column values
        var scTask = new GlideRecord('sc_task');
        scTask.initialize();
        var description = '';      
        for (var j = 0; j < headerRow.length; j++) {
            description += headerRow[j] + ' : ' + row[j] + '\n'; // Format: HeaderName : Value
        }      
        scTask.short_description = 'Task for Ercu';
        scTask.description = description.trim(); // Set the description field with row and header details      
        try {
            scTask.insert();
        } catch (e) {
            gs.error('Error creating sc_task record: ' + e);
        }
    }
})(current, event, email, logger, classifier);