Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Lookup contact using email address?

(0) ShareShare
ReportReport
Posted on by 405

Hello, I have a custom entity with a Contact lookup field, and another field that contains an email address. The records get created automatically and include the email address but not the Contact name. How can I get CRM to automatically populate the Contact field using the email address? If I have the form open, I can just copy/paste the email address into the Contact field and it will automatically resolve. But I'd like that to happen via workflow or some plugin that's already been created. I do not know js or any other coding language. Suggestions?

I am using CRM Online 2016 with update 1 (8.1.x).

*This post is locked for comments

  • Suggested answer
    Nadeeja Bomiriya Profile Picture
    Nadeeja Bomiriya 6,804 on at
    RE: Lookup contact using email address?

    Hi Franco,

    You can create a custom workflow activity which takes a FetchXML query as an input and returns a Contact as an EntityReference, which then can be used to associate with your custom entity record.  You can pass the email address as an argument to the custom workflow activity.  

    public class GetContact : CodeActivity
    {
        [RequiredArgument]
        [Input("FetchXML Query")]
        public InArgument<string> FetchXmlQuery { get; set; }
    
        [Input("FetchXML Query Format Argument 1")]
        public InArgument<string> FetchXmlQueryFormatArg1 { get; set; }
    
        [Input("FetchXML Query Format Argument 2")]
        public InArgument<string> FetchXmlQueryFormatArg2 { get; set; }
    
        [Input("FetchXML Query Format Argument 3")]
        public InArgument<string> FetchXmlQueryFormatArg3 { get; set; }
    
        [Input("FetchXML Query Format Argument 4")]
        public InArgument<string> FetchXmlQueryFormatArg4 { get; set; }
    
        [Input("FetchXML Query Format Argument 5")]
        public InArgument<string> FetchXmlQueryFormatArg5 { get; set; }
    
        [ReferenceTarget("contact")]
        [Output("Entity Reference")]
        public OutArgument<EntityReference> EntityRef { get; set; }
    
        [Output("Successful")]
        public OutArgument<bool> Successful { get; set; }
    
        [Output("Result Count")]
        public OutArgument<int> Count { get; set; }
    
        [Output("Error")]
        public OutArgument<string> Error { get; set; }
    
        protected override void Execute(CodeActivityContext context)
        {
            var entityReference = new EntityReference();
    
            try
            {
                var serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
                var service = serviceFactory.CreateOrganizationService(Guid.Empty); //Use current user's ID
    
                var collection = service.RetrieveMultiple(new FetchExpression(GetFormattedFetchQuery(context)));
    
                if (collection != null && collection.Entities != null && collection.Entities.Count() > 0)
                {
                    entityReference = collection.Entities.FirstOrDefault().ToEntityReference();
                    EntityRef.Set(context, entityReference);
                    Successful.Set(context, true);
                    Count.Set(context, collection.Entities.Count());
                    Error.Set(context, string.Empty);
                }
                else if (collection != null && collection.Entities != null && collection.Entities.Count() == 0)
                {
                    EntityRef.Set(context, null);
                    Successful.Set(context, false);
                    Count.Set(context, 0);
                    Error.Set(context, "No records found");
                }
                else
                {
                    EntityRef.Set(context, null);
                    Successful.Set(context, false);
                    Count.Set(context, 0);
                    Error.Set(context, "Collection or Collection.Entitites is null");
                }
            }
            catch (Exception ex)
            {
                EntityRef.Set(context, null);
                Successful.Set(context, false);
                Count.Set(context, 0);
                Error.Set(context, ex.Message);
            }
        }
    
        private string GetFormattedFetchQuery(CodeActivityContext context)
        {
            var query = this.FetchXmlQuery.Get(context);
            return String.Format(query,
                this.FetchXmlQueryFormatArg1.Get(context),
                this.FetchXmlQueryFormatArg2.Get(context),
                this.FetchXmlQueryFormatArg3.Get(context),
                this.FetchXmlQueryFormatArg4.Get(context),
                this.FetchXmlQueryFormatArg5.Get(context));
        }
    }
    

    This idea was inspired by below article.

    https://bernado-nguyen-hoan.com/2015/05/16/custom-workflow-activity-to-process-multiple-related-records-crm/

    You can extend this to include other error criteria such as duplicate record scenarios, which you can handle within the workflow.

    Cheers,

    Nadeeja

    If the answer solves your problem, please mark as Verified. Thanks.

    My Blog: http://dyn365apps.com/ - Follow me on Twitter: https://twitter.com/dyn365apps

    LinkedIn: https://www.linkedin.com/in/nadeeja

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Lookup contact using email address?

    Hi Franco,

    You can write a custom workflow for this. In the example shown below, I update the primary contact email address related to the account if the account email address is given during creation of account or if it changes.

    Please refer the screenshots below.

    Screenshot-_2800_66_2900_.png

    Screenshot-_2800_66_2900_.png

    For another example related to this scenario, please refer the link below.

    www.powerobjects.com/.../retrieving-data-from-a-related-entity-crm-2013/

    Hope this helps you.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans