Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Javascript object filter

Posted on by 105

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? IMG_2D00_c81fba3c086e77effd949a3f7bb4e1f1_2D00_V.jpg

  • Suggested answer
    Amit Katariya007 Profile Picture
    Amit Katariya007 8,506 Super User 2024 Season 1 on at
    RE: Javascript object 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.

    pastedimage1653751385838v1.png

    In below image you can see I am calling clone function and passing PrimaryControl

    pastedimage1653751463809v2.png

    In the below video you will able to see how final working code looks like.

  • Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Javascript object filter

    no, syntax is different, you can use my tool Dataverse REST Builder to generate the requests and see how the field types are filled.

  • sjov Profile Picture
    sjov 105 on at
    RE: Javascript object filter

    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"[; ?

  • Suggested answer
    Amit Katariya007 Profile Picture
    Amit Katariya007 8,506 Super User 2024 Season 1 on at
    RE: Javascript object filter

    You only want to clone the main record correct not related record? 

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Javascript object filter

    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

  • sjov Profile Picture
    sjov 105 on at
    RE: Javascript object filter

    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.

  • Suggested answer
    Amit Katariya007 Profile Picture
    Amit Katariya007 8,506 Super User 2024 Season 1 on at
    RE: Javascript object filter

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans