Need to create AD account with lastname, firstname.

kumbharh
Tera Contributor

While creating AD account it's creating account with first initial last name same as Samaccount name, but I want to create account with lastname, firstname,

When I replace CN with lastname, firstname , it's showing error as dn syntax invalid 

How we can achieve this.

 

1 REPLY 1

Ravi Gaurav
Giga Sage
Giga Sage

In ServiceNow, when you're creating an Active Directory (AD) account through an integration or automation, and you want to format the CN (Common Name) as "lastname, firstname", you need to ensure the DN (Distinguished Name) is properly formatted to avoid syntax errors.

Steps to Achieve This in ServiceNow:

  1. Escape the Comma in CN:

    • When constructing the CN in your ServiceNow script, you need to escape the comma to conform to AD's DN requirements. For example, if you're using a script in a flow or a business rule:
      var lastName = "Doe"; var firstName = "John"; var cn = lastName + "\\, " + firstName; var dn = "CN=" + cn + ",OU=Users,DC=domain,DC=com";
  2. Creating the User in AD:

    • Use the cn variable when constructing the DN in your REST API call or LDAP operation. Here's a basic example of how this might look in a script:
      var requestBody = { "attributes": { "samAccountName": "jdoe", "userPrincipalName": "jdoe@domain.com", "cn": cn, "sn": lastName, "givenName": firstName, // Add other required attributes here }, "dn": dn }; // Assuming you're making a REST call to your AD integration var response = sn_ws.RESTMessageV2('AD Create User', 'post'); response.setRequestBody(JSON.stringify(requestBody)); var responseObj = response.execute();
  3. Handle the Response:

    • Ensure your script handles any potential errors from AD, especially regarding DN syntax issues, so you can troubleshoot if the formatting needs adjustment.

Important Considerations:

  • Attribute Validation: Ensure that other required attributes in the AD creation process are correctly provided and formatted.
  • Error Handling: Add error handling to catch and log any issues with the DN syntax or other problems in the creation process.
--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/