2016-10-05 65 views
2

我創建了新的實體。更新自定義工作流程活動中創建的記錄-CRM -C#

從那個實體我調用創建機會的自定義工作流活動實體。 它的工作原理,但另外我必須改變創造機會的一些領域。 (我必須添加機會產品,並且必須更改每個機會的價格清單)。

作爲一項測試,我嘗試在創建後更新帳戶字段,但它失敗了字段。當我在創建之前填充此帳戶字段時,它可以正常工作,所以它不是那樣的。 下面是部分代碼:

  Entity entity = null; 
     if (context.InputParameters != null && context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) 
     { 
      entity = (Entity)context.InputParameters["Target"]; 
     } 
     else 
     { 
      entity = service.Retrieve(context.PrimaryEntityName, ((Guid)context.PrimaryEntityId), new ColumnSet(true)); 
     } 
     Entity opportunity = new Entity("opportunity"); 
     string name = entity.GetAttributeValue<string>("subject"); 
     opportunity["name"] = name; 
     opportunityId = service.Create(opportunity); 
     EntityReference accountlookup = (EntityReference)entity.Attributes["ad_sendto"]; 
     Guid accountId = accountlookup.Id; 

     opportunity["parentaccountid"] = new EntityReference("account", accountId); 
     service.Update(opportunity); 

要重複,它創造的機會,但它不適用於更新工作,有沒有其他的辦法做到這一點,還是我在這裏有一些錯誤?

回答

2

由於您試圖更新opportunity未設置主鍵(opportunityid)的實體,因此失敗。

爲什麼不在創建操作過程中分配parentaccountid而不是在創建後更新機會?

var opportunity = new Entity("opportunity"); 
opportunity["name"] = entity.GetAttributeValue<string>("subject"); ; 
opportunity["parentaccountid"] = entity.Attributes["ad_sendto"]; 
opportunityId = service.Create(opportunity); 

以備將來參考,如果你有更新剛創建的實體或與此有關的任何實體:

var opportunityToUpdate = new Entity("opportunity") 
{ 
    Id = opportunityId 
}; 
opportunityToUpdate["parentaccountid"] = entity.Attributes["ad_sendto"]; 
service.Update(opportunityToUpdate); 
+0

'機會[ 「parentaccountid」] = entity.Attributes [「ad_sendto 「];' 'service.Update(opportunityToUpdate);' 最後兩行,你的意思是'機會[」parentaccountid「]'或'opportunityToUpdate [」parenaccountid「]',befor更新? –

+0

你是對的,複製粘貼錯誤,更新了答案。 – dynamicallyCRM

相關問題