Showing posts with label Multiple. Show all posts
Showing posts with label Multiple. Show all posts

Saturday, April 1, 2023

PowerApps: Multiple Filters On Single Column (And/Or)

Hello Friends,
Welcome back with another post on PowerApps. We had discussed lot of things in some of our earlier posts. Sometimes, we have to apply multiple filter criteria on a single column. For example- we have a name column and we want to filter those names which Starts With "Wes" AND Ends With "mons". Additionally, it might be possible that instead of Starts with, user wants to filter records where Name Equals To  "Richard Lin". Or, user may want to get the records which Starts With "Roger". Means, there may be multiple combination of filter options with Single or Multiple criteria. How, we are going to tackle it?


Today, we will be discussing the same. Let's start-

  1. Login to PowerApps Maker Portal.
  2. Create a blank PowerApps Canvas App and give a suitable name.
  3. Either refer below post for adding a scrollable gallery or you can just create a gallery and add some data.
    1. PowerApps: Scroll Bar In Gallery
    2. PowerApps: Pagination Component
  4. I had added a scrollable gallery from Scroll Bar post and the content of gallery picked from Pagination component post.
  5. Here, I have created 1 master collection named "coll_MasterData" and later on assigned this master collection to another collection named "coll_FinalData". The reason of creating the second collection is to assign the filtered data to this collection so that it can be linked to gallery, while our master collection will remain intact.
  6. Next, we will add-
    1. Text Label (1 Nos)
    2. Text Input (2 Nos)
    3. Dropdown (3 Nos)
    4. Button (2 Nos)
  7. After adding, I had grouped them. The naming conventions are-
    1. Text Label: lbl_FatherNameFilterTitle
    2. Dropdown For 1st Filter Criteria: dd_FatherNameFilter1
    3. Textinput For 1st Filter Criteria: txt_FatherNameFilter1
    4. Dropdown For Filter Join Condition: dd_FatherNameFilterJoin
    5. Dropdown For 2nd Filter Criteria: dd_FatherNameFilter2
    6. Textinput For 2nd Filter Criteria: txt_FatherNameFilter2
    7. Button For Apply Filter: btn_FatherNameFilter_Apply
    8. Button For Apply Filter: btn_FatherNameFilter_Clear
  8. Similary, add another set of controls (mentioned above) for Mother Name filter.
  9. Now, we will create a collection of various filter criteria. Likewise- "If equal to", "Contains" ...
  10. So, on App >> OnStart, we will create this collection.
    1. ClearCollect(
          coll_FilterOptions,
          {Text: "Is equal to"},
          {Text: "Is not equal to"},
          {Text: "Starts with"},
          {Text: "Ends with"},
          {Text: "Contains"},
          {Text: "Does not contain"}
      );
  11. Next, we will bind this collection with Items property of-
    1. dd_FatherNameFilter1
    2. dd_FatherNameFilter2
    3. dd_MotherNameFilter1
    4. dd_MotherNameFilter2
  12. Also assign the Default property to the first item of collection.
  13. Now, we will create another collection "coll_JoinConditions" for Join conditions (And/Or) on App >> OnStart. Later, we will bind this collection to the Items property of-
    1. dd_FatherNameFilterJoin
    2. dd_MotherNameFilterJoin
  14. Also assign the Default property to the first item of collection.
  15. At this point, our base structure is ready. Now, we will write the filter code for each button. Ideally, we must include one more button which will contain the common logic. The reason behind is that, when a user will click on "Apply" button, you have to validate the filters of each column. So, rather than writing the same code on each "Apply" button, write down the code on a common button and add the Select function for this button on each "Apply" button.
  16. Let's add the button and name it as "btn_ApplyCommonFilter". Later-on we will set it's visible property as False. So that, the button should not be visible to users. 
  17. Now, apply the below code on "OnSelect" property of this button.
    1. UpdateContext({vrFilterCriteria1Key_FatherName:dd_FatherNameFilter1.SelectedText.Text});
      UpdateContext({vrFilterCriteria1Value_FatherName:txt_FatherNameFilter1.Text});
      UpdateContext({vrFilterJoinCondition_FatherName:dd_FatherNameFilterJoin.SelectedText.Text});
      UpdateContext({vrFilterCriteria2Key_FatherName:dd_FatherNameFilter2.SelectedText.Text});
      UpdateContext({vrFilterCriteria2Value_FatherName:txt_FatherNameFilter2.Text});
      UpdateContext({vrIsFilterAppliedOn_FatherName:false});
      ClearCollect(coll_Filter1_FatherName,[]);
      ClearCollect(coll_Filter2_FatherName,[]);
      ClearCollect(coll_FinalFilter_FatherName,[]);
      
      //GET COLLECTION BASED UPON FATHERNAME FIRST FILTER CRITERIA 
      If(
          !IsBlank(vrFilterCriteria1Value_FatherName)
          ,UpdateContext({vrIsFilterAppliedOn_FatherName:true});
          If(
              Upper(vrFilterCriteria1Key_FatherName) = "IS EQUAL TO"
              ,ClearCollect(coll_Filter1_FatherName,Filter(coll_MasterData,Upper(Father) = Upper(vrFilterCriteria1Value_FatherName)));
              ,If(
                  Upper(vrFilterCriteria1Key_FatherName) = "IS NOT EQUAL TO"
                  ,ClearCollect(coll_Filter1_FatherName,Filter(coll_MasterData,Upper(Father) <> Upper(vrFilterCriteria1Value_FatherName)));
                  ,If(
                      Upper(vrFilterCriteria1Key_FatherName) = "STARTS WITH"
                      ,ClearCollect(coll_Filter1_FatherName,Filter(coll_MasterData,StartsWith(Upper(Father), Upper(vrFilterCriteria1Value_FatherName))));
                      ,If(
                          Upper(vrFilterCriteria1Key_FatherName) = "ENDS WITH"
                          ,ClearCollect(coll_Filter1_FatherName,Filter(coll_MasterData,EndsWith(Upper(Father), Upper(vrFilterCriteria1Value_FatherName))));
                          ,If(
                              Upper(vrFilterCriteria1Key_FatherName) = "CONTAINS"
                              ,ClearCollect(coll_Filter1_FatherName,Filter(coll_MasterData,Upper(vrFilterCriteria1Value_FatherName) in Upper(Father)));
                              ,If(
                                  Upper(vrFilterCriteria1Key_FatherName) = "DOES NOT CONTAIN"
                                  ,ClearCollect(coll_Filter1_FatherName,Filter(coll_MasterData,!(Upper(vrFilterCriteria1Value_FatherName) in Upper(Father))));
                              );
                          );
                      );
                  );
              );
          );
      );
      
      //GET COLLECTION BASED UPON FATHERNAME SECOND FILTER CRITERIA 
      If(
          !IsBlank(vrFilterCriteria2Value_FatherName)
          ,UpdateContext({vrIsFilterAppliedOn_FatherName:true});
          If(
              Upper(vrFilterCriteria2Key_FatherName) = "IS EQUAL TO"
              ,ClearCollect(coll_Filter2_FatherName,Filter(coll_MasterData,Upper(Father) = Upper(vrFilterCriteria2Value_FatherName)));
              ,If(
                  Upper(vrFilterCriteria2Key_FatherName) = "IS NOT EQUAL TO"
                  ,ClearCollect(coll_Filter2_FatherName,Filter(coll_MasterData,Upper(Father) <> Upper(vrFilterCriteria2Value_FatherName)));
                  ,If(
                      Upper(vrFilterCriteria2Key_FatherName) = "STARTS WITH"
                      ,ClearCollect(coll_Filter2_FatherName,Filter(coll_MasterData,StartsWith(Upper(Father), Upper(vrFilterCriteria2Value_FatherName))));
                      ,If(
                          Upper(vrFilterCriteria2Key_FatherName) = "ENDS WITH"
                          ,ClearCollect(coll_Filter2_FatherName,Filter(coll_MasterData,EndsWith(Upper(Father), Upper(vrFilterCriteria2Value_FatherName))));
                          ,If(
                              Upper(vrFilterCriteria2Key_FatherName) = "CONTAINS"
                              ,ClearCollect(coll_Filter2_FatherName,Filter(coll_MasterData,Upper(vrFilterCriteria2Value_FatherName) in Upper(Father)));
                              ,If(
                                  Upper(vrFilterCriteria2Key_FatherName) = "DOES NOT CONTAIN"
                                  ,ClearCollect(coll_Filter2_FatherName,Filter(coll_MasterData,!(Upper(vrFilterCriteria2Value_FatherName) in Upper(Father))));
                              );
                          );
                      );
                  );
              );
          );
      );
      
      // GET THE FINAL FILTER RESULT FOR FATHER NAME BASED UPON JOIN CONDITION
      If(
          !IsBlank(vrFilterCriteria1Value_FatherName) && !IsBlank(vrFilterCriteria2Value_FatherName)
          ,If(
              Upper(vrFilterJoinCondition_FatherName) = "OR"
              ,ClearCollect(coll_FinalFilter_FatherName,coll_Filter2_FatherName);
              Collect(coll_FinalFilter_FatherName,Filter(coll_Filter1_FatherName, !(ID in coll_Filter2_FatherName.ID)));
              ,ClearCollect(coll_FinalFilter_FatherName,Filter(coll_Filter1_FatherName, ID in coll_Filter2_FatherName.ID));
          )
          ,If(
              !IsBlank(vrFilterCriteria2Value_FatherName)
              ,ClearCollect(coll_FinalFilter_FatherName,coll_Filter2_FatherName);
              ,If(
                  !IsBlank(vrFilterCriteria1Value_FatherName)
                  ,ClearCollect(coll_FinalFilter_FatherName,coll_Filter1_FatherName);
                  ,ClearCollect(coll_FinalFilter_FatherName,coll_MasterData);
              )
          )
      );
      
      
      //********************************************************************************************************//
      
      UpdateContext({vrFilterCriteria1Key_MotherName:dd_MotherNameFilter1.SelectedText.Text});
      UpdateContext({vrFilterCriteria1Value_MotherName:txt_MotherNameFilter1.Text});
      UpdateContext({vrFilterJoinCondition_MotherName:dd_MotherNameFilterJoin.SelectedText.Text});
      UpdateContext({vrFilterCriteria2Key_MotherName:dd_MotherNameFilter2.SelectedText.Text});
      UpdateContext({vrFilterCriteria2Value_MotherName:txt_MotherNameFilter2.Text});
      UpdateContext({vrIsFilterAppliedOn_MotherName:false});
      ClearCollect(coll_Filter1_MotherName,[]);
      ClearCollect(coll_Filter2_MotherName,[]);
      ClearCollect(coll_FinalFilter_MotherName,[]);
      
      //GET COLLECTION BASED UPON MotherNAME FIRST FILTER CRITERIA 
      If(
          !IsBlank(vrFilterCriteria1Value_MotherName)
          ,UpdateContext({vrIsFilterAppliedOn_MotherName:true});
          If(
              Upper(vrFilterCriteria1Key_MotherName) = "IS EQUAL TO"
              ,ClearCollect(coll_Filter1_MotherName,Filter(coll_MasterData,Upper(Mother) = Upper(vrFilterCriteria1Value_MotherName)));
              ,If(
                  Upper(vrFilterCriteria1Key_MotherName) = "IS NOT EQUAL TO"
                  ,ClearCollect(coll_Filter1_MotherName,Filter(coll_MasterData,Upper(Mother) <> Upper(vrFilterCriteria1Value_MotherName)));
                  ,If(
                      Upper(vrFilterCriteria1Key_MotherName) = "STARTS WITH"
                      ,ClearCollect(coll_Filter1_MotherName,Filter(coll_MasterData,StartsWith(Upper(Mother), Upper(vrFilterCriteria1Value_MotherName))));
                      ,If(
                          Upper(vrFilterCriteria1Key_MotherName) = "ENDS WITH"
                          ,ClearCollect(coll_Filter1_MotherName,Filter(coll_MasterData,EndsWith(Upper(Mother), Upper(vrFilterCriteria1Value_MotherName))));
                          ,If(
                              Upper(vrFilterCriteria1Key_MotherName) = "CONTAINS"
                              ,ClearCollect(coll_Filter1_MotherName,Filter(coll_MasterData,Upper(vrFilterCriteria1Value_MotherName) in Upper(Mother)));
                              ,If(
                                  Upper(vrFilterCriteria1Key_MotherName) = "DOES NOT CONTAIN"
                                  ,ClearCollect(coll_Filter1_MotherName,Filter(coll_MasterData,!(Upper(vrFilterCriteria1Value_MotherName) in Upper(Mother))));
                              );
                          );
                      );
                  );
              );
          );
      );
      
      //GET COLLECTION BASED UPON MotherNAME SECOND FILTER CRITERIA 
      If(
          !IsBlank(vrFilterCriteria2Value_MotherName)
          ,UpdateContext({vrIsFilterAppliedOn_MotherName:true});
          If(
              Upper(vrFilterCriteria2Key_MotherName) = "IS EQUAL TO"
              ,ClearCollect(coll_Filter2_MotherName,Filter(coll_MasterData,Upper(Mother) = Upper(vrFilterCriteria2Value_MotherName)));
              ,If(
                  Upper(vrFilterCriteria2Key_MotherName) = "IS NOT EQUAL TO"
                  ,ClearCollect(coll_Filter2_MotherName,Filter(coll_MasterData,Upper(Mother) <> Upper(vrFilterCriteria2Value_MotherName)));
                  ,If(
                      Upper(vrFilterCriteria2Key_MotherName) = "STARTS WITH"
                      ,ClearCollect(coll_Filter2_MotherName,Filter(coll_MasterData,StartsWith(Upper(Mother), Upper(vrFilterCriteria2Value_MotherName))));
                      ,If(
                          Upper(vrFilterCriteria2Key_MotherName) = "ENDS WITH"
                          ,ClearCollect(coll_Filter2_MotherName,Filter(coll_MasterData,EndsWith(Upper(Mother), Upper(vrFilterCriteria2Value_MotherName))));
                          ,If(
                              Upper(vrFilterCriteria2Key_MotherName) = "CONTAINS"
                              ,ClearCollect(coll_Filter2_MotherName,Filter(coll_MasterData,Upper(vrFilterCriteria2Value_MotherName) in Upper(Mother)));
                              ,If(
                                  Upper(vrFilterCriteria2Key_MotherName) = "DOES NOT CONTAIN"
                                  ,ClearCollect(coll_Filter2_MotherName,Filter(coll_MasterData,!(Upper(vrFilterCriteria2Value_MotherName) in Upper(Mother))));
                              );
                          );
                      );
                  );
              );
          );
      );
      
      // GET THE FINAL FILTER RESULT FOR Mother NAME BASED UPON JOIN CONDITION
      If(
          !IsBlank(vrFilterCriteria1Value_MotherName) && !IsBlank(vrFilterCriteria2Value_MotherName)
          ,If(
              Upper(vrFilterJoinCondition_MotherName) = "OR"
              ,ClearCollect(coll_FinalFilter_MotherName,coll_Filter2_MotherName);
              Collect(coll_FinalFilter_MotherName,Filter(coll_Filter1_MotherName, !(ID in coll_Filter2_MotherName.ID)));
              ,ClearCollect(coll_FinalFilter_MotherName,Filter(coll_Filter1_MotherName, ID in coll_Filter2_MotherName.ID));
          )
          ,If(
              !IsBlank(vrFilterCriteria2Value_MotherName)
              ,ClearCollect(coll_FinalFilter_MotherName,coll_Filter2_MotherName);
              ,If(
                  !IsBlank(vrFilterCriteria1Value_MotherName)
                  ,ClearCollect(coll_FinalFilter_MotherName,coll_Filter1_MotherName);
                  ,ClearCollect(coll_FinalFilter_MotherName,coll_MasterData);
              )
          )
      );
      
      //********************************************************************************************************//
      
      // FINAL RESULT BASED UPON ALL COLUMNS AND FILTERS
      
      If(
          vrIsFilterAppliedOn_FatherName && vrIsFilterAppliedOn_MotherName
          ,ClearCollect(coll_FinalData,Filter(coll_FinalFilter_FatherName,ID in coll_FinalFilter_MotherName.ID));
          ,If(
              vrIsFilterAppliedOn_FatherName
              ,ClearCollect(coll_FinalData,coll_FinalFilter_FatherName)
              ,If(
                  vrIsFilterAppliedOn_MotherName
                  ,ClearCollect(coll_FinalData,coll_FinalFilter_MotherName)
                  ,ClearCollect(coll_FinalData,coll_MasterData)
              );
          );
      );
  18. Just to clarify, it contains 3 section. The first section contains code for FatherName. The second section is replica of first section for the MotherName. we had just replaced Father with Mother. (or whatever be the column name you are going to apply the filter). The third section is the merging the result of both sections to one final result. Always remember to write code in such a way that if you have to make a replica of that code for other section/column, you have to do minimum rework. Here, I just replaced Father by Mother and it worked.
  19. Now, add the below code on "OnSelect" property of buttons - "btn_FatherNameFilter_Apply" & "btn_MotherNameFilter_Apply".
    1. Select(btn_ApplyCommonFilter)
  20. Add below code on "OnSelect" property of "btn_FatherNameFilter_Clear"-
    1. Reset(dd_FatherNameFilter1);
      Reset(txt_FatherNameFilter1);
      Reset(dd_FatherNameFilterJoin);
      Reset(dd_FatherNameFilter2);
      Reset(txt_FatherNameFilter2);
      Select(btn_ApplyCommonFilter);
  21. Similary add below code on "OnSelect" property of "btn_MotherNameFilter_Clear"
    1. Reset(dd_MotherNameFilter1);
      Reset(txt_MotherNameFilter1);
      Reset(dd_MotherNameFilterJoin);
      Reset(dd_MotherNameFilter2);
      Reset(txt_MotherNameFilter2);
      Select(btn_ApplyCommonFilter);
  22. Lastly, set the "Visible" property of button "btn_ApplyCommonFilter" as false.
  23. Now, save the app, publish and Run.
  24. First I will filter the data where FatherName StartsWith "a"
  25. Now, I will add the filter for MotherName where it should EndsWith "s".
  26. If I say, I want records where FatherName should either StartsWtih "ric" OR StartsWith "ant".
  27. This way, you may apply filters on more columns as well.
  28. A typical UI example of above filter box, I had applied in one of the application-

With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

Monday, January 31, 2022

Dynamics 365: CRUD Operations-6 (Delete Item)

Hello Friends,

Welcome back with another post on Dynamics 365. This post is the 6th part of CRUD operations.

In last post, we have created an item in Account Entity.

Dynamics 365: CRUD Operations-1 (Basic Preparations)

Dynamics 365: CRUD Operations-2 (Retrieve All Items)

Dynamics 365: CRUD Operations-3 (Create Item)

Dynamics 365: CRUD Operations-4 (Read Item)

Dynamics 365: CRUD Operations-5 (Update Item)

Now, we will learn about how to delete an individual item from D365 >> Account entity. 

  1. Below is the complete HTML. This HTML includes code of previous post as well. You may copy/paste this entire HTML.
  2. <html>
        <head></head>
        <body onfocusout="parent.setEmailRange();" style="overflow-wrap: break-word;">
            <meta charset="UTF-8">
            <link rel="stylesheet" href="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-js/1.4.0/css/fabric.min.css">
            <link rel="stylesheet" href="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-js/1.4.0/css/fabric.components.min.css">
            <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
            <link href="https://unpkg.com/bootstrap-table@1.18.1/dist/bootstrap-table.min.css" rel="stylesheet">
            
            <script src="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-js/1.4.0/js/fabric.min.js"></script>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
            <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>  
            <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
            <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
            <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
            <script src="https://unpkg.com/bootstrap-table@1.16.0/dist/bootstrap-table.min.js"></script>
            <script src="https://unpkg.com/tableexport.jquery.plugin/tableExport.min.js"></script>
            <style type="text/css">
                .footer {
                    position: fixed;
                    bottom: 0;
                    right: 0;
                    padding-bottom: 10px;
                    padding-right: 10px;
                }
        
                .footerButton {
                    width: 150px;
                }
            </style>
            <div>
                <table id="tblCreateRecord" style="width: 100%;">
                    <tr>
                        <td style="width: 50%;">Account Name</td>
                        <td style="width: 5%;">:</td>
                        <td style="width: 45%;"><input type="text" id="txtAccountName"></td>
                    </tr>
                    <tr>
                        <td style="width: 50%;">Primary Contact Person (ID)</td>
                        <td style="width: 5%;">:</td>
                        <td style="width: 45%;"><input type="text" id="txtPrimaryContactPersonID"></td>
                    </tr>
                    <tr>
                        <td style="width: 50%;">Account Category Code (Preferred Customer-1 / Standard-2)</td>
                        <td style="width: 5%;">:</td>
                        <td style="width: 45%;"><input type="text" id="txtAccountCategoryCode"></td>
                    </tr>
                    <tr>
                        <td style="width: 50%;">Annual Revenue</td>
                        <td style="width: 5%;">:</td>
                        <td style="width: 45%;"><input type="text" id="txtAnnualRevenue"></td>
                    </tr>
                    <tr>
                        <td style="width: 50%;">Primary Phone</td>
                        <td style="width: 5%;">:</td>
                        <td style="width: 45%;"><input type="text" id="txtPrimaryPhone"></td>
                    </tr>
                    <tr>
                        <td colspan="3">
                            <input type="button" onclick="fnCreateRecord();return false;" value="Create Record"/>
                            <input type="button" onclick="fnUpdateRecord();return false;" value="Update Record"/>
                            <input type="button" onclick="fnDeleteRecord();return false;" value="Delete Record"/>
                        </td>
                    </tr>
                    
                </table>
            </div>
            <div>
                <table id="table" data-search="true" data-header-style="headerStyle" data-page-size="25">
                    <thead>
                        <tr>
                            <th data-field="id" data-visible="false" data-checkbox="true">Id</th>
                            <th data-field="name" data-sortable="true">Full Name</th>
                            <th data-field="mainphone" data-sortable="true">Main Phone</th>
                            <th data-field="primaryemail" data-sortable="true">EMail (Primary)</th>
                            <th data-field="primarycontact" data-sortable="true">Primary Contact</th>
                            <th data-field="primarycontactname" data-sortable="true">Primary Contact Name</th>
                            <th data-field="readitem" data-sortable="false">Read</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </body>
        <script type="text/javascript">
            var vrAccountID = 0;
            $(document).ready(function(){
                fnGetAllAccounts();
            });
            function fnGetAllAccounts() 
            {
                //var query = '?$select=accountcategorycode,address1_addressid,address1_longitude,donotphone,name,_primarycontactid_value,revenue';
                var query = '?$select=accountid,name,telephone1,emailaddress1,_primarycontactid_value';
                window.parent.Xrm.WebApi.online.retrieveMultipleRecords("account", query).then(
                function success(accounts) 
                {
                    var results = accounts.entities;
                    var accountRecords = [];  
                    results.forEach(function (result, index, array) 
                    {
                        var accountid = result["accountid"];
                        var name = result["name"];
                        var telephone = result["telephone1"];
                        var emailaddress = result["emailaddress1"];
                        var primarycontactid = result["_primarycontactid_value"];
                        var primarycontactname = result["_primarycontactid_value@OData.Community.Display.V1.FormattedValue"];
                        
                        var accountRecord = {};  
                        accountRecord =  
                        {  
                            'id': accountid,
                            'name': name,
                            'account': accountid,
                            'mainphone': telephone,
                            'primaryemail':emailaddress,
                            'primarycontact':primarycontactid,
                            'primarycontactname':primarycontactname,
                            'readitem':'<a href="javascript:void(0);" onclick="fnReadRecord(\'' + accountid + '\');return false;">Read</a>'
                        }  
                        accountRecords.push(accountRecord);  
                    });
                    $('#table').bootstrapTable({ data: accountRecords });  
                },
                function (error) {
                    console.log(error.message);
                    reject(Error(error.message));
                });
            }
    
            function fnCreateRecord()
            {
                var entityData =
                {
                    "name": $('#txtAccountName').val(),
                    "primarycontactid@odata.bind": "/contacts(" + $('#txtPrimaryContactPersonID').val() + ")", //--- Lookup --- Display Text = Primary Contact
                    "accountcategorycode": $('#txtAccountCategoryCode').val(),     // Option Set --- Display Text = Category
                    "revenue": parseFloat($('#txtAnnualRevenue').val()),        // Currency (money type) ---Display Text = Annual Revenue
                    "telephone1":$('#txtPrimaryPhone').val()
                }
                window.parent.Xrm.WebApi.createRecord("account", entityData).then(
                    function success(result) {
                        //Success - No Return Data
                        alert("Account created successfully.");
                    },
                    function (error) {
                        alert(error.message);
                    }
                );
            }
    
            function fnReadRecord(accountID)
            {
                vrAccountID = accountID;
                //var query = '?$select=accountcategorycode,address1_addressid,address1_longitude,donotphone,name,_primarycontactid_value,revenue';
                var query = '?$select=accountid,name,accountcategorycode,revenue,telephone1,_primarycontactid_value';
                window.parent.Xrm.WebApi.online.retrieveRecord("account",accountID, query).then(
                function success(result) 
                {debugger;
                    if(result != null)
                    {
                        $('#txtAccountName').val(result["name"]);
                        $('#txtPrimaryContactPersonID').val(result["_primarycontactid_value"]);
                        $('#txtAccountCategoryCode').val(result["accountcategorycode"]);
                        $('#txtAnnualRevenue').val(result["revenue"]);
                        $('#txtPrimaryPhone').val(result["telephone1"]);
                    }
                },
                function (error) {
                    console.log(error.message);
                    reject(Error(error.message));
                });
            }
    
            function fnUpdateRecord()
            {
                var entityData =
                {
                    "name": $('#txtAccountName').val(),
                    "primarycontactid@odata.bind": "/contacts(" + $('#txtPrimaryContactPersonID').val() + ")", //--- Lookup --- Display Text = Primary Contact
                    "accountcategorycode": $('#txtAccountCategoryCode').val(),     // Option Set --- Display Text = Category
                    "revenue": parseFloat($('#txtAnnualRevenue').val()),        // Currency (money type) ---Display Text = Annual Revenue
                    "telephone1":$('#txtPrimaryPhone').val()
                }
    
                window.parent.Xrm.WebApi.updateRecord("account", vrAccountID, entityData).then(
                    function success(result) {
                        alert("Account updated successfully.");
                    },
                    function (error) {
                        // handle error conditions
                        console.log(error.message);
                        reject(error.message);
                    }
                );
            }
    
            function fnDeleteRecord()
            {
                window.parent.Xrm.WebApi.deleteRecord("account", vrAccountID).then(
                    function success(result) {
                        alert("Account deleted successfully.");
                    },
                    function (error) {
                        console.log(error.message);
                        // handle error conditions
                    }
                ); 
            }
        </script>
    </html>
    
  3. You need to copy/paste this HTML in Web Resource file we discussed in previous post.
  4. Save the Web Resource file and then publish it. Now refresh the D365 page (Ctrl + F5).
  5. Now, check it.
  6. First load the page and click on Read button for any record, you wish to delete.
  7. Now, click on Delete Record button. It will delete the record and prompts the message.
  8. Now, refresh the page or open the Account entity list form.
  9. The entity record has been deleted.
  10. This way, you can delete the individual record itself.
  11. The API used is:-
    1. window.parent.Xrm.WebApi.deleteRecord
  12. With this post, this series comes to an end. This is how, you can perform CRUD operations in D365.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !