Tuesday, October 19, 2010

Create CRM plugin to Update Account Object Based On Address/ CRM 4

public class AccountCreatePlugin : IPlugin
{
public void Execute(IPluginExecutionContext context)
{
DynamicEntity entity = null;

// Check if the input parameters property bag contains a target
// of the create operation and that target is of type DynamicEntity.
if (context.InputParameters.Properties.Contains("Target") &&
context.InputParameters.Properties["Target"] is DynamicEntity)
{
// Obtain the target business entity from the input parmameters.
entity = (DynamicEntity)context.InputParameters.Properties["Target"];

// Verify that the entity represents an account.
if (entity.Name != EntityName
.customeraddress
.ToString()) { return; }
}
else
{
return;
}

try
{
//Get Address ID
if (context.OutputParameters.Properties.Contains("id"))
{
Guid regardingobjectid =
new Guid(context.OutputParameters.Properties["id"].ToString());
string regardingobjectidType = EntityName.customeraddress.ToString();

ColumnSet accountColumns = new ColumnSet();
accountColumns.AddColumn("city");
accountColumns.AddColumn("country");
// accountColumns.AddColumn("Account_CustomerAddress");
ICrmService service = (ICrmService)context.CreateCrmService(true);
customeraddress crmaddress =
(customeraddress)service.Retrieve(regardingobjectidType, regardingobjectid, accountColumns);

//Check Address is Belong to Account

if (crmaddress.objecttypecode.Value == EntityName.account.ToString())
{

ColumnSet accountColumns2 = new ColumnSet();
accountColumns.AddColumn("address1_city");

regardingobjectidType = crmaddress.objecttypecode.Value;
regardingobjectid = crmaddress.parentid.Value;

account crmaccount =
(account)service.Retrieve(regardingobjectidType, regardingobjectid, accountColumns2);

//Update Account Object
crmaccount.address1_city = crmaddress.city;

service.Update(crmaccount);



}

}





}
catch (System.Web.Services.Protocols.SoapException ex)
{
throw new Exception("An error occurred in the AccountCreateHandler plug-in.", ex);
}

No comments:

Post a Comment