web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
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

I have the same question (0)
  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    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.

  • Suggested answer
    Nadeeja Bomiriya Profile Picture
    6,804 on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans