Retrieve the string preceding the @ from the array list's email ID.

Community Alums
Not applicable

Hi ,

Arr=[abc@gmail.com,xyz@gmail.com,pqr@gmail.com]

I need to retrieve name from the given array list.

for eg: abc, xyz, pqr 

1 ACCEPTED SOLUTION

Bhavya11
Kilo Patron

hi @Community Alums ,

 

try to run below script in background/fix script

var oldemail=['abc@gmail.com','xyz@gmail.com','pqr@gmail.com'];
var newarray=[];
for(var i=0;i<oldemail.length;i++){
var emails= oldemail[i];
var names=emails.split('@')[0];
newarray.push(names);
}
gs.info(newarray);

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks,

BK

View solution in original post

4 REPLIES 4

Valmik Patil1
Kilo Sage

Hello @Community Alums ,

Below is the code

// Array of email addresses
var emails = ["abc@gmail.com", "xyz@gmail.com", "pqr@gmail.com"];

// Array to hold the names
var names = [];

// Loop through each email
for (var i = 0; i < emails.length; i++) {
  // Split the email at '@' and take the first part (the name)
  var name = emails[i].split('@')[0];
  names.push(name);
}

gs.info(names);  // Output: ['abc', 'xyz', 'pqr']

Try running it in background.

Thanks,

Valmik Patil

Bhavya11
Kilo Patron

hi @Community Alums ,

 

try to run below script in background/fix script

var oldemail=['abc@gmail.com','xyz@gmail.com','pqr@gmail.com'];
var newarray=[];
for(var i=0;i<oldemail.length;i++){
var emails= oldemail[i];
var names=emails.split('@')[0];
newarray.push(names);
}
gs.info(newarray);

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks,

BK

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

this should be a simple one.

what did you try so far?

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

Hi Ankush, I posted this question because I attempted the same script but was not obtaining the desired results.

 

Thank you.