Task_1 JavaScript

 User Story

In the Case entity, based on Incident type selection, filter the Case category

using the category-Incident master entity

 Sudo Code

Tasks

1. Create Incident type entity -- 30 mins

2. Create a Case category entity

3. Create a Category-Incident master entity

4. In case entity provide the lookup of the Incident type and Case category.

5. On change of Incident type __ form type 1

   Read the selected incident type  

   Read the case category


   If the Incident type is null then disable the case category

   If the Incident type is not null then enable the case category


=> Also apply this code in the OnLoad event.


6. Create Incident type and case category lookup fields in Case Entity.


7. Prepare fetchXML from the advanced find view to get the case categories based on incident type 


8. On Change event of the incident field, if the Incident field is not null Create a custom filter view to show retrieved case categories and apply it to the case category lookup field.


8. OnChange event of case category lookup field, if it is not null then retrieve the case category and by using case category retrieve the related incident type from the category-Incident master entity.

  •     Here we can retrieve the related incident type id by using REST Builder, but to create lookup data we need name of the incent also.
  • For that we should retrieve the incident type record data by using retrieveSingle method, then create a new lookup field.


9. Set the retrieved incident type to the incident type lookup field.



Task Completion Procedure: -


  • Click on the gear icon => select Advanced Settings => click on Solutions => create a new solution.
  • Click the new button and select the Entity to create a new entity.
  • Create 3 entities with names as Case category, Incident type, Case-Incident


  • On the case-incident master entity, we need to set the case category field disabled on Onload and Enable when Incident lookup is not null.
  • For this, we need two Javascript functions.


// OnLoad event to disable Case category by default
function disableCaseCategory(executionContext) {
    var formContext = executionContext.getFormContext();
    if (formContext.ui.getFormType() == 1) {
        formContext.getControl("naree_categorytype").setDisabled(true);
    }

}




// OnChange of Incident type event

function OnChangeIncidentType(executionContext){
    var formContext = executionContext.getFormContext();
    var IncidentValue = formContext.getAttribute("naree_incidenttype").getValue();
    if(IncidentValue != null && formContext.ui.getFormType() == 1){
        formContext.getControl("naree_categorytype").setDisabled(false);  
    }  
}






After giving both values you need to update the name with concatenation of Incident type and case category.


function OnChangeCaseCategory(executionContext){
    var formContext = executionContext.getFormContext();
    var IncidentValue = formContext.getAttribute("naree_incidenttype").getValue();
    var CategoryValue = formContext.getAttribute("naree_categorytype").getValue();
    if(CategoryValue != null && formContext.ui.getFormType() == 1){
        var fullname = IncidentValue[0].name + "-" + CategoryValue[0].name;
        formContext.getAttribute("naree_name").setValue(fullname);  
    }  
}







Updating the Case category Lookup view based on Incident type

  • Now we have to create a Case category and Incident-type lookups in the Case entity.
  • If the Incident type is not equal to null, then for the OnChange event the Case Category Filter view should be updated based on category-incident master entity.
  • For this firstly we need the fetch XML string for filtering the lookup view.

  • To get the fetchXml use the Advanced Find.
After selecting the filter condition check the results, if the results are getting properly then click on Download Fetch XML.

  • Then we need to convert that XML code to a formatted string using XML formatter.
  • Now the below code will add a Filtered view to the case category lookup view.



// On change event for incident type field in case entity
// formContext.getControl(arg).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, isDefault)
// isDefault is a boolean value to make the created view as default view

function FilterCaseCategory_addCustomView(executionContext) {
    var formContext = executionContext.getFormContext();
    var Incident_Type = formContext.getAttribute("new_incidenttype").getValue();



    if (Incident_Type !== null) {

        var Incident_Type_id = Incident_Type[0].id;
        var Incident_Type_name = Incident_Type[0].name;

        var viewId = "00003652-0923-2000-0002-000000250001";
        var entityName = "naree_casecategory";
        var viewDisplayName = "Custom Category Filter View";
        var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
            "  <entity name='naree_casecategory'>" +
            "    <attribute name='naree_casecategoryid' />" +
            "    <attribute name='naree_name' />" +
            "    <attribute name='createdon' />" +
            "    <order attribute='naree_name' descending='false' />" +
            "    <link-entity name='naree_caseincident' from='naree_categorytype' to='naree_casecategoryid' link-type='inner' alias='ab'>" +
            "      <filter type='and'>" +
            "        <condition attribute='naree_incidenttype' operator='eq' uiname='" + Incident_Type_name + "' uitype='naree_incidenttype' value='" + Incident_Type_id + "' />" +
            "      </filter>" +
            "    </link-entity>" +
            "  </entity>" +
            "</fetch>";

        var layoutXml =
            "<grid name='resultset' icon='1' preview='1' select='1' jump='naree_name' object='5'>" +
            "<row name='result' id='naree_casecategoryid' >" +
            "<cell name='naree_name' width='300'/>" +
            "<cell name='createdon' width='300'/>" +
            "</row>" +
            "</grid>";



        formContext.getControl("new_casecategory").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
    }
}

            The below image will show the result.




Updating the Incident type based on the Case category selection


  • To update the incident type based on case category, we need to retrieve the relation between case category and incident entity from Incident-case master entity.
  • then get the data from that entity using REST Builder Web api code.
  • Create a lookup array and then update the field value.
// set the Incident type based on case category

function SetIncidentTypeBasedOnCategoryType(executionContext) {
    var formContext = executionContext.getFormContext();
    var Case_Category = formContext.getAttribute("new_casecategory").getValue();


    if (Case_Category != null) {


        var Case_Category_id = Case_Category[0].id.slice(1, -1);

        Xrm.WebApi.online.retrieveRecord("naree_casecategory", Case_Category_id, "?$expand=naree_naree_casecategory_naree_caseincident_CategoryType($select=_naree_incidenttype_value)").then(
            function success(result) {
                var naree_casecategoryid = result["naree_casecategoryid"];
                for (var a = 0; a < result.naree_naree_casecategory_naree_caseincident_CategoryType.length; a++) {
                    var Lookup_id = result.naree_naree_casecategory_naree_caseincident_CategoryType[a]["_naree_incidenttype_value"];

                    Xrm.WebApi.online.retrieveRecord("naree_incidenttype", Lookup_id, "?$select=naree_incidenttype,naree_incidenttypeid").then(
                        function success(result) {
                            var naree_incidenttype = result["naree_incidenttype"];
                            var naree_incidenttypeid = result["naree_incidenttypeid"];

                            var Lookup_data = [];
                            var object = {};
                            object.id = Lookup_id;
                            object.entityType = "naree_incidenttype";
                            object.name = naree_incidenttype;
                            Lookup_data[0] = object;



                            formContext.getAttribute("new_incidenttype").setValue(Lookup_data);
                        },
                        function (error) {
                            Xrm.Utility.alertDialog(error.message);
                        }
                    );
                }



            },
            function (error) {
                Xrm.Utility.alertDialog(error.message);
            }
        );
    }
}





Comments

Popular posts from this blog

How to create an Azure AD(Active Directory) and create an application to connect Dynamics 365 from console application

Task -6 JavaScript , Regular expressions