Thursday 1 September 2016

How to update the selected record into subgrid from the main form.



function RetrieveSubGridRecords() {

  //Get an array of entity references for all selected rows in the subgrid
  var selectedEntityReferences = [];
  var selectedRows = Xrm.Page.getControl("Contacts").getGrid().getSelectedRows();
  selectedRows.forEach(function (selectedRow, i) {
    selectedEntityReferences.push(selectedRow.getData().getEntity().getEntityReference());
  });

   if (selectedEntityReferences.length > 0)
    updateContact(selectedEntityReferences[0].id) //Get selected record guid
  else
    alert("Please select contact first");
}


// this function used to update contact using GUID 
function updateContact(contactid) {
  var contact = {};
  contact.JobTitle = "MR"; // set the field value.
  SDK.REST.updateRecord(contactid, contact, "Contact", updateSuccessCallback, errorHandler); //Provide Existing contact record GUID
}


function updateSuccessCallback() {
  alert("The account record changes were saved");
}

function errorHandler(error) {
  alert(error.message);
}




Thanks