2012-05-23 30 views
2

我希望somone能夠進一步幫助我,因爲我真的被困在這裏。我試圖從post-creat插件添加合同行到我的合同實體。我的代碼:Add Contract Line to Contract CRM 2011

Guid c_Id = (Guid)entity.Attributes["contractid"]; 
DateTime start = (DateTime)entity["activeon"]; 
DateTime end = (DateTime)entity["expireson"]; 
Money money = new Money(); 
money.Value = 0; 


//Set Instance for Contract Line 
Entity ContractLine = new Entity(); 
ContractLine.LogicalName = "contractdetail"; 

//Create Contract Line for the Contract 
ContractLine["title"] = "PLUGIN FIRED"; 
ContractLine["activeon"] = start; 
ContractLine["expireson"] = end; 
ContractLine["totalallotments"] = 1; 
//ContractLine["customerid"] = entity["customerid"]; 
//ContractLine["productid"] = entity["productid"]; 
ContractLine["price"] = money; 
ContractLine["contractid"] = c_Id; 

service.Create(ContractLine); 

出於某種原因,我得到的錯誤「屬性:contractid不能設置爲NULL」,這真的stragnge,因爲它實際上沒有得到的GUID contractid,因爲我checkd而在另一場關於另一個實體。如果somone能幫助我,我真的很感激。謝謝。

回答

2

應該ContractLine["contractid"] = c_Id;是ContractLine["contractid"] = new EntityReference("contract", c_Id);

此外,應該c_Id是一個EntityReference而不是一個GUID?

+0

非常感謝你布恩,欣賞它非常適合我。我必須將其更改爲ContractLine [「contractid」] =新的EntityReference(「contract」,c_Id)。 –

1

任何機會,你有另一個插件,因爲你的拋出異常而發射。這似乎總是咬我。嘗試禁用除您正在工作的其他插件外的所有其他插件...

+0

嗨達里爾,我正在開發環境,但事實並非如此。但它真的很奇怪,因爲它有效地獲得了contractId,但是當我爲ContractLine [「contractid」]設置它時,我得到這個錯誤。 –

+1

所以,如果你調試它,這條線會引發異常:ContractLine [「contractid」] = c_Id;還是在創建過程中被拋出? – Daryl

+0

嗨達里爾,謝謝你effore的答案顯示如下,我不得不像這樣設置ContractLine [「contractid」] =新的EntityReference(「合同」,c_Id);再次申請您的回覆。 –