diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php
index 08617ed31..cd24bdf22 100644
--- a/application/cmdbabstract.class.inc.php
+++ b/application/cmdbabstract.class.inc.php
@@ -35,7 +35,7 @@ abstract class cmdbAbstractObject extends CMDBObject
protected static function MakeHyperLink($sObjClass, $sObjKey, $aAvailableFields)
{
- if ($sObjKey == 0) return 'undefined';
+ if ($sObjKey <= 0) return 'undefined'; // Objects built in memory have negative IDs
$oAppContext = new ApplicationContext();
$sExtClassNameAtt = MetaModel::GetNameAttributeCode($sObjClass);
@@ -89,23 +89,35 @@ abstract class cmdbAbstractObject extends CMDBObject
if ($oAtt->IsExternalKey())
{
- // retrieve the "external fields" linked to this external key
$sTargetClass = $oAtt->GetTargetClass();
- $aAvailableFields = array();
- foreach (MetaModel::GetExternalFields(get_class($this), $sAttCode) as $oExtField)
+ if ($this->IsNew())
{
- $aAvailableFields[$oExtField->GetExtAttCode()] = $oExtField->GetAsHTML($this->Get($oExtField->GetCode()));
- }
- $sExtClassNameAtt = MetaModel::GetNameAttributeCode($sTargetClass);
- // Use the "name" of the target class as the label of the hyperlink
- // unless it's not available in the external fields...
- if (isset($aAvailableFields[$sExtClassNameAtt]))
- {
- $sDisplayValue = $aAvailableFields[$sExtClassNameAtt];
+ // The current object exists only in memory, don't try to query it in the DB !
+ // instead let's query for the object pointed by the external key, and get its name
+ $targetObjId = $this->Get($sAttCode);
+ $oTargetObj = MetaModel::GetObject($sTargetClass, $targetObjId, false); // false => not sure it exists
+ if (is_object($oTargetObj))
+ {
+ $sDisplayValue = $oTargetObj->GetName();
+ }
}
else
{
- $sDisplayValue = implode(' / ', $aAvailableFields);
+ // retrieve the "external fields" linked to this external key
+ foreach (MetaModel::GetExternalFields(get_class($this), $sAttCode) as $oExtField)
+ {
+ $aAvailableFields[$oExtField->GetExtAttCode()] = $oExtField->GetAsHTML($this->Get($oExtField->GetCode()));
+ }
+ // Use the "name" of the target class as the label of the hyperlink
+ // unless it's not available in the external fields...
+ if (isset($aAvailableFields[$sExtClassNameAtt]))
+ {
+ $sDisplayValue = $aAvailableFields[$sExtClassNameAtt];
+ }
+ else
+ {
+ $sDisplayValue = implode(' / ', $aAvailableFields);
+ }
}
}
else
diff --git a/pages/ajax.render.php b/pages/ajax.render.php
index 477643d55..fcc2e0e90 100644
--- a/pages/ajax.render.php
+++ b/pages/ajax.render.php
@@ -89,8 +89,9 @@ switch($operation)
{
$sId = $oWizardHelper->GetIdForField($sAttCode);
$value = $oObj->Get($sAttCode);
+ $displayValue = $oObj->GetDisplayValue($sAttCode);
$oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
- $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, '', 'att_'.$sId, '', 0, array('this' => $oObj));
+ $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, $displayValue, 'att_'.$sId, '', 0, array('this' => $oObj));
$oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
}