Bhubaneswar, Odisha, India
+91-8328865778
support@softchief.com

Sample Code Business Use Case – Subgrids with command button, Open views as dialog with sync and await, pass parameter from NavigateTo Dialog to parent form

Sample Code Business Use Case – Subgrids with command button, Open views as dialog with sync and await, pass parameter from NavigateTo Dialog to parent form

Here is sample scripts.

JavaScript

//call it from command button click of subgrid command of child entity
function CreateTicket(primaryControl)
{
    sessionStorage.setItem("cid", primaryControl.data.entity.getId().replace("{","").replace("}",""));
   
    var pageInput = {
        pageType: "entitylist",
        entityName: "sanjay_sanjayhdfcserviceticket"
    };

    var navigationOptions = {
        target: 2,
        height: {value: 80, unit:"%"},
        width: {value: 70, unit:"%"},
        position: 1
    };


    Xrm.Navigation.navigateTo(pageInput,navigationOptions).then(
        function success() {
                // Run code on success
        },
        function error() {
                // Handle errors
        }
    );
}

// call it from home grid command button to update child records
async function AddTicket(Ids) {
    var recCount=0;
    var selectedRecs = String(Ids).split(",");
    var cid = (sessionStorage.getItem("cid"));
    for (let index = 0; index < selectedRecs.length; index++) {
        const element = selectedRecs[index];
        // define the data to update a record
        var data =
        {
            "sanjay_TargetCustomer_contact@odata.bind": "/contacts("+cid+")"
        }
        
        // update the record
        await Xrm.WebApi.updateRecord("sanjay_sanjayhdfcserviceticket", selectedRecs[index], data).then(
            function success(result) {
                recCount = recCount+1; 
                // perform operations on record update
            },
            function (error) {
                console.log(error.message);
                // handle error conditions
            }
        );
    }
    alert(recCount + " records are linked.");
    var pageInput = {
        pageType: "entityrecord",
        entityName: "contact",
        entityId:cid
    };

    var navigationOptions = {
        target: 1,
        height: {value: 90, unit:"%"},
        width: {value: 70, unit:"%"},
        position: 1
    };
    

    Xrm.Navigation.navigateTo(pageInput,navigationOptions).then(
        function success() {
                // Run code on success
        },
        function error() {
                // Handle errors
        }
    );

    
}
JavaScript

Hope it helps.