Object cannot be converted to Link

Hello mates,

I am trying to get Links for an account and i am having the error below:

Object cannot be converted to Link

Code:

...
Object aplicacionesLink = IdentityDetails.getLinks();
	String fecha0;
	String fechaRol;
	String startDate2 = new SimpleDateFormat("yyyyMMdd").format(new Date());
	String NombreDetails;

if (aplicacionesLink instanceof List) {
	
		    List LinksIdentity = (List) aplicacionesLink;
		    
		    for (Link linkIde : LinksIdentity) {		    
		        Application appnameComp = linkIde.getApplication();

		        if (appnameComp == appPortal) {

		        	
		            List atributoConfecha = linkIde.getAttribute("Role Details");

		            for (String RoleDet : atributoConfecha) {

		                try {
		                    int startIndex = RoleDet.indexOf("#");
		                    NombreDetails = RoleDet.substring(0, startIndex);
		                   if (NombreDetails == roleName) {

		                        fecha0 = RoleDet.substring(RoleDet.indexOf("#") + 1);
		                        fechaRol = fecha0.substring(0, 10);
		                        requestLineDataMap.put("ValidFrom", fechaRol);
		                    } 
		                    else {
		                        requestLineDataMap.put("ValidFrom", startDate2);
		                    }
		                } catch (Exception e) {
		                    System.out.println(e.getMessage());
		                    requestLineDataMap.put("ValidFrom", startDate2);
		                }

		            }
		        }
		    }
		}

Could anyone help me, please!?
Any tip or help will be welcome! :slight_smile:

Thank you so much

  1. From the exception it is most likely from this line:
for (Link linkIde : LinksIdentity) {	
  1. Identity.getLinks() return a List of Link. There is no need for all those instanceof and casting.
  2. You should check your import statements. Maybe you imported a Link class from another package.
1 Like

What is IdentityDetails ? By the way things are capitalized, that looks to be a static class?

1 Like
public void fillRoleNameAndConnectorName(String roleName, Map requestLineDataMap, boolean isCuaApp,
			String applicationName, Identity IdentityDetails)

Thanks for that, I see now its a variable name. Its just odd to see it capitalized like a class would be.

If you’re getting the links off the Identity object, its going to return a list - all the time. You don’t really need to check the type.

I like using an iterator for this stuff. Try something like

List listOfLinks = IdentityDetails.getLinks();
Iterator listOfLinksItr = listOfLinks.iterator();
while(listOfLinksItr.hasNext()){
    Link link = (Link) listOfLinksItr.next();
    //Do stuff with the link here.
}

Alternatively (and probably preferred at this point - someone at SP can chime in on this one), you can use the IdentityService API class. I see your original code is only looking for certain app names too, which is bonus points in using this API.

Try:

import sailpoint.api.IdentityService

List listOfLinks = IdentityService.getLinks(IdentityDetails, appPortal) //just using your variables here.
Iterator listOfLinksItr = listOfLinks.iterator();
while(listOfLinksItr.hasNext()){
    Link link = (Link) listOfLinksItr.next();
    //Do stuff with the link here.
    //Also no need to check the application name, since this getLinks function does it for you.
}
2 Likes