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);

    });

    

}