Hi, i want to retrieve data for selected record via api and then create new record with the retrieved data, but i want to filter the data and exclude the id field so i can create a new record line with the same values. This is my code. How should i proceed? What do i add at the filter?
Please refer below JavaScript which will copy content of the current sales order record and clone it and will open in new tab. I have added some of the common fields you can add more as per your requirement.
function clone(PrimaryControl) { var formContext = PrimaryControl; var lookupFieldObject; var entityFormOptions = {}; entityFormOptions["entityName"] = "salesorder"; entityFormOptions["openInNewWindow"] = true var formParameters = {}; var confirmStrings = { text: "Do you want to clone this Order ?", title: "Please Confirmation", subtitle: "", "cancelButtonLabel": "Cancel", confirmButtonLabel: "Confirm" }; var confirmOptions = { height: 200, width: 500 }; Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then( function (success) { Xrm.Utility.showProgressIndicator("Cloning Sales order..."); setTimeout(function (){Xrm.Utility.closeProgressIndicator();},10000); if(formContext.getAttribute("name").getValue() !== null) { formParameters["name"] = formContext.getAttribute("name").getValue(); } if(formContext.getAttribute("freightamount").getValue() !== null) { formParameters["freightamount"] = formContext.getAttribute("freightamount").getValue(); } if(formContext.getAttribute("opportunityid").getValue() !== null) { lookupFieldObject = formContext.getAttribute("opportunityid").getValue(); formParameters["opportunityid"] = lookupFieldObject[0].id.slice(1, -1); formParameters["opportunityidname"] = lookupFieldObject[0].name; formParameters["opportunityidtype"] = lookupFieldObject[0].entityType; } if(formContext.getAttribute("quoteid").getValue() !== null) { lookupFieldObject = formContext.getAttribute("quoteid").getValue(); formParameters["quoteid"] = lookupFieldObject[0].id.slice(1, -1); formParameters["quoteidname"] = lookupFieldObject[0].name; formParameters["quoteidtype"] = lookupFieldObject[0].entityType; } Xrm.Navigation.openForm(entityFormOptions, formParameters).then( function (success) { console.log(success); }, function (error) { console.log(error); }); }); }
Refer below image for the Clone Ribbon button.
In below image you can see I am calling clone function and passing PrimaryControl
In the below video you will able to see how final working code looks like.
no, syntax is different, you can use my tool Dataverse REST Builder to generate the requests and see how the field types are filled.
Yes, thank you. I'll try that.
And can you please tell me about the lookup fields. Is the syntax same as newRecord["quantity"] = salesorderdetail["quantity"[; ?
You only want to clone the main record correct not related record?
as I understood you are trying to clone a record.
From my perspective you have two problems:
1) the Xrm.WebApi retrieves also the formatted values and add some additional fields like the etag, I am not sure that just removing the id attribute (in your case salesorderdetailid) will be enough to create a valid object for the createRecord function
2) the salesorderdetail is a tricky entity, generally you need to pay more attention on the columns you are passing to create a record under this entity than another one (like a contact for example)
however in your code you can avoid the filter, that is nice until someone else not understanding it checks your code and proceed with a standard creation and assignment, like
let newRecord = {};
newRecord["quantity"] = salesorderdetail["quantity"[;
etc etc, filling only the properties you need.
if you want to copy all the properties regardless I suggest to switch to another way to retrieve the record (like fetchAPI) where you can disable by an header the return of formatted values and deal with less properties.
hope it helps
Hi.
I have a ribbon button "Copy" and when a record is selected on click should copy the record.
So im trying to retrieve api data for the selected record with retrieveRecord and then create new record with the retrieved data. That is why I need to filter. I have to filter the fields and exclude the id field so i can create new record, because if i dont exclude the id, record wont be created.
Can you elaborate more about the selected record which you have mentioned in above requirements.
Below is the article which you can refer for the retrieve record and create record Web APIs.
docs.microsoft.com/.../retrieverecord
docs.microsoft.com/.../createrecord
Thank you,
Amit Katariya
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156