- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi,
I am working with a field that contains LDAP Distinguished Names (DNs) concatenated using ^ as a separator. I need to extract the CN (Common Name) from each DN. CNs can be either numeric IDs (e.g., 02042174) or person names that may include escaped commas (e.g., DUNN\, KAREN {FLNA}).
Sample input (single string)
CN=DUNN\, KAREN {FLNA},OU=CaOnMississauga1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=King\, Alzra {FLNA},OU=UsVaLynchburg1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=02042174,OU=PCNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=Wilson\, Brian J {FLNA},OU=UsTxPlano1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt..........................
Expected CN outputs: I need to extract CN from all DN in the below format.
DUNN, KAREN {FLNA}
King, Alzra {FLNA}
02042174
Wilson, Brian J {FLNA}
I have written this script but not showing result as expected :
var members = "CN=DUNN\, KAREN {FLNA},OU=CaOnMississauga1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=King\, Alzra {FLNA},OU=UsVaLynchburg1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=02042174,OU=PCNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=Wilson\, Brian J {FLNA},OU=UsTxPlano1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt";
var dnArray = members.split('^');
for (var i = 0; i < dnArray.length; i++) {
var dn = dnArray[i].trim();
if (!dn) continue;
// Extract CN value
var cnMatch = dn.match(/CN=([^,]+)/);
if (!cnMatch) continue;
var cnValue = cnMatch[1].replace("\\,", ",").trim();
gs.info("Chandra@@@: CNmatch: " + cnValue);
}
Please correct this where needed.
Thank You in advanced.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
try this
function extractCNs(members) {
var cns = [];
var dnArray = members.split('^');
for (var i = 0; i < dnArray.length; i++) {
var dn = dnArray[i].trim();
if (!dn) continue;
// Match CN=... until the next comma that is not part of the CN value
var match = dn.match(/^CN=([^,]+(?:,[^=]+)*)/);
if (match) {
var cnValue = match[1].replace(/\\,/g, ',').trim();
cns.push(cnValue.replace(",OU", ''));
}
}
return cns;
}
// Example usage
var members = "CN=DUNN\\, KAREN {FLNA},OU=CaOnMississauga1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=King\\, Alzra {FLNA},OU=UsVaLynchburg1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=02042174,OU=PCNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=Wilson\\, Brian J {FLNA},OU=UsTxPlano1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt";
var cns = extractCNs(members);
// Output each CN on a new line
for (var i = 0; i < cns.length; i++) {
gs.info(cns[i]);
}
Output:
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
yes why not
Thank you for marking my response as helpful.
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Ankur Bawiskar
Thank You for your support.
Now result coming like : CN=BALLEZ, ANTONIO {FLNA}.
I want add one more space before {FLNA} like : CN=BALLEZ, ANTONIO {FLNA}
Please help on this I will accept as solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
try this
function extractCNs(members) {
var cns = [];
var dnArray = members.split('^');
for (var i = 0; i < dnArray.length; i++) {
var dn = dnArray[i].trim();
if (!dn) continue;
// Match CN=... until the next comma that is not part of the CN value
var match = dn.match(/^CN=([^,]+(?:,[^=]+)*)/);
if (match) {
var cnValue = match[1].replace(/\\,/g, ',').trim();
// Add an extra space before {FLNA}
cnValue = cnValue.replace(/\{FLNA\}/, ' {FLNA}');
cns.push(cnValue.replace(",OU", ''));
}
}
return cns;
}
// Example usage
var members = "CN=DUNN\\, KAREN {FLNA},OU=CaOnMississauga1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=King\\, Alzra {FLNA},OU=UsVaLynchburg1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=02042174,OU=PCNA,OU=People,DC=corp,DC=pep,DC=pvt^CN=Wilson\\, Brian J {FLNA},OU=UsTxPlano1,OU=FLNA,OU=People,DC=corp,DC=pep,DC=pvt";
var cns = extractCNs(members);
// Output each CN on a new line
for (var i = 0; i < cns.length; i++) {
gs.info(cns[i]);
}
Output: 2 space before FLNA
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Ankur Bawiskar,
It is passing correctly before in scripts - background result also .
But I am using this in transform map it is showing only one space however, I used you latest script which is adding one more space.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Chandra18
Glad to know.
Please mark my response as correct and close the thread.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader