RE: I want to populate lookup value in child entity subgrid which has also this lookup field
I'm not sure this is the best code, but it should work as I do similar in my model driven app. Xrm.Page is deprecated so I think this should be updated.
function populateLookupChildRecords() {
var parentRecordId = Xrm.Page.data.entity.getId();
var lookupValue = Xrm.Page.getAttribute("new_lookupfield").getValue();
// Get the child entity records
var childRecords = Xrm.Page.getControl("childentity_subgridname").getGrid().getRows().get();
for (var i = 0; i < childRecords.length; i++) {
var childRecordId = childRecords[i].getData().getEntity().getId();
// Set the lookup value on the child record
var entity = {};
entity.new_lookupfield = { Id: parentRecordId, LogicalName: "parententity_logicalname" };
Xrm.WebApi.updateRecord("childentity_logicalname", childRecordId, entity)
.then(function success(result) {
console.log("Lookup value set on child record with ID: " + childRecordId);
},
function (error) {
console.log(error.message);
});
}
}