javascriptdynamics-crmmicrosoft-dynamicsdynamics-365

Call external API to get a file and attach it in Notes


I have a button where a user clicks on it, javascript makes a GET request to an external API to create an excel file. I want this excel file to be attached at a Note record and be shown in the timeline. While I have created the request via javascript and excel file is being generated, I don't know how to 'get' this file and attach it in CRM

function sendQuoteWithAPI(quoteId, selectedItems) {
                    var sendQuoteProcess = [];
                    var tokenId = 'tokenid';
                    ISV_abs_CloseDialog('ok');
                    var confirmStrings = {
                        text: "Excel file is being created. \n The file will be uploaded to timeline. \n\n\n Click Cancel if you don't want to proceed.",
                        title: "Export to Excel Confirmation!", confirmButtonLabel: "OK"
                    };
                    var confirmOptions = { height: 200, width: 400 };
                    //let test = "Excel file is being created. \n The file will be uploaded to timeline";
                    //window.parent.Xrm.Navigation.openAlertDialog({ text: test }, { height: 200, width: 300 });
                    var endpointURL = 'https://dynamics365-excelexport-dev.azurewebsites.net/api/export-quote?quoteId='+quoteId+'&customer='+selectedItems;
                    var requestOptions = {
                        method: 'GET',
                        headers: {
                            'Token': tokenId
                        }};
                    fetch(endpointURL, requestOptions)
                        .then(response => response.blob())
                        .then(result => console.log(result))
                        .catch(error => console.log('error:', error));
                }

Solution

  • First you need your file contents as a Base64-encoded string

    This gist provides an example of how to accomplish this: https://gist.github.com/n1ru4l/dc99062577b746e0783410b1298ab897

    Once you have the base64-encoded string that is your file, you need to create an annotation record with this structure: