Hi Guys,
I added a custom field to my account entity that contains different mail domain names that are owned by the account holder.
then i wrote a plugin so when a new contact is created it looks up if a domain name matches email adress and auto assigns it to the account.
however it does not work.
i am very new to plugin dev. and don't quite understand how to apply my "_contact" variable to the CRM... i know my query works and all but i must be missing a details. can anybody help me with this?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query; using Microsoft.Xrm.Sdk.Discovery; using Microsoft.Crm.Sdk.Messages; using System.ServiceModel; namespace Assigncontacttoaccount { public class AssignAccount : IPlugin { public void Execute(IServiceProvider serviceProvider) { // Obtain the execution context from the service provider. (same as other plugin-written diff) Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)); IOrganizationServiceFactory wod_serviceFactory = null; // why is this null and in the other plugin it pulls stuff... IOrganizationService wod_CrmService = null; wod_serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); wod_CrmService = wod_serviceFactory.CreateOrganizationService(context.UserId); if ((context.MessageName == "Create") && (context.PrimaryEntityName == "contact") && (context.InputParameters.Contains("Target")) && (context.InputParameters["Target"] is Entity)) { Entity _contact = (Entity)context.InputParameters["Target"]; //Make sure there is an email address on the contact and it's not already being tied to an Account if ((_contact.Contains("emailaddress1")) && (!_contact.Contains("parentcustomerid"))) { string _email = (string)_contact["emailaddress1"]; string[] _domain = _email.Split('@'); if (_domain.Length == 2) { QueryExpression _query = new QueryExpression(); _query.NoLock = true; _query.EntityName = "account"; _query.ColumnSet = new ColumnSet(); _query.ColumnSet.Columns.Add("name"); _query.Distinct = true; _query.Criteria = new FilterExpression(); _query.Criteria.FilterOperator = LogicalOperator.And; _query.Criteria.AddCondition("new_linkeddomain", ConditionOperator.Like, "%"+ _domain[1].ToString()+ "%"); EntityCollection _accounts = (EntityCollection)wod_CrmService.RetrieveMultiple(_query); if ((_accounts != null) && (_accounts.Entities.Count > 0)) { _contact.Attributes.Add("Jobtitle", _accounts.Entities[0].Attributes["name"].ToString()); _contact.Attributes.Add("parentcustomerid", new EntityReference(_accounts.Entities[0].LogicalName, _accounts.Entities[0].Id)); context.InputParameters["Target"] = _contact; } } } } } } }
*This post is locked for comments