Thursday, 10 August 2023

RetrieveMultiple records from dataverse using console application

 public static void DataverseConnector()

        {

            try

            {

               string conn = @"AuthType=OAuth;Username=user@org.onmicrosoft.com;Password=password;Url=https://orgapp.crm.dynamics.com;TokenCacheStorePath=c:\MyTokenCache2;LoginPrompt=Auto";

                using (ServiceClient serviceClient = new ServiceClient(conn))

                {

                    if (serviceClient.IsReady)

                    {

                      var  query = @"<fetch mapping='logical'><entity name='account'><attribute name='accountid'/><attribute name='name'/><attribute name='numberofemployees'/></entity></fetch>";

                       var recordColl= serviceClient.RetrieveMultiple(new FetchExpression(query));

                        foreach (var accountrecord in recordColl.Entities)

                        {

                              Console.WriteLine(accountrecord.Id);

                              Console.WriteLine(accountrecord["name"]);

                              Console.WriteLine(accountrecord["numberofemployees"]);

                        }

                    }

                    else

                    {

                        Console.WriteLine("A web service connection was not established.");

                    }

                }

                // Pause the console so it does not close.

                Console.WriteLine("Press any key to exit.");

                Console.ReadLine();

            }

            catch (Exception ex)

            {

                Console.Write(ex.Message);

            }

            

        }

retrieve a record from dataverse table using console application

  public static void DataverseConnector()

        {

            try

            {

               string conn = @"AuthType=OAuth;Username=user@org.onmicrosoft.com;Password=password;Url=https://orgapp.crm.dynamics.com;TokenCacheStorePath=c:\MyTokenCache2;LoginPrompt=Auto";

                using (ServiceClient serviceClient = new ServiceClient(conn))

                {

                    if (serviceClient.IsReady)

                    {

                      var columnSet = new ColumnSet("name","numberofemployees");

                      var accountrecord= serviceClient.Retrieve("account", new Guid("a17f700a-6d37-ee11-bdf4-000d3a0aab51"),columnSet);

                        Console.WriteLine(accountrecord.Id);

                        Console.WriteLine(accountrecord["name"]);

                        Console.WriteLine(accountrecord["numberofemployees"]);

                    }

                    else

                    {

                        Console.WriteLine("A web service connection was not established.");

                    }

                }


                // Pause the console so it does not close.

                Console.WriteLine("Press any key to exit.");

                Console.ReadLine();

            }

            catch (Exception ex)

            {

                Console.Write(ex.Message);

            }

            

        }

update a record in dataverse using console application

  public static void DataverseConnector()

        {

            try

            {

                string conn = @"AuthType=OAuth;Username=user@org.onmicrosoft.com;Password=password;Url=https://orgapp.crm.dynamics.com;TokenCacheStorePath=c:\MyTokenCache2;LoginPrompt=Auto";

                using (ServiceClient serviceClient = new ServiceClient(conn))

                {

                    if (serviceClient.IsReady)

                    {

                        //Use Entity class with entity logical name

                        var account = new Entity("account");

                        account.Id = new Guid("a17f700a-6d37-ee11-bdf4-000d3a0aab51");

                        // set attribute values

                        // string primary name

                        account["name"] = "Contoso2";

                        // Boolean (Two option)

                        account["creditonhold"] = false;

                        // DateTime

                        account["lastonholdtime"] = new DateTime(2017, 1, 1);

                        // Double

                        account["address1_latitude"] = 47.642311;

                        account["address1_longitude"] = -122.136841;

                        // Int

                        account["numberofemployees"] = 500;

                        // Money

                        account["revenue"] = new Money(new decimal(5000000.00));

                        // Picklist (Option set)

                        account["accountcategorycode"] = new OptionSetValue(1); //Preferred customer

                        //Create the account

                       serviceClient.Update(account);

                      //  Console.WriteLine("Record ID is {0}.", accountid);

                    }

                    else

                    {

                        Console.WriteLine("A web service connection was not established.");

                    }

                }


                // Pause the console so it does not close.

                Console.WriteLine("Press any key to exit.");

                Console.ReadLine();

            }

            catch (Exception ex)

            {

                Console.Write(ex.Message);

            }

            

        }

Create a record in Dataverse in Console Apllication c#

  public static void DataverseConnector()

        {

            try

            {

               string conn = @"AuthType=OAuth;Username=user@org.onmicrosoft.com;Password=password;Url=https://orgapp.crm.dynamics.com;TokenCacheStorePath=c:\MyTokenCache2;LoginPrompt=Auto";

                using (ServiceClient serviceClient = new ServiceClient(conn))

                {

                    if (serviceClient.IsReady)

                    {

                        //Use Entity class with entity logical name

                        var account = new Entity("account");

                        // set attribute values

                        // string primary name

                        account["name"] = "Contoso";

                        // Boolean (Two option)

                        account["creditonhold"] = false;

                        // DateTime

                        account["lastonholdtime"] = new DateTime(2017, 1, 1);

                        // Double

                        account["address1_latitude"] = 47.642311;

                        account["address1_longitude"] = -122.136841;

                        // Int

                        account["numberofemployees"] = 500;

                        // Money

                        account["revenue"] = new Money(new decimal(5000000.00));

                        // Picklist (Option set)

                        account["accountcategorycode"] = new OptionSetValue(1); //Preferred customer

                        //Create the account

                        Guid accountid= serviceClient.Create(account);

                        Console.WriteLine("Record ID is {0}.", accountid);

                    }

                    else

                    {

                        Console.WriteLine("A web service connection was not established.");

                    }

                }


                // Pause the console so it does not close.

                Console.WriteLine("Press any key to exit.");

                Console.ReadLine();

            }

            catch (Exception ex)

            {

                Console.Write(ex.Message);

            }

            finally

            {

                Console.Write("test");

            }

        }

Connect dataverse in Console Application c#

  Install "Microsoft.PowerPlatform.Dataverse.Client"


Using Microsoft.PowerPlatform.Dataverse.Client;

 public static void DataverseConnector()

        {

            try

            {

               string conn = @"AuthType=OAuth;Username=user@org.onmicrosoft.com;Password=password;Url=https://orgapp.crm.dynamics.com;TokenCacheStorePath=c:\MyTokenCache2;LoginPrompt=Auto";


                using (ServiceClient serviceClient = new ServiceClient(conn))

                {

                    if (serviceClient.IsReady)

                    {

                        WhoAmIResponse response =(WhoAmIResponse)serviceClient.Execute(new WhoAmIRequest());

                        Console.WriteLine("User ID is {0}.", response.UserId);

                    }

                    else

                    {

                        Console.WriteLine("A web service connection was not established.");

                    }

                }


                // Pause the console so it does not close.

                Console.WriteLine("Press any key to exit.");

                Console.ReadLine();

            }

            catch (Exception ex)

            {

                Console.Write(ex.Message);

            }

            finally

            {

                Console.Write("test");

            }

        }


Tuesday, 1 August 2023

Steps to create Patch and deployment on Higher environment

 Dev- Original Solution-Create Clone Patch.

-Include Component in Patch and update the solution.

-Export patch Solution.

QA-Import in QA.

DEV-Select Origional Solution.

-Click on Clone Solution it will merge the patch in original solution.

-Export the original Solution.

QA-Import the Solution in QA and it will merge the patch in Original solution.

Monday, 6 February 2023

Set field required using code in model driven app

 Below is code which automate the column configuration based on settings

We have to create a table "validationtable" and some columns for settings of the columns

Entityname

ColumnName

IsRequired

IsHidden

IsDisabled


Then call below function on the form on which you want to use these settings applied

add some data in ValidationTable and execute open new form where the code is registered. 

function bindClientControl(formcontext) {
    var frmcontext = formcontext.getFormContext();
    var fetchXML='?fetchXml=<fetch version="1.0" mapping="logical" no-lock="false" count="100" distinct="true"><entity name="cr6a2_validationtable"><attribute name="cr6a2_validationtableid"/><attribute name="cr6a2_name"/><order attribute="cr6a2_name" descending="false"/><attribute name="cr6a2_columnname"/><attribute name="cr6a2_isdisabled"/><attribute name="cr6a2_ishidden"/><attribute name="cr6a2_isrequired"/><filter type="and"><condition attribute="cr6a2_name" operator="eq" value="'+frmcontext.data.entity._entityType+'"/></filter></entity></fetch>';
    Xrm.WebApi.retrieveMultipleRecords("cr6a2_validationtable", fetchXML).then(
        function success(result) {
            for (var i = 0; i < result.entities.length; i++) {
                if (frmcontext.getControl(result.entities[i].cr6a2_columnname) != null) {
                       frmcontext.getControl(result.entities[i].cr6a2_columnname).setDisabled(result.entities[i].cr6a2_isdisabled);
                       frmcontext.getControl(result.entities[i].cr6a2_columnname).setVisible(result.entities[i].cr6a2_ishidden);
                    if(result.entities[i].cr6a2_isrequired==true)
                    {
                       frmcontext.getAttribute(result.entities[i].cr6a2_columnname).setRequiredLevel("required");
                    }
                    else
                    {
                       frmcontext.getAttribute(result.entities[i].cr6a2_columnname).setRequiredLevel("none");
                    }
                }


                console.log(result.entities[i].cr6a2_columnname);
            }                    
            // perform additional operations on retrieved records
        },
        function (error) {
            console.log(error.message);
            // handle error conditions
        }
    );
}

Auto save form in model driven app

 Auto save form in model driven app on change event on controls

Call below method on any control's change event

function autoSavedData(formcontext)
{
    var frmcontext = formcontext.getFormContext();
    frmcontext.data.entity.save("no");
//below parameter can be passed in save mehtod as per requirement
//no
//saveandclose
// saveandnew
}

Friday, 3 February 2023

Bind Team Members in Lookup field in Model Driven app

 We have can bind for a specific team's members in Lookup field in Model driven app/dataverse using code


Upload the below code on Web Resources  and add on page load event and pass the formcontext parameter



function bindLookUpField(formcontext)

{

    var frmcontext= formcontext.getFormContext();

    var viewId = "GUID";

    var entityName = "systemuser";

    var viewDisplayName = "Team Members";

    frmcontext.getControl("InternaleLookupFieldName").addPreSearch(function () {

    var fechxml='<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">'+

    '<entity name="systemuser">'+

    '<attribute name="fullname" />'+

    '<attribute name="systemuserid" />'+

    '<order attribute="fullname" descending="false" />'+

    '<link-entity name="teammembership" from="systemuserid" to="systemuserid" visible="false" intersect="true">'+

    '<link-entity name="team" from="teamid" to="teamid" alias="ac">'+

    '<filter type="and">'+

    '<condition attribute="name" operator="eq" value="Team Name" />'+

    '</filter>'+

    '</link-entity>'+

    '</link-entity>'+

    '</entity>'+

    '</fetch>';

    var layoutXml = "<grid name='resultset' object='1' jump='systemuserid' select='1' icon='1' preview='1'>" +

                    "<row name='result' id='cr6a2_teamsmember'>" +

                    "<cell name='fullname' width='150' />" +

                    "</row>" +

                    "</grid>";

   frmcontext.getControl("InternaleLookupFieldName").addCustomView(viewId, entityName, viewDisplayName, fechxml, layoutXml, true);

    });

    

}




Friday, 9 July 2021

How to debug Unit test cases in Node

 How to debug Unit test cases in Node


1.  Add below in package.json file 


"test-debug": "node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand"


2.  go to visual studio code and press f5 then in the select node.js from  dropdown of the Debug and Run Script


3. select the debug terminal from the option 

4. type debugger; in your function 

5. open chrome new tab and enter chrome://inspect/#devices

6 . then select node dev tool

7 then press f8 to execute the code