using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace ClassLibrary3
{
public class Class2 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Extract the tracing service for use in debugging sandboxed plug-ins.
// If you are not registering the plug-in in the sandbox, then you do
// not have to add any tracing service related code.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// Obtain the organization service reference which you will need for
// web service calls.
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// The InputParameters collection contains all the data passed in the message request.
if (context.MessageName.ToLower() == "update")
{
var target = context.InputParameters["Target"] as Entity;
Entity eAccount = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet("address1_line1", "address1_line2", "address1_line3", "address1_city", "address1_stateorprovince", "address1_postalcode", "address1_country"));
string new2 = eAccount.Attributes.Contains("address1_city") ? eAccount.GetAttributeValue<string>("address1_city") : String.Empty;
string new3 = eAccount.Attributes.Contains("address1_stateorprovince") ? eAccount.GetAttributeValue<string>("address1_stateorprovince") : String.Empty;
string new4 = eAccount.Attributes.Contains("address1_line1") ? eAccount.GetAttributeValue<string>("address1_line1") : String.Empty;
string new5 = eAccount.Attributes.Contains("address1_line2") ? eAccount.GetAttributeValue<string>("address1_line2") : String.Empty;
string new6 = eAccount.Attributes.Contains("address1_line3") ? eAccount.GetAttributeValue<string>("address1_line3") : String.Empty;
string new7 = eAccount.Attributes.Contains("address1_postalcode") ? eAccount.GetAttributeValue<string>("address1_postalcode") : String.Empty;
string new8 = eAccount.Attributes.Contains("address1_country") ? eAccount.GetAttributeValue<string>("address1_country") : String.Empty;
string FetchAssociatedContacts = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='contact'>" +
"<attribute name='fullname'/>" +
"<attribute name='contactid' />" +
"<attribute name='address1_line1' />" +
"<attribute name='address1_line2' />" +
"<attribute name='address1_line3' />" +
"<attribute name='address1_postalcode' />" +
"<attribute name='address1_city' />" +
"<attribute name='address1_stateorprovince' />" +
"<attribute name='address1_country' />" +
"<order attribute='fullname' descending='false' />" +
"<filter type='and'>" +
"<condition attribute='parentcustomerid' operator='eq' value='" + target.Id + @"'/>" +
"</filter>" +
"</entity>" +
"</fetch>";
EntityCollection AssociatedContacts = service.RetrieveMultiple(new FetchExpression(FetchAssociatedContacts));
if (AssociatedContacts.Entities.Count>=0)
{
foreach (var item in AssociatedContacts.Entities)
{
Entity Company = new Entity(item.LogicalName,item.Id);
Company["address1_line1"] = new4;
Company["address1_line2"] = new5;
Company["address1_line3"] = new6;
Company["address1_postalcode"] = new7;
Company["address1_city"] = new2;
Company["address1_stateorprovince"] = new3;
Company["address1_country"] = new8;
service.Update(Company);
}
}
}
}
}
}
Update Contact address fields Associated to Accounts when account fields are updated using Java Script.
function UpdateAddress(executionContext){
var formContext = executionContext.getFormContext();
var Add1=formContext.getAttribute(“address1_line1”).getValue();
var Add2=formContext.getAttribute(“address1_line2”).getValue();
var Add3=formContext.getAttribute(“address1_line3”).getValue();
var city=formContext.getAttribute(“address1_city”).getValue();
var state_province=formContext.getAttribute(“address1_stateorprovince”).getValue();
var postal=formContext.getAttribute(“address1_postalcode”).getValue();
var country=formContext.getAttribute(“address1_country”).getValue();
var GUID=formContext.data.entity.getId();
Xrm.WebApi.online.retrieveMultipleRecords(“contact”, “?$filter=_parentcustomerid_value eq ‘” + GUID + “‘”).then(
function success(results) {
for (var i = 0; i < results.entities.length; i++) {
var contactid = results.entities[i][“contactid”];
var data =
{
“address1_line1”: Add1,
“address1_line2”: Add2,
“address1_line3”: Add3,
“address1_city”: city,
“address1_stateorprovince”: state_province,
“address1_postalcode”: postal,
“address1_country”: country
}
Xrm.WebApi.online.updateRecord("contact",contactid, data).then(
function success(result) {
Xrm.Utility.alertDialog("Data Updated");
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);
}
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);
}