How to user JavaScript String search() Method

Radhika11
Tera Contributor

Hello,

We are receiving service request submitted from using emails.  The RITM description field captures the entire email.

I would like to search the name "From: John,Lopez" in the RITM description field and populate in "Requested For" field.

See attached

 

Could someone please help?   Thank you

2 ACCEPTED SOLUTIONS

You are using an after business rule to update current. That beaks rule no. 1 of Business rules: whenever the current record is to be updated, it must be done using a before business rule.

Rule no. 2 being: whenever a different record than the current one needs to be updated, it must be done using an after business rule.

The other issue is the RegExp itself; it should be:

/^From:\s*(.+)$/mi

Another issue is that you are using the name directly to set the field. You need to fetch the user's sys_id and use that to set the value of the field:

GetIDValue('sys_user', )

Something like:

// Script to extract sender name from RITM description and set as requested for
// Get RITM description
var description = '' + current.description;

// Use regular expression to extract name from "From" field
var senderName = description.match(/^From:\s*(.+)$/mi);

// Set extracted name as requested for
current.requested_for = GetIDValue('sys_user', senderName[1]);

Of course the code should check that the value returned by GetIDValue is not null and only set the requested for if it is not. This is also a very fragile solution as the slightest mistype, even an additional or missing space in the name and it will not work.

View solution in original post

Hello,

Something simple like

var senderName = description.match(/^To:.+<mailto:([^<>]+)/mi);

should do it, but if you want a solution where you match either the original patter or this one, a different RegExp is needed.

View solution in original post

32 REPLIES 32

Hi @Radhika11 ,

As per the video you posted, the code did work. 

I've requested to write a before business rule in my previous replies. By mistake you have written after business rule.

Can you change it to before BR and test it? 

Till now we were able to get the name, means the code did work till the line before GlideRecord. Just change it to before BR  it should work as per me.

 

 

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

Good morning @Basheer 

I have tried both before and after BR and had not luck.  I did what you suggested again and it still did not work.  (See attached video).  Many thank for your time and effort. 

 

Hemant Kumar Ch
Kilo Sage

Hello Radhika 

 

Try with below script 

 

/

 

// Script to extract sender name from RITM description and set as requested for

// Get RITM description
var description = current.description;

// Use regular expression to extract name from "From" field
var senderName = description.match(/From: (.+?),/);

// Set extracted name as requested for
current.requested_for = senderName[1];

 

Thank you for helping @Hemant Kumar Ch 

I also tried your code and that did not work as well (See attached).

You are using an after business rule to update current. That beaks rule no. 1 of Business rules: whenever the current record is to be updated, it must be done using a before business rule.

Rule no. 2 being: whenever a different record than the current one needs to be updated, it must be done using an after business rule.

The other issue is the RegExp itself; it should be:

/^From:\s*(.+)$/mi

Another issue is that you are using the name directly to set the field. You need to fetch the user's sys_id and use that to set the value of the field:

GetIDValue('sys_user', )

Something like:

// Script to extract sender name from RITM description and set as requested for
// Get RITM description
var description = '' + current.description;

// Use regular expression to extract name from "From" field
var senderName = description.match(/^From:\s*(.+)$/mi);

// Set extracted name as requested for
current.requested_for = GetIDValue('sys_user', senderName[1]);

Of course the code should check that the value returned by GetIDValue is not null and only set the requested for if it is not. This is also a very fragile solution as the slightest mistype, even an additional or missing space in the name and it will not work.