Get the values from HTML table from an email in a Inbound Action

sivareddy
Tera Contributor

Hi All,

 

How to retrieve a value from HTML Table. I need to get the User Name.

 

sivareddy_0-1715251639414.png

 

Regards,

Siva

1 ACCEPTED SOLUTION

Hello Guys @Ansh1 , @sivareddy ,

 

Apologizes for the late reply. I was stuck in someother work. Here we go. I just worked with your HTML and pasting the code below to print the User Name --> Creddy.

 

Cheers🥂.

 

var html = 'Your HTML Code';
var splitHtml = html.split('</td>');
var count = 0;
for (var i = 0; i < splitHtml.length; i++) {
    if (splitHtml[i].includes('User Name')) {
        count = i + 1;
    }
}
var result = splitHtml[count].replace(/<[^>]*>/g, '').replace('&nbsp;&nbsp;', '').trim();
gs.info(result)

 

Regards,

Dhanraj.

View solution in original post

7 REPLIES 7

Jaspal Singh
Mega Patron
Mega Patron

Hi Siva,

Did you try something as below for parsing

var userdomainis=email.caller_user_domain; //should ideally give you value
gs.log('User domain ',userdomainis);

Hi @Jaspal Singh  I'm trying to pull the value from the received mail. i've attached sample template as well.

dhanrajb
Tera Guru

Hi @sivareddy ,

 

Is all that values pulled from the table or somewhere else? 

 

If it is pulled from the table, you can use the target record to print the values of it.

 

If it is not from table, you can split the html body using tags and pull the values from it. I have wrote a script for it using sample HTML and the JS to print the value of some string value in the HTML.

 

 

<style>
	#snc_notification_preference {
		text-decoration: none;
	}
	
	#snc_notification_unsubscribe {
		text-decoration: none;
	}
</style>

<style> #schedule-report table, #schedule-report th, #schedule-report td { border-collapse: collapse; border: 1px solid black; text-align: center; padding: 5px; font-size: 10pt; font-family: SourceSansPro, "Helvetica Neue", Arial; } #schedule-report th, #schedule-report tr > td:first-child { background-color: #767676; color: white; } </style><div><div id="schedule-report"><p>Please review pending actions for your On-Call group(s).</p><table><tr> <th>On-Call Coverage Gaps</th> <th>Group Name</th> <th>Manager Name</th> <th>Schedule Name</th> <th>Occurrences</th> <th>Take Action</th> </tr><tr> <td></td> <td>Application Development</td> <td>Bushra Akhtar</td> <td>Working Hours</td> <td>11</td> <td> <a href='https://dev57352.service-now.com/$oc_workbench.do?sysparm_group_id=0a52d3dcd7011200f2d224837e6103f2' target='_blank'>View Workbench</a></td> </tr></table></div></div>

<br></br>

<a id="snc_notification_unsubscribe" href="mailto:dev57352@servicenowdevelopers.com?subject=Unsubscribe from &quot;On-Call Schedule - Gaps and Conflicts&quot;&amp;body=Sending this email with the predefined content in the subject and body will unsubscribe you from the notification &quot;On-Call Schedule - Gaps and Conflicts&quot;%0D%0A%0D%0AUnsubscribe:{&quot;id&quot;:&quot;5d7cfc2273602300cbb654eb7df6a7ce&quot;,&quot;token&quot;:&quot;c797e62132&quot;}">Unsubscribe</a> | <a id="snc_notification_preference" href="https://dev57352.service-now.com/nav_to.do?uri=notification_preferences.do%3Fsysparm_notification=5d7cfc2273602300cbb654eb7df6a7ce">Notification Preferences</a>

 

In the below script, I used <td> to split the body of the html and I have printed the value of "Bushra Akhtar". You can use the same logic to print the value of username. Here I have used 'Application Development', you can use 'User Name'. 

 

var email = new GlideRecord('sys_email');
if(email.get('fc03c369974e4a104020b986f053af32')){
    var body = email.body;
    var spBody = body.split('<td>');
    for (var i = 0; i < spBody.length; i++){
        if(spBody[i].includes('Application Development')){
            var count = i;
        }
    }
    var name = spBody[count+1].replace('</td>','');
    gs.info(name);
}

 

 

This code might not work, because your HTML might differ from mine. If you are facing difficulties in print the value, paste the HTML here. I will give you the exact code to print the value of username.

 

Regards,

Dhanraj.

 

Ansh1
Tera Contributor

Hi,

We would like to Extract the 'User Name' that is 'creddy' here in the attached HTML. Request you to share the code for this.


Regards,

Ansh