Note that there are some explanatory texts on larger screens.

plurals
  1. POCRM create CustomerAddress Programmatically
    text
    copied!<p>I am trying to make an activity that migrates an address from a lead to a contact. We do not use the default Address1 and Address2 in our CRM deployment (not my decision) so although the Qualification process does copy the address entered in the lead to the contact, it does so using the Address1 fields. I am using the code below, and everything seems to be working (no errors registering, no errors running the workflow that uses this activity). There's just one problem... nothing happens. Although there are no errors, no address gets created. I am running as the CRM admin so this shouldn't be a permissions thing, however even if it was shouldn't that generate a security exception? Any ideas why this is not working?</p> <pre class="lang-scharp prettyprint-override"><code>public class MigrateLeadAddressToContactActivity : CodeActivity { [Input("Contact input")] [ReferenceTarget("contact")] public InArgument&lt;EntityReference&gt; InContact { get; set; } protected override void Execute(CodeActivityContext executionContext) { // Get the tracing service                         var tracingService = executionContext.GetExtension&lt;ITracingService&gt;(); 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&lt;IWorkflowContext&gt;(); var serviceFactory = executionContext.GetExtension&lt;IOrganizationServiceFactory&gt;(); // 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."); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload