我的PLUGIN正在点击实体A ,在我的代码中,我正在调用一个Web服务,该服务返回一个带有一些属性的文件(attr1,attr2,attr3等...)实体B ,包括GUID。 我需要使用从Web服务收到的属性更新实体B.
我可以使用服务上下文类(SaveChanges)或者完成任务的最佳方法是什么? 如果你提供一个例子,我将不胜感激。
答案 0 :(得分:2)
在此实例中,您没有理由需要使用服务上下文。这是我如何解决这个要求的基本例子。您显然需要更新此代码以使用适当的实体,实现外部Web服务调用,并处理字段更新。此外,这没有任何错误检查或处理,因为应该包含在生产代码中。
我假设您使用的是早期绑定的实体类,如果不是,则需要更新代码以使用通用Entity()
。
class UpdateAnotherEntity : IPlugin
{
private const string TARGET = "Target";
public void Execute(IServiceProvider serviceProvider)
{
//PluginSetup is an abstraction from: http://nicknow.net/dynamics-crm-2011-abstracting-plugin-setup/
var p = new PluginSetup(serviceProvider);
var target = ((Entity) p.Context.InputParameters[TARGET]).ToEntity<Account>();
var updateEntityAndXml = GetRelatedRecordAndXml(target);
var relatedContactEntity =
p.Service.Retrieve(Contact.EntityLogicalName, updateEntityAndXml.Item1, new ColumnSet(true)).ToEntity<Contact>();
UpdateContactEntityWithXml(relatedContactEntity, updateEntityAndXml.Item2);
p.Service.Update(relatedContactEntity);
}
private static void UpdateContactEntityWithXml(Contact relatedEntity, XmlDocument xmlDocument)
{
throw new NotImplementedException("UpdateContactEntityWithXml");
}
private static Tuple<Guid, XmlDocument> GetRelatedRecordAndXml(Account target)
{
throw new NotImplementedException("GetRelatedRecordAndXml");
}
}