Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

FetchXML to retrieve and update records

Posted on by Microsoft Employee

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);
}
);
}
  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: FetchXML to retrieve and update records

    Hi,

    Please have a look here I have answered here -

    community.dynamics.com/.../899799

  • LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: FetchXML to retrieve and update records

    Hi Rabbal,

    1.You have set the wrong parameter format in "Xrm.WebApi.updateRecord", the second parameter should be the GUID of the record.

    community172.png

    https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/xrm-webapi/updaterecord

    2.The response of fetchxml querying contains all fields value of the entity include GUID. You could try "medicationObject.Attributes["Accoundid"]".

    community172.png

    Hope it helps.

    Best Regards,

    Leo

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: FetchXML to retrieve and update records

    Also, how can I access the id of the returned record? thanks

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans