Tuesday, October 19, 2010

Create CRM plugin to Create Address After Account Object Create CRM 4

namespace AccountCreatePlugin
{
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.account
.ToString()) { return; }
}
else
{
return;
}

try
{

//Create Address Object
customeraddress address = new customeraddress();
address.line1 = "Line 1";
address.line2 = "Line 2";
address.city = "City";
address.stateorprovince = "State";
address.postalcode = "Zip";
address.country = "Sri lanka";



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

address.parentid = new Lookup();
address.parentid.type = EntityName.account.ToString();
address.parentid.Value = regardingobjectid;

//sets the objecttypecode value to that of the account system
address.objecttypecode = new EntityNameReference();

address.objecttypecode.Value = EntityName.account.ToString();

}

ICrmService service = (ICrmService)context.CreateCrmService(true);
service.Create(address);



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


}


}

No comments:

Post a Comment