Power Automate : Create Lead from Email with Attachment

Below are steps to create a ‘lead with attachment’ from an ‘incoming email with atachment’.

Lets trigger the power automate flow when the email is created in CRM with direction as incoming. We dont want outgoing email to be converted to lead 🙂

Here we can add further filters like excluding a domain, or checking if body contaiin certain keywords etc

To create a lead, we put the below component in ‘If Yes’ block. We will fill in the username of the email address as the lastname. This might not be preferred choice, but it can be configured as per requirements.

We are using following expression to achive this:

substring(triggerOutputs()?[‘body/sender’],0, indexOf(triggerOutputs()?[‘body/sender’],’@’))

Also, we want to store the email text to description field. Now since the description might have HTML tag which we dont want in our description field – we are using a HTML to text conversion service.

Next we want to find all the attachment linked to this email message. Here we can simply loop through attachment entity.

Please note that you will see two drop down for attachment – one is attachment and other is activitymimeattachment

We need to select activitymimeattachment. So select attachment and then right click to peek code to be sure you selected the correct one,

We are applying filter to get only the attachment of email message as shown :

Remember to use “_activityid_value” and not just “activitiyid” else you will get error message :

“Could not find a property named ‘activityid’ on type ‘Microsoft.Dynamics.CRM.activitymimeattachment’.”

Now we can loop through all the attachment and create annotation(notes) for all the files.

Please note not to fill in the “object type” with entity type when you are specifying the type in regardingobject directly else you might get following error.

The entity with a name = ‘4’ with namemapping = ‘Logical’ was not found in the MetadataCache. MetadataCacheDetails: ProviderType=Dynamic, StandardCache=True, IsLoadedInStagedContext = False, Timestamp=7862004, MinActiveRowVersion=7862004, MetadataInstanceId=21976159, LastUpdated=2021-02-06 09:25:40.947

Also the format of regardingobjectid should be /leads/GUID

This will create lead with following values :

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: