How to – Clone a record in Dataverse / Dynamics 365 (C#)


Here we will be cloning the below contact record –

For it, we can make use Clone method of EntityExtensions class

If we run the below code, we will get the “Cannot insert duplicate key” exception.

Because we need to set the ID for the newly cloned record to either empty or a new GUID.

Also, we need to remove the contactid attribute or entity’s id field else we will get the below exception – “Entity Id must be the same as the value set in property bag

Also if we are retrieving all the attributes we need to make sure we remove those attribute which can cause below unique constraint errors.

Sql error: The operation attempted to insert a duplicate value for an attribute with a unique constraint. CRM ErrorCode: -2147012606 Sql ErrorCode: -2146232060 Sql Number: 2627

In case of contact, we had to remove the address id attributes, to get the record cloned.

Now let us clone the same contact record with its related leads.

  • Here first we need to retrieve contact along with its associated lead record.
  • Then remove the id attribute that could duplicate issues in the child lead records also.

Please refer the helpful posts –

https://debajmecrm.com/clone-record-dynamics-crm-clone-method/

https://www.inogic.com/blog/2014/08/clone-records-in-dynamics-crm/

https://dyncrmexp.com/2017/10/24/retrieve-primary-entities-along-with-related-entities-with-one-queryexpression/comment-page-1/

https://dynamict.eu/2016/08/09/create-record-with-related-records-in-a-single-request-using-c/

Hope it helps..

 // related lead records of contact
                    QueryExpression query = new QueryExpression("lead");                    
                    query.ColumnSet = new ColumnSet(true);

                    // contact and lead - 1 - n relationship
                    Relationship relationship = new Relationship("lead_parent_contact");
                    relationship.PrimaryEntityRole = EntityRole.Referenced;

                    RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection();
                    relatedEntity.Add(relationship, query);

                    RetrieveRequest request = new RetrieveRequest();
                    request.RelatedEntitiesQuery = relatedEntity;
                    request.ColumnSet = new ColumnSet(true);
                    request.Target = new EntityReference("contact", new Guid("ca0aa844-57c6-ec11-a7b6-00224826af1e"));

                    RetrieveResponse response = (RetrieveResponse)svc.Execute(request);
                    var contactToBeCloned = response.Entity;                   
                    var clonedContact = contactToBeCloned.Clone(true);

                    // remove attribute that can cause error from main entity             
                    clonedContact.Id = Guid.NewGuid();
                    clonedContact.Attributes.Remove("contactid");                    
                    clonedContact.Attributes.Remove("address1_addressid");
                    clonedContact.Attributes.Remove("address2_addressid");
                    clonedContact.Attributes.Remove("address3_addressid");

                    // remove attribute that can cause error from child / related entities
                    foreach (var relatedEntities in clonedContact.RelatedEntities)
                    {
                        foreach(var leadEntity in relatedEntities.Value.Entities)
                        {
                            leadEntity.Id = Guid.NewGuid();
                            leadEntity.Attributes.Remove("leadid");
                            leadEntity.Attributes.Remove("address1_addressid");
                            leadEntity.Attributes.Remove("address2_addressid");
                        }
                    }
                    svc.Create(clonedContact);
Advertisements

Environment Variable referencing Azure Key Vault secrets (Dataverse / Power Platform)


Check out the below articles –

https://docs.microsoft.com/en-us/power-apps/maker/data-platform/environmentvariables#use-azure-key-vault-secrets-preview

Within plugin https://itmustbecode.com/azure-key-vault-secrets-in-dataverse/

Within Power Automatehttps://www.msautomate.com/2022/06/16/azure-keyvault-and-power-automate/

https://powerapps.microsoft.com/en-us/blog/how-to-use-environment-variables-with-akv-secrets-in-the-ci-cd-pipeline-when-deploying-applications/

https://blog.yannickreekmans.be/environment-variable-secrets-azure-key-vault-improvement/

Advertisements

Filter rows and Trigger Conditions in Power Automate


Filter rows and Trigger Conditions can be used to specify the condition on which the trigger should fire. This makes sure that we do not exceed flow execution limits.

Power Automate Licensing – Power Platform Requests

Power Automate Limits and Configuration

In the case of our Dataverse trigger “When a row is added, modified or deleted” we can make use of either Filter rows or Trigger Conditions, as we have both options available.

We’d be using Trigger Conditions for those triggers where we do not have additional property to specify the filter condition.

E.g. we want the flow to run only on a specific file in a library or based on the extension of the file as nicely explained here

https://tomriha.com/trigger-power-automate-flow-only-on-specific-file-in-a-library/

https://www.enjoysharepoint.com/trigger-conditions-in-power-automate/

Now out of curiosity, we tried specifying both Filter rows and Trigger conditions to see how it behaves.

Here for the same trigger we have specified Filter Rows as below

address1_city eq ‘Ahmedabad’

And Trigger Conditions as below

@equals(triggerOutputs()?[‘body/companyname’], ‘MS’)

During our test, we observed that the trigger is firing only when both the conditions or expressions, specified in Filter Rows as well as Trigger Conditions are true.

So basically it will only fire for those rows or records having Company as MS and City equal to Ahmedabad.

It will not fire if either of Filter Rows or Trigger Conditions expression evaluate to be false.

Hope it helps..

Advertisements

Fixed – Unable to process template language expressions for trigger. In the template language function ‘convertToUtc’, the value provided for the time zone id ‘Gulf Standard Time’ was not valid while using Delay Until property– Power Automate


Recently while using Delay until property of the trigger, in one of the flows, we got the below error

“Unable to process template language expressions for trigger ‘When_a_row_is_added,_modified_or_deleted’ at line ‘1’ and column ‘16344’: ‘In the template language function ‘convertToUtc’, the value provided for the time zone id ‘Gulf Standard Time’ was not valid. ‘.”

Usage of Delay until is nicely explained here by Debajit –

https://debajmecrm.com/how-to-delay-a-power-automate-flow-execution-till-a-specified-date-and-time/

https://debajmecrm.com/replicate-dynamics-365-workflow-timeout-wait-condition-in-microsoft-flows/

 

Delay until = convertToUtc(‘2022-05-16T17:22:00′,’Gulf Standard Time’)

It turns out that Gulf Standard Time is not a valid time zone id.

Changing it to Arabian Standard Time resolved the issue for us.

i.e.

convertToUtc(‘2022-05-16T19:22:00′,’Arabian Standard Time’)


Also check – https://nishantrana.me/2022/01/19/how-to-use-do-until-and-delay-in-power-automate/

Hope it helps..

Advertisements

Autonumber field values in case of exception in Dynamics 365 / CRM


Recently in one of our projects, where we were using the auto number attribute (with sequential numbers), we realized that it gets incremented even in case of an exception thrown during Pre Stage of the Plugin Execution. We were assuming the number gets incremented only after the record gets created i.e. on post-stage.

But as the documentation mentions, the number gets pre-selected as soon as the record is started.

https://docs.microsoft.com/en-us/power-apps/maker/data-platform/autonumber-fields

Let us see it in action also –

Here we have the lead record created with My Autonumber as the auto number column with seed as 1000

The auto number field has a value of 1009 currently.

Now let us register a plugin on the Pre-Create stage that throws the exception.

Let us try creating a new lead record, which as expected will throw the exception.

Let us try saving the record 3 times more, triggering the record creation as well as the plugin along with the exception.

Now let us disable the plugin step and save/create the record.

As expected the record gets created, and the auto number field has a value of 1014.

The same behavior was observed in case of Pre-Validation stage.

Hope it helps..

 

Advertisements

Fixed – Sorry, we need additional information to verify your identity. Please Contact Support error while creating Dynamics 365 Trial


Recently while creating a Dynamics 365 Trial, https://dynamics.microsoft.com/en-us/dynamics-365-free-trial/, we were getting the below error, during the verification step

“Sorry, we need additional information to verify your identity. Please Contact Support”

 

 

 

 

 

 

This error could be related to the same phone number being used multiple times to create the Dynamics 365 trial or could be related browser cache.

Before trying with a new phone number, we should first try creating a trial in the In-Private mode (or clear cache in the browser).

In our case, the In-Private mode worked as shown below, and we were able to create the trial.

Hope it helps..

Advertisements
%d bloggers like this: