2016-05-03 29 views
1

我需要檢索的全名和Dynamics CRM中接觸的上級單位。 我使用下面的代碼:獲取動態CRM接觸parentcustomerid

ColumnSet cols = new ColumnSet(new String[] { "fullname", "parentcustomerid" }); 
    Entity retrContact = (Entity)orgService.Retrieve("contact", contactID, cols); 
    fullName = retrContact.Attributes["fullname"]; 
    parentAccount = retrContact.Attributes["parentcustomerid"]; 
    nameStr = fullName.ToString(); 
    companyStr = parentAccount.ToString(); 

我的問題是companyStr獲得「Microsoft.Xrm.Sdk.EntityReference」,而不是名稱值。 parentAccount包含以下內容:

LogicalName "account" string 
    Name "Microsoft Corp" string 
    RowVersion null string 

如何獲取Name字符串?

+0

你已經有答案了。 companyStr是一個EntityReference,您可以簡單地使用companyStr.Name來獲取名稱。 – Renjith

+0

companyStr是一個字符串,那麼請將.Name不存在那裏。我想知道爲什麼我不能使用parentAccount.Name。我得到錯誤:\t \t parentAccount.Name \t錯誤CS1061:'對象'不包含'名稱'的定義並且沒有擴展方法'名稱'接受類型'對象'的第一個參數可以找到(你是否缺少一個使用指令或程序集引用?) –

回答

0

parentcustomerid對象是一個ñEntityReference對象,它具有您正在尋找的名稱。此代碼的工作:

ColumnSet cols = new ColumnSet(new string[] { "fullname", "parentcustomerid" }); 
Entity retrContact = (Entity)orgService.Retrieve("contact", new Guid("{9DF2ACC2-0212-E611-80E4-6C3BE5A83B1C}"), cols); 
var parentAccount = (EntityReference)retrContact.Attributes["parentcustomerid"]; 
var companyStr = parentAccount.Name; 
+0

謝謝@Polshgiant。你給了我一個主意。這裏是最終的代碼:'parentAccountRef =(EntityReference)retrContact.Attributes [「parentcustomerid」]; Entity retrAccount =(Entity)orgService.Retrieve(「account」,parentAccountRef.Id,new ColumnSet(true)); companyStr = retrAccount.Attributes [「name」]。ToString();' –

0

你或許應該取從服務器parentAccount(請參閱EntityReference.Name Property

This property is not automatically populated unless the EntityReference object has been retrieved from the server.

例如,你應該使用的Id parentcustomerid服務器獲取數據,有點像

Entity Account = service.Retrieve(Account.EntityLogicalName, parentAccount.Id, new ColumnSet(true)); 

你可以肯定替換Account.EntityLogicalName"account"字符串