Hello,
In a script runner, I am trying to access LDAP attributes.
I have this piece of code that works if I use the phone attribute :
import com.onresolve.scriptrunner.ldap.LdapUtil
import org.springframework.ldap.core.AttributesMapper
import javax.naming.directory.SearchControls
import javax.naming.directory.Attributes
def cnList = LdapUtil.withTemplate('LDAP') { template ->
template.search("", "(cn=123456)", SearchControls.SUBTREE_SCOPE, { Attributes attributes ->
attributes.get('telephoneNumber').get()
} as AttributesMapper<String>)
}
But if I try to retrieve the department attribute, I have this error message:
java.lang.NullPointerException: Cannot invoke method get() on null object
at Script209$_run_closure1$_closure2.doCall(Script209.groovy:8)
at com.sun.proxy.$Proxy6509.mapFromAttributes(Unknown Source)
at org.springframework.ldap.core.AttributesMapperCallbackHandler.getObjectFromNameClassPair(AttributesMapperCallbackHandler.java:62)
at org.springframework.ldap.core.CollectingNameClassPairCallbackHandler.handleNameClassPair(CollectingNameClassPairCallbackHandler.java:50)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:367)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:328)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:604)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:594)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:482)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:498)
at Script209$_run_closure1.doCall(Script209.groovy:7)
at com.sun.proxy.$Proxy6283.apply(Unknown Source)
at java_util_function_Function$apply.call(Unknown Source)
at com.onresolve.scriptrunner.ldap.DefaultLdapConnectionManager$_withTemplate_closure2.doCall(DefaultLdapConnectionManager.groovy:50)
at com.onresolve.scriptrunner.ldap.DefaultLdapConnectionManager$_withTemplate_closure2.doCall(DefaultLdapConnectionManager.groovy)
at com.atlassian.plugin.util.ContextClassLoaderSwitchingUtil.runInContext(ContextClassLoaderSwitchingUtil.java:48)
at com.onresolve.scriptrunner.ldap.DefaultLdapConnectionManager.withTemplate(DefaultLdapConnectionManager.groovy:49)
at com.onresolve.scriptrunner.ldap.LdapConnectionManager$withTemplate$0.callCurrent(Unknown Source)
at com.onresolve.scriptrunner.ldap.DefaultLdapConnectionManager.withTemplateTracked(DefaultLdapConnectionManager.groovy:66)
at com.onresolve.scriptrunner.ldap.LdapConnectionManager$withTemplateTracked.call(Unknown Source)
at com.onresolve.scriptrunner.ldap.LdapUtil.withTemplate(LdapUtil.groovy:13)
at Script209.run(Script209.groovy:6)
Could you help me so that I can retrieve the department attribute in a script
I know this is a very old post and this answer is super late, but I just spent a lot of time today troubleshooting the same issue. When your query returns multiple people, if that attribute is empty for even a single person the script will return a null pointer error.
You could surround your script in a try catch block, and if a NullPointerException is thrown take some other approach like pulling all the usernames (attribute samaccountname) and then iterate through them one at a time.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.