In this example , i will display the number of calls made to an account .Number of calls will display on an account as soon as Sales or marketing people complete the phone calls.
I will make a new read only field on account Form “Total number of calls(new_totalnumberofcalls)” as shown in below figure.
Figure “Call made to an account
Figure = Displaying Total number of calls
Plugin will be registered as ” SetStateDynamicEntity”.
Code :
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace Microsoft.Crm.Sdk.UpdatingAccount
{
public class endless : IPlugin
{
// assigning default value for total number of calls
int NumberOfCalls= 1;
public void Execute(IPluginExecutionContext context)
{
if (context.PrimaryEntityName == EntityName.phonecall.ToString() && context.InputParameters.Properties.Contains(“EntityMoniker”) && context.InputParameters.Properties.Contains(“State”))
{
// I will get the current Phone call Id and its state
Guid PhoneCallId = ((Moniker)context.InputParameters.Properties["EntityMoniker"]).Id;
PhoneCallState stateCode = (PhoneCallState)Enum.Parse(typeof(PhoneCallState), ((string)context.InputParameters.Properties["State"]));
// Only update the regarding account ‘s field , if the phone call is “completed”
if (stateCode == PhoneCallState.Completed)
{
ICrmService service = context.CreateCrmService(false);
phonecall myphonecall = (phonecall)service.Retrieve(EntityName.phonecall.ToString(),
PhoneCallId, new AllColumns());
string regardingType = myphonecall.regardingobjectid.type;
Guid regardingId = myphonecall.regardingobjectid.Value;
// Invalid customer type. then return .
if (regardingType != EntityName.account.ToString())
{
return;
}
// Retrieve the Account’s traget field
TargetRetrieveAccount target = new TargetRetrieveAccount();
target.EntityId = regardingId;
RetrieveRequest getAccount = new RetrieveRequest(); getAccount.ReturnDynamicEntities = true;
getAccount.Target = target;
getAccount.ColumnSet = new AllColumns();
// Get the account
RetrieveResponse retrieved = (RetrieveResponse)service.Execute(getAccount);
DynamicEntity regardingAccount = (DynamicEntity)retrieved.BusinessEntity;
// If the “Total number of call” exists, update them ,otherwise set it
if (regardingAccount.Properties.Contains(“new_totalnumberofcalls”))
{
((CrmNumber)regardingAccount.Properties["new_totalnumberofcalls"]).Value += NumberOfCalls;
}
else
{
regardingAccount.Properties.Add(new CrmNumberProperty(“new_totalnumberofcalls”, new CrmNumber(NumberOfCalls))); }
// Updateing
service.Update(regardingAccount);
} }
}
}
}
Recent Comments