Tuesday, January 25, 2022

Power Apps Portal Useful JS

1. If you want to hide Subgrid Create button based on condition you can use the below script.

You have to apply the below script on Basic Forms --> Open your Basic Form --> go to Additional Settings --> add in Custom Javascript.


// Based on Condition Hide Subgrid Create Button

$(document).ready(function(){


    // hide create button once sub-grid contains a record

    $(".entity-grid").on("loaded", function () {

        var tBody = $(this).find("tbody")

        if(tBody.length > 0)

        {        

            var Value=$("#its_budgeted").val();

            if(Value==2)

            {

               $(".grid-actions").hide();

            }

        }

    });

});


2.  Hiding Lookup values on Portal Forms, use the below script on Basic Form.

You have to apply the below script on Basic Forms --> Open your Basic Form --> go to Additional Settings --> add in Custom Javascript.

//Hide Lookup Values

$(document).ready(function() {  

$("#msdyn_serviceaccount").parent().parent().hide();
$("#msdyn_serviceaccount_label").parent().parent().hide();
$("#msdyn_serviceaccount_lookupmodal").parent().parent().hide();

 }

 );


3. To setup Lookup Value on Portal form, use the below script on Basic Form.

You have to apply the below script on Basic Forms --> Open your Basic Form --> go to Additional Settings --> add in Custom Javascript.

$(document).ready(function () {

  //To Set Lookup value use the below code

  $("#msdyn_serviceaccount_name").attr("value","Venkat");

  $("#msdyn_serviceaccount").attr("value","DAC92DEC-C94A-EC11-8C62-002248811BA2");

  $("#msdyn_serviceaccount_entityname").attr("value","account");


    //To Retrieve Value use the below line

    //var aa=$("#msdyn_serviceaccount").val();

    //alert(aa);

  });

4. Disable Lookup Selection, use the below script.

//Disable Lookup Values
$(document).ready(function() {
$("#msdyn_serviceaccount_name").parent().find('.input-group-btn').hide();
$("#its_divisiondept_name").parent().find('.input-group-btn').hide();
}
);


5. Automatically reopen last created record, use the below script.

You have to apply the below script on Lists --> Open your List Form --> go to Options --> add in Custom Javascript.

var existingRecordsList;
var firstLoad = true;
$(document).ready(function() {
var list = $(".entitylist.entity-grid").eq(0);
list.on("loaded", CheckNewRecord);
});
function CheckNewRecord() {
var list = $(".entitylist.entity-grid").eq(0);
var newRecordFound = false;
var newRecordId;
// first load
if (firstLoad) {
existingRecordsList = [];
}
if (existingRecordsList.length != $(list).find('table tbody tr').length) {
var tempRecordList = [];
$(list).find('table tbody tr').each(function() {
var id = $(this).attr("data-id");
tempRecordList.push(id);
if (!firstLoad && !newRecordFound && existingRecordsList.indexOf(id) < 0) {
newRecordId = id;
newRecordFound = true;
}
});
// reload global variable with items in temp list
existingRecordsList = tempRecordList;
if (newRecordFound && !!newRecordId) {
// open edit modal
var newRecordElement = $(list).find("[data-id='" + newRecordId + "']");
$(newRecordElement).find("a.edit-link").eq(0).trigger("click");
}
}
firstLoad = false;
}


6. Comparing Past Date with Current Date

$(document).ready(function () {
try{
if (typeof (Page_Validators) == 'undefined') return;
// Create new DOB validator
var dobValidator = document.createElement('span');
dobValidator.style.display = "none";
dobValidator.id = "dobValidator";
dobValidator.controltovalidate = "its_accountingdate";
dobValidator.errormessage = "<a href='#its_accountingdate_label'>Accounting Date should not be past date.</a>";
dobValidator.validationGroup = "";
dobValidator.initialvalue = "";
dobValidator.evaluationfunction = function () {
var dobValue = $("#its_accountingdate").val();
var dob=moment(new Date(dobValue),'DD/MM/YYYY');
var today =moment(new Date(),'DD/MM/YYYY');
if (dob < today) {
return false;
}
return true;
};
// Add the dobValidator to Web Page’s validators array
Page_Validators.push(dobValidator);
// Attach event handler of the validation summary link
$("a[href='#its_accountingdate_label']").on("click", function () { scrollToAndFocus('its_accountingdate_label','its_accountingdate');

});
}
catch(e)
{
alert("Error during DOB validation – "+e.description);
}
});

7. Apply JS on Onchange of Field, use the below script.

$(document).ready(function () {
// register onPrimaryChange function to run on change of its_name field
$("#its_name").change(onPrimaryChange);
//$("#its_name").change();
});
function onPrimaryChange(){
alert("Test");
}

8. Associate Subgrid Record, use the below script on Subgrid Basic Form

$(document).ready(function() {
$('#its_workorderid').val("{{request.params['id']}}").closest('.lookup').hide();
});

No comments: