Sample Code – Capture / Record lead qualification date – Dynamics 365 / Dataverse


Recently we had a requirement to capture the date when a lead is qualified. Here we can create a new date time attribute to capture the lead qualification date and update this field either through workflow, flow, or plugin on lead qualification.

Status Code = 3 (Qualified)

We implemented a plugin on PreOperationUpdate of lead, with filtering attributes as the state code.

Below is the sample code for the plugin  –

 public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
         
            try
            {
                tracingService.Trace("start plugin execution: {0}", this.GetType().FullName);
                // plugin is in pre update stage of lead
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity &&
                    context.MessageName.Equals("update", StringComparison.OrdinalIgnoreCase) &&
                   context.Stage == 20)   
                {
                    var leadEntity = (Entity)context.InputParameters["Target"];
                    if(leadEntity.LogicalName == "lead")
                    {
                        if(leadEntity.Attributes.Contains("statuscode") && 
                            ((OptionSetValue)leadEntity.Attributes["statuscode"]).Value == 3
                            )
                        {
                            tracingService.Trace("Lead with Id {0} is getting qualified at {1}", 
                                leadEntity.Id.ToString(), DateTime.UtcNow);
								
							// the custom date time field to capture the qualification date 
                            leadEntity["custom_qualificationdate"] = DateTime.UtcNow;                         
                        }
                    }                 

                }

                tracingService.Trace("end plugin execution: {0}", this.GetType().FullName);
            }
            catch (System.ServiceModel.FaultException<OrganizationServiceFault> ex)
            {
                tracingService.Trace(ex.Detail.Message);
                throw;
            }
            catch (Exception ex)
            {
                tracingService.Trace(ex.ToString());
                throw;
            }
        }

Hope it helps..

Advertisements

Retrieve primary entities along with related entities with one QueryExpression


Retrieve related records for a parent record

It’s possible to get related records of  a relationship when we retrieve a parent record using RelatedEntitiesQuery property in the RetrieveRequest. That means you have to construct the RetrieveRequest not using the Retrieve method from the OrganizationServiceProxy class.

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

by Khoa Nguyen
Advertisements

How to – Filter Collection / Items in Gallery based on the related (Lookup) record’s value – Power Apps / Canvas Apps (Dataverse/Dynamics 365)


Suppose we have 3 tables, Table A, Table B, and Table C related to each other as

Table A (n-1) Table B (n-1) Table C i.e.

Table A has a lookup of Table B and Table B has a lookup of Table C.

Below is our sample canvas app that has a gallery and a combo box.

What we would want is based on the Table C record’s name selected in the combo box, we want to filter the collection to only show those records that are related to it.

One of the ways we could implement this is by using the AddColumns to add Table C data to Table A and apply the Filter to it.

Below is the formula to achieve the same.

In Table A, add a column name “TableCName”.

To get the Table C name, perform a Lookup on Table B, where Table B GUID is equal to Table A’s Table B lookup field, and then fetch the Table C Lookup’s name from Table B.

And lastly, perform a Filter on this new column TableCName, based on the selected value in the combo box.

See it working –

https://www.screencast.com/t/XZNDEsykAw

On selecting Table C1 in the combo box –

Similarly on selecting Table C2 in the combo box

Thanks to Debajit for guiding – https://debajmecrm.com/how-to-use-addcolumns-with-lookup-fields-of-sharepoint-in-power-apps/

To read more on AddColumns, Filter, and Lookup.

Hope it helps..

Advertisements

Product Table has Village Lookup in it and Village table has Region lookup in this sample screenshot

Fixed –Web resource method does not exist in Dynamics 365 / Dataverse


We might get this error while working with Web resource of type JavaScript.

Either it could be because the method doesn’t exist or it could be because of the incorrect syntax.

For a small file, it is easy to figure out, but for a large file, we could use online validator tools like

Fix the syntax error, upload and publish the file and the error should get fixed.

Hope it helps..

Advertisements

Fixed – Bad Request – Error in query syntax while using Xrm.WebAPI


We might get the below error while using Xrm.WebAPI.

‘Bad Request – Error in query syntax.’

One of the reasons could be that while setting up the Lookup field, we haven’t removed the curly brackets.

Use the below function to replace/remove them.

  • let result = myGuid.replace(/[{}]/g, ”);
  • let result = myGuid.replace(“{“, “”).replace(“}”, “”);

Hope it helps..

Advertisements

Binding Choice / OptionSet (multiselect) with Combo box in Canvas Apps / Power Apps (DataVerse)


Instead of hardcoding Combo box , we can bind it to the Choice / Option Set field of Dynamics 365 / Dataverse.

Format – Choices(DataSource[@columnname])

Check out the post that explains it – https://debajmecrm.com/how-to-bind-a-multiselect-choice-of-dataverse-to-a-combobox-in-canvas-apps/

Also check out – how to bind and do filter on multiselect combo box – https://youtu.be/5dSk5iOgT68?t=286

To learn about all the properties of Combo box refer – https://www.spguides.com/powerapps-combobox-control/

Hope it helps..

Advertisements