public class Plugin IPlugin private string crmUrl public Plugin string

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
public class Plugin : IPlugin
{
private string crmUrl;
public Plugin(string unsecure, string secure)
{
crmUrl = unsecure;
}
public void Execute(IPluginExecutionContext context)
{
Microsoft.Crm.Sdk.DynamicEntity entity = null;
#region Verify execution context
// 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 Microsoft.Crm.Sdk.DynamicEntity)
{
// Obtain the target business entity from the input parmameters.
entity = (Microsoft.Crm.Sdk.DynamicEntity)context.InputParameters.Properties["Target"];
// Verify that the entity represents an contact.
if (entity.Name != ContactPostNumeratorPlugIn.CRM.EntityName.contact.ToString()) { return; }
}
else
{
return;
}
#endregion Verify execution context
try
{
// Set up the CRM Service.
ContactPostNumeratorPlugIn.CRM.CrmAuthenticationToken token = new ContactPostNumeratorPlugIn.CRM.CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = context.OrganizationName;
ContactPostNumeratorPlugIn.CRM.CrmService service = new ContactPostNumeratorPlugIn.CRM.CrmService();
service.Url = crmUrl;
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
QueryExpression query = new QueryExpression();
ColumnSet cols = new ColumnSet();
cols.Attributes = new string[] { "new_donornumber" };
query.EntityName = ContactPostNumeratorPlugIn.CRM.EntityName.new_numerator.ToString();
query.ColumnSet = cols;
//The RetrievedMultiple method was used (and not the regular Retrieve method)
//since it does not require the Guid of the target entity
ContactPostNumeratorPlugIn.CRM.BusinessEntityCollection numerators = service.RetrieveMultiple(query);
new_numerator retrievedNumerator = (new_numerator)numerators.BusinessEntities[0];
// Refer to the contactid
if (context.OutputParameters.Properties.Contains("id"))
{
//Updates the numerator entity
if (retrievedNumerator.new_donornumber == null)
retrievedNumerator.new_donornumber = "0";
int donorNtor = int.Parse(retrievedNumerator.new_donornumber);
donorNtor++;
retrievedNumerator.new_donornumber = donorNtor.ToString();
//update numerator entity
service.Update(retrievedNumerator);
//update contact entity
Guid contactid = new Guid(context.OutputParameters.Properties["id"].ToString());
ContactPostNumeratorPlugIn.CRM.contact contact = new ContactPostNumeratorPlugIn.CRM.contact();
contact.contactid = new ContactPostNumeratorPlugIn.CRM.Key();
contact.contactid.Value = contactid;
contact.new_donorno = donorNtor.ToString();
service.Update(contact);
}
}
catch (System.Web.Services.Protocols.SoapException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the ContactNumCreateHandler plug-in.", ex);
}
}
}