How to get the value from the field and store in the variable

suuriya
Tera Contributor

Hi Community,

 

Im trying to get the value from the field and store it in the variable and reuse that variable in the script.

 

Basically, In sys_email table there is field called body text i want that value to be use in the description of the incident which is creating by this BR (written on sys_email table)....i tried something like email.body_text but didnt worked...so taught of gliding sys_email table and using encodedquery.

suuriya_0-1703158747219.png

 

suuriya_2-1703158807680.png

Idea of this BR is based on specific conditions incident record need to be created...which is working fine i did that but im struck with the description part.

suuriya_3-1703159251640.png

Please provide any insights on this...how we can get the value of body text from the recieved-ignored email and use it in the description field of the incident.

 

Note : This needs to be achieved by Br or something but not with inbound email actions because incident needs to be created when emails are received-ignored type.

 

Thanks in advance

9 REPLIES 9

Hello @suuriya,

 

The server name is present in the email body? Then you need to change your code.

Can you share your email body format? so I can help you in code.

 

Please Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Regards,

Vaishnavi Shinde

Hi @VaishnaviShinde ,

 

Yes server name is present in email body and in an incident configuration item (reference) in that table also the server name mentioned in email body is present in it.

 

I cant share the email body but it will look something like this

email body 

59*****4**5

**-np-**

 

---------------------------------------------

i-05**********f

Name:servername

Apmid:*****

---------------------------------------------

 

Name:servername this is were the actual server name will be mentioned

If you don't know how to use INBOUND EMAIL ACTION properly & still want to use this BR only to create INC, you will have to share the BODY what you see in the sys_email table for this particular email.

My actual Email Body is something as follows -

 

HSB_1-1703254272299.png

 

However I need body which looks something as follows from the sys_email table record -

HSB_0-1703254207918.png

You can hide any sensitive data from that email & just share the format of it but don't remove any HTML tags. That body is required because when you do 'current.body', you are accessing the whole email-body including the HTML Tags as well.

 

Now if you notice, I have added 'Name:ServerName' based on your earlier format in my email  and the ServerName is between 'Name:' & '</div>' strings 

 

Based on this assumption I was able to fetch the ServerName using following script -

 

var emailBody = current.body;
var Server = emailBody.substring(
    emailBody.indexOf("Name:") + 4,
    emailBody.lastIndexOf("</div>")
);
gs.log('Server Name is '+Server); /just to verify if ServerName is coming in the logs correctly
 
Finally instead of serName in query, use this Server variable as it would be containing the correct server name. Rest looks good as of now.
 
I am pretty sure you can achieve it this way however in your scenario, it could be just a bit different so I can't commit until you share your backend email-body available in 'Sys-Email' table record.
 
If it helps, please mark the response as ACCEPTED / HELPFUL. 

VaishnaviShinde
Kilo Sage

Hello @suuriya 

 

Try this code:

 

 

var string = "Name:*ANNIE-IBM\nApmid:" // Add your email body_text
var text1= string.replace(/\n/g, " ");
var serverName = string.split('Apmid:');
var servers = serverName[0];
var server = servers.toString();
var serv= server.split(':')
var serversName = serv[1].toString()
var result = serversName.trim();
var cmdbCi = new GlideRecord('cmdb_ci');
cmdbCi.addEncodedQuery('name='+result);
cmdbCi.query();
if(cmdbCi.next()){
    sysId= cmdbCi.getUniqueValue();
}

 

replace inc.cmdb_ci = name with inc.cmdb_ci = sys_id

  

Please Mark my Solution as Accept and Give me a thumbs up👍, if you find it Helpful.

VaishnaviShinde
Kilo Sage

Hello @suuriya ,

 

If my answer solve your issue.

Please Mark my Solution as Accept and Give me a thumbs up👍, if you find it Helpful.

 

Regards,

Vaishnavi Shinde