yes, look:
in table menu, all menu-items store their link, something like "index.php?option=com_content&view=article&id=58".
With articles, it doesn't work that good if the admin didn't make a menu-item for the article you want to get correct ItemID, because there might be items pointing to a lot of sections categories, or other articles. But with some changes you can make it be more precise...
Anyway, I made the function to work best with components, so I will give you an example for the eventlist component.
Let's say the admin has made menu-items for his eventlist component. The basic things of the eventlist are two: events, and venues.
Let's say you want to link to some event.
in this case you could call the function like this:
$eventid = 15; // or any other number
$link = "index.php?option=com_eventlist";
$exactlink = "&view=details&id=".$eventid;
$notlike = "%venue%";
$itemid = newItemID($link,$exactlink,$notlike);
$link_jc = JRoute::_($link."&view=details&id=".$eventid.$itemid);
It searches for the link "index.php?option=com_eventlist&view=details&id=15"
If the user has made a menu item that points to this event, it will find it and return the ID from the row it found it in the table jos_menu.
This is the ItemID. Let's say it is 28, so your $link_jc will be now "index.php?option=com_eventlist&view=details&id=15&Itemid=28". Cool, we found it!
If the user didn't make a menu-item for this event, the function will enter the (count($data)==0) condition, and it will search for a more generic eventlist menu-item. But we want to link to an event, so we must exclude venues, that's why $notlike="%venue%";
Since $notlike has a value now, this is true: if ($notlike) $notlike = "AND link NOT LIKE '$notlike'";
So $notlike is now "AND link NOT LIKE '%venue%'
So the query now will bring all eventlist menu-items which are good for events and will not bring any that are for venues.
You take the first one ($itemid = $data[0]->id) and you use it as ItemID.
I SAID this is the best GUESS, it's not always correct, but what can you do? We all know the problem with Joomla's stupid ItemIDs...
But I think this is too much. What joomlacomment needs is just a link to CB profiles. The MAIN menu-item of CB is this "index.php?option=com_comprofiler"
CB component tells you to make ONLY one main menu-item!
So, all you need to do is call the function like this:
$userid = 62; // or any other number
$link = "index.php?option=com_comprofiler";
$itemid = newItemID($link,'','');
$link_jc = JRoute::_($link."&task=userProfile&user=".$userid.$itemid);
This way, it will search for the main CB menu-item, and will return the exact ItemID!
If the admin didn't make the mandatory CB menu-item (highly unlikely), it will bring you the first one that is -at least- CB menu-item.
e.g. admin didn't make CB main menu-item, but made "edit profile" CB menu-item (index.php?option=com_comprofiler&task=userProfile&task=userDetails). It will bring the ItemID of this menu-item, at least!
Anyway, I had many complaints about ItemIDs in CB Super Activity, since my module lists more than 20 activities, it would be crazy to make 20 special functions for every different component. I made this, I didn't have ONE complaint since then!!