Auto populate the “to” field of custom entity in Dynamics 365 CRM

To auto populate “to” field email form, in Dynamics CRM. For custom entity, it is not available by default. You need to write JS code to achieve this. Below code is all you need

function setToPartyList(executionContext){
    var formContext = executionContext.getFormContext()
    if(formContext.getAttribute("regardingobjectid").getValue() != null) {
        var regardingidname = formContext.getAttribute("regardingobjectid").getValue()[0].name;
        var regardingId = formContext.getAttribute("regardingobjectid").getValue()[0].id;

        // Create new array
        var partlist = new Array();
        partlist[0] = new Object();
        partlist[0].id = regardingId;
        partlist[0].name =  regardingidname;
        partlist[0].entityType = "lib_librarytracker";
        formContext.getAttribute("to").setValue(partlist);
    }
  };