Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

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

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
Mega 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


Regards
BK
LinkedIn: https://www.linkedin.com/in/bhavyashree-karanth-55032a121
YouTube: https://www.youtube.com/@NowWithBhavya

View solution in original post

4 REPLIES 4

Not applicable

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
Mega 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


Regards
BK
LinkedIn: https://www.linkedin.com/in/bhavyashree-karanth-55032a121
YouTube: https://www.youtube.com/@NowWithBhavya

Ankur Bawiskar
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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Not applicable

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

 

Thank you.