我正在尝试进行一项将地址从潜在客户迁移到联系人的活动。我们在 CRM 部署中不使用默认地址 1 和地址 2(不是我的决定),因此尽管资格认证过程确实将在线索中输入的地址复制到联系人,但它使用地址 1 字段这样做。我正在使用下面的代码,一切似乎都正常(没有错误注册,没有错误运行使用此活动的工作流)。只有一个问题……什么都没有发生。虽然没有错误,但没有创建地址。我以 CRM 管理员的身份运行,所以这不应该是权限问题,但是即使它不应该产生安全异常?任何想法为什么这不起作用?
public class MigrateLeadAddressToContactActivity : CodeActivity
{
[Input("Contact input")]
[ReferenceTarget("contact")]
public InArgument<EntityReference> InContact { get; set; }
protected override void Execute(CodeActivityContext executionContext)
{
// Get the tracing service
var tracingService = executionContext.GetExtension<ITracingService>();
if (InContact == null)
{
const string errorMessage = "Contact was not set for Address Migration Activity";
tracingService.Trace(errorMessage);
throw new InvalidOperationException(errorMessage);
}
// Get the context service.
var context = executionContext.GetExtension<IWorkflowContext>();
var serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
// Use the context service to create an instance of CrmService.
var service = serviceFactory.CreateOrganizationService(context.UserId);
//Retrieve the contact id
var contactId = this.InContact.Get(executionContext).Id;
// Get The Lead if it exists
var query = new QueryByAttribute
{
ColumnSet = new ColumnSet(
new[]
{
"address1_line1",
"address1_line2",
"address1_line3",
"address1_city",
"address1_stateorprovince",
"address1_postalcode",
"address1_country",
}
),
EntityName = "lead"
};
// The query will retrieve all leads whose associated contact has the desired ContactId
query.AddAttributeValue("customerid", contactId);
// Execute the retrieval.
var results = service.RetrieveMultiple(query);
var theLead = results.Entities.FirstOrDefault();
if (null == theLead)
{
tracingService.Trace("Activity exiting... Contact not sourced from Lead.");
return;
}
var newAddress = new Entity("customeraddress");
newAddress.Attributes["name"] = "business";
newAddress.Attributes["objecttypecode"] = "contact";
newAddress.Attributes["addresstypecode"] = 200000;
newAddress.Attributes["parentid"] = new CrmEntityReference("contact", contactId);
newAddress.Attributes["line1"] = theLead.Attributes["address1_line1"];
newAddress.Attributes["line2"] = theLead.Attributes["address1_line2"];
newAddress.Attributes["line3"] = theLead.Attributes["address1_line3"];
newAddress.Attributes["city"] = theLead.Attributes["address1_city"];
newAddress.Attributes["stateorprovince"] = theLead.Attributes["address1_stateorprovince"];
newAddress.Attributes["postalcode"] = theLead.Attributes["address1_postalcode"];
newAddress.Attributes["country"] = theLead.Attributes["address1_country"];
service.Create(newAddress);
tracingService.Trace("Address Migrated from Contact to Lead.");
}