Automatically map a Contact to an Account - dynamics-crm

I want to add a field to Accounts which shows the email domain for that account e.g. #BT.com. I then have a spreadsheet which lists all the Accounts and their email domains. What I want to do is when a new Contact is added to Dynamics that it checks the spreadsheet for the same email domain (obviously without the contacts name in the email) and then assigned the Contact to the Account linked to that domain. Any idea how I would do this. Thanks

Probably best chance would be to develop CRM plugin. Register your plugin to be invoked when on after contact is created or updated (so called post-event phase). And in your plugin update the parentaccountid property of the contact entity to point to account of your choice.
Code-wise it goes something like (disclaimer: not tested):
// IPluginExecutionContext context = null;
// IOrganizationService organizationService = null;
var contact = (Entity)context.InputParameters["Target"];
var email = organizationService.Retrieve("contact", contact.Id, new ColumnSet("emailaddress1")).GetAttributeValue<string>("emailaddress1");
string host;
try
{
var address = new MailAddress(email);
host = address.Host;
}
catch
{
return;
}
var query = new QueryExpression("account");
query.TopCount = 1;
// or whatever the name of email domain field on account is
query.Criteria.AddCondition("emailaddress1", ConditionOperator.Contains, "#" + host);
var entities = organizationService.RetrieveMultiple(query).Entities;
if (entities.Count != 0)
{
contact["parentaccountid"] = entities[0].ToEntityReference();
}
organizationService.Update(contact);

I took Ondrej's code and cleaned it up a bit, re-factored for pre-operation. I also updated the logic to only match active account records and moved the query inside the try/catch. I am unfamiliar with the MailAddress object, I personally would just use string mapping logic.
var target = (Entity)context.InputParameters["Target"];
try
{
string host = new MailAddress(target.emailaddress1).Host;
var query = new QueryExpression("account");
query.TopCount = 1;
// or whatever the name of email domain field on account is
query.Criteria.AddCondition("emailaddress1", ConditionOperator.Contains, "#" + host);
query.Criteria.AddCondition("statecode", ConditionOperator.Equals, 0); //Active records only
var entities = organizationService.RetrieveMultiple(query).Entities;
if (entities.Count != 0)
{
target["parentaccountid"] = entities[0].ToEntityReference();
}
}
catch
{
//Log error
}

Related

Check if current user is a member of exchange distribution list - Outlook C#

I want to find out if current Outlook user is a member of particular exchange distribution list. If he is, then he should see child form and if he isn't; then he should see message box.
My following code is working up to the point, if user is a member of DistList, he get child form but I don't know how to check show him message box if he isn't member.
string UserName = (string)application.ActiveExplorer().Session.CurrentUser.Name;
string PersonalPublicFolder = "Public Folders - " + application.ActiveExplorer().Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
Outlook.MAPIFolder contactsFolder = outlookNameSpace.Folders[PersonalPublicFolder].Folders["Favorites"];
Outlook.DistListItem addressList = contactsFolder.Items["ContactGroup"];
if (addressList.MemberCount != 0)
{
for (int i = 1; i <= addressList.MemberCount; i++)
{
Outlook.Recipient recipient = addressList.GetMember(i);
string contact = recipient.Name;
if (contact == UserName)
{
var assignOwnership = new AssignOwnership();
assignOwnership.Show();
}
}
}
Any help would be appreciated.
Thank you.
Use Application.Session.CurrentUser.AddressEntry.GetExchangeUser().GetMemberOfList() - it will return AddressEntries object that contains all DLs that the user is a member of.
Be prepared to handle nulls and errors.

eror when retrieve createdby field in crm using plugin

I try to retrieve the createdby field when I create a cases with a plugin, but the first retrieval fails, and the second and subsequent retrieval are successful. And then when I logged out and login with other user the first retrieval fails (retrieve result is the user before i change the user), and the second and subsequent retrieval are successful.
here is the code i write :
public void Execute(IServiceProvider serviceProv)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProv.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory servicefac = (IOrganizationServiceFactory)serviceProv.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = servicefac.CreateOrganizationService(context.UserId);
ITracingService trace = (ITracingService)serviceProv.GetService(typeof(ITracingService));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity ent = (Entity)context.InputParameters["Target"];
if (ent.LogicalName != "incident")
return;
QueryExpression qe = new QueryExpression("incident");
string[] cols1 = { "createdby" };
qe.ColumnSet = new ColumnSet(true);
EntityCollection ec = service.RetrieveMultiple(qe);
foreach (Entity act in ec.Entities)
{
created = act. GetAttributeValue<EntityReference>("createdby").Name;
}
if (created == "CRM SNA")
{
created = string.Empty;
}
else
{
//here is the autonumber code
created = string.Empty;
}
}
}
What I want to make is an autonumber plugin, when cases are created by "CRM SNA" then the autonumber must not run, when cases are created by other users the autonumber will run.
How to make the first retrieve successful? and did not retrieve the user before?
thanks.
I assume your plugin runs in the Pre-Create step. CreatedBy and CreatedOn are not available in this step (probably because the record is not saved yet).
If you are just trying to get the user that executed the action that fired the plugin, use context.InitiatingUserId. You could also look into the documentation for the WhoAmI request.
Hope that helps!

(Windows Phone 10) Is possible to edit, add new contact programmatically in windows phone 10?

I want to implement function edit and add contact programatically in windows phone 10.
Is it possible? Has any sample about it ?
Here is a code snippet for creating the contact:
public async Task AddContact(String FirstName, String LastName)
{
var contact = new Windows.ApplicationModel.Contacts.Contact();
contact.FirstName = FirstName;
contact.LastName = LastName;
//Here you can set other properties...
//Get he contact store for the app (so no lists from outlook and other stuff will be in the returned lists..)
var contactstore = await Windows.ApplicationModel.Contacts.ContactManager.RequestStoreAsync(Windows.ApplicationModel.Contacts.ContactStoreAccessType.AppContactsReadWrite);
try
{
var contactLists = await contactstore.FindContactListsAsync();
Windows.ApplicationModel.Contacts.ContactList contactList;
//if there is no contact list we create one
if (contactLists.Count == 0)
{
contactList = await contactstore.CreateContactListAsync("MyList");
}
//otherwise if there is one then we reuse it
else
{
contactList = contactLists.FirstOrDefault();
}
await contactList.SaveContactAsync(contact);
}
catch
{
//Handle it properly...
}
}
And here is a short sample for changing an existing contact:
//you can obviusly couple the changes better then this... this is just to show the basics
public async Task ChangeContact(Windows.ApplicationModel.Contacts.Contact ContactToChange, String NewFirstName, String NewLastName)
{
var contactStore = await Windows.ApplicationModel.Contacts.ContactManager.RequestStoreAsync(Windows.ApplicationModel.Contacts.ContactStoreAccessType.AppContactsReadWrite);
var contactList = await contactStore.GetContactListAsync(ContactToChange.ContactListId);
var contact = await contactList.GetContactAsync(ContactToChange.Id);
contact.FirstName = NewFirstName;
contact.LastName = NewLastName;
await contactList.SaveContactAsync(contact);
}
And very important:
In the appxmanifest you have to add the contacts capability. Right click to it in the solution explorer and "View Code" and then under Capabilities put
<uap:Capability Name="contacts" />
There is no UI for this. See this.
Both samples are meant to be for starting point... obviously it's not production ready and you have to adapt it to your scenario.
Update
Since this came up in the comments I extend my answer a little bit.
Based on this (plus my own experimentation) the ContactListId for aggregated contacts is null (which makes sense if you think about it). Here is how to get the raw contact with ContactlLstId (code is based on the comment from the link)
public async Task IterateThroughContactsForContactListId()
{
ContactStore allAccessStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly);
var contacts = await allAccessStore.FindContactsAsync();
foreach (var contact in contacts)
{
//process aggregated contacts
if (contact.IsAggregate)
{
//here contact.ContactListId is "" (null....)
//in this case if you need the the ContactListId then you need to iterate through the raw contacts
var rawContacts = await allAccessStore.AggregateContactManager.FindRawContactsAsync(contact);
foreach (var rawContact in rawContacts)
{
//Here you should have ContactListId
Debug.WriteLine($"aggregated, name: {rawContact.DisplayName }, ContactListId: {rawContact.ContactListId}");
}
}
else //not aggregated contacts should work
{
Debug.WriteLine($"not aggregated, name: {contact.DisplayName }, ContactListId: {contact.ContactListId}");
}
}
}
And another important thing:
According to the documentation you won’t be able to change all the contacts which are created by other apps.
AllContactsReadWrite:
Read and write access to all app and system contacts. This value is
not available to all apps. Your developer account must be specially
provisioned by Microsoft in order to request this level of access.
In some cases, I get a System.UnauthorizedAccessException when SaveContactAsync(contact) is called. One example for this was when the contact was in the Skype Contact List.

Creating a Salesforce Apex Trigger to update a lookup field in the Contacts object

I have created a trigger that will auto-create a contact when a specific Account record type is created or updated. The problem is that I am not sure how to populate the Contact 'Account Name' lookup field. This field is a lookup to the Account object. My code is below. Any help on how to integrate this missing component would be greatly appreciated.
trigger autoCreateContact on Account (after update, after insert)
{
List newContact = new List();
for (Account oAccount : trigger.new)
{
if (oAccount.RecordTypeid == '012F0000001MCfgIAG')
{
List<Contact> cCheck = [SELECT ID From Contact WHERE LastName=:oAccount.Name];
if(cCheck.isEmpty()==True)
{
System.debug(oAccount);
Contact oContact = new Contact();
oContact.LastName = oAccount.Name;
oContact.phone = oAccount.Phone;
oContact.email = oAccount.Email__c;
oContact.Owner = oAccount.Owner;
newContact.add(oContact);
}
}
if(newContact.isEmpty() == false)
{
Database.insert(newContact);
}
}
}
nice trigger I'm pretty sure you just need to add one line which is a reference to the account.id.
So if I were you I would add the link:
oContact.AccountID = oAccount.id;
NOTE: its not a good practice to have a SOQL inside the for loop.

CRM 2011 accessing webcontext with outlook plugin

I have found some plugin code on the web that enables me to get the entity ID and the object type code for an entity in a plugin. The plugin is fired on RetrieveMultiple on activitypointer. The code lets me get the id and object code of the entity that is currently being viewed (which is displaying the activities grid which is firing the plugin).
This code works fine when using the web interface. However I need it to also work in the Outlook preview pane and currently it does not. The activities grid in the Outlook preview pane just says "an error has occurred". Below is the code that the plugin is using to get the details from the web header.
internal static Dictionary<string, string> GetHeaderFields(HttpContext webcontext, string objectTypeCode, string objectId)
{
Dictionary<string, string> fields = new Dictionary<string, string>();
string callerentitytype = null;
string callerentityidstring = null;
try
{
// Activities Navigation Pane
if (new List<string>(webcontext.Request.Params.AllKeys).Contains("oType"))
{
callerentitytype = webcontext.Request.Params["oType"];
callerentityidstring = webcontext.Request.Params["oId"];
}
// Activities Sub Grid
else
{
string requeststring = webcontext.Request.UrlReferrer.Query;
requeststring = requeststring.Substring(1);
string[] parts = requeststring.Split(new string[] { "=", "&" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < parts.Length - 1; i++)
if (parts[i].ToLower() == "otype" || parts[i].ToLower() == "etc")
callerentitytype = parts[i + 1];
else if (parts[i].ToLower() == "oid" || parts[i].ToLower() == "id")
callerentityidstring = parts[i + 1];
}
fields.Add(objectTypeCode, callerentitytype);
fields.Add(objectId, callerentityidstring);
}
catch (Exception ex)
{
throw new Plugin.LoggableException(string.Format("Failed to obtain header information; {0}", ex.Message), ex.InnerException);
}
return fields;
}
The reason is that webcontext.Request.UrlReferrer is NULL. Is there anywhere else I can get this info of the 'calling' entity? (Not the activity sub grid that is triggering the plugin, but the actual parent entity that the sub grid is on).
Thanks for any help or direction with this.
This might work. Each of the activitypointers that are returned should all be "regarding" the same record (if in a sub grid). If you take say the 1st one and examine the regardingobjectid property, that should be an entity reference which will give you the logical name of the parent and it's guid. If that works, it will work across all clients (in theory anyway).

Resources