ObjectField refField = new ObjectField(refFieldName);
String relationshipName = refField.getObjectName();
String fieldName = refField.getFieldName();
// get object info for the given reference (foreign key) relationship
DescribeRefObject entityRefInfo = controller.getReferenceDescribes().get(relationshipName);
// build the reference SObject
SObject sObjRef = new SObject();
// set entity type, has to be set before all others
sObjRef.setType(entityRefInfo.getObjectName());
// set external id, do type conversion as well
Class typeClass = SforceDynaBean.getConverterClass(entityRefInfo.getFieldInfoMap().get(fieldName));
Object extIdValue = ConvertUtils.convert(this.referenceExtIdValue.toString(), typeClass);
sObjRef.setField(fieldName, extIdValue);
// Add the sObject reference as a child elemetn, name set to relationshipName
sObj.addField(relationshipName, sObjRef);
}