I'm trying to retrieve multiple records using fetchXML on the basis of a key and update these returned records later and save changes. So far, I have been able to retriev records but I'm not able to access attributes and update. Below is my code, please let me know the issue. thanks.
function markAllMedicationPast(userId) {
var fetchXML= "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct ='false'>"+
"<entity name='new_medication20'> "+
"<attribute name='hpben_parentmedication20' /> "+
"<attribute name='new_status' /> "+
"<filter> "+
"<condition attribute ='new_client' operator='eq' value='"+userId+"'/>"+
"</filter> "+
"</entity> "+
"</fetch>";
fetchXML= "?fetchXml=" + encodeURIComponent(fetchXML);
Xrm.WebApi.retrieveMultipleRecords("new_medication20", fetchXML).then (
function success(result) {
for (var i= 0; i < result.entities.length; i++) {
updateStatus(result.entities[i]);
}
},
function (error) {
window.alert("bummer, " + error.message);
}
);
}
function updateStatus(medicationObject) {
window.alert(medicationObject.Attributes["hpben_parentmedication20"]);
window.alert(medicationObject.Attributes["new_status"]);
if (medicationObject['hpben_parentmedication20'] == null) {
return;
}
var data = {
"new_status": "Last"
}
// update the record
Xrm.WebApi.updateRecord("new_medication20", medicationObject, data).then(
function success(result) {
console.log("Account updated");
},
function (error) {
console.log(error.message);
}
);
}