Uncategorized – melgaard.nu https://melgaard.nu Blog about Dynamics 365, Power Platform, and everything in between Thu, 04 Jul 2024 14:01:39 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.11 https://melgaard.nu/wp-content/uploads/2024/06/cropped-cropped-Designer-4-1-32x32.jpeg Uncategorized – melgaard.nu https://melgaard.nu 32 32 Patching Dataverse Records Directly from the Browser Without Plugins https://melgaard.nu/patching-browser/ Sun, 23 Jun 2024 17:27:07 +0000 https://melgaard.nu/?p=161

Quite recently, I needed to do some data manipulation in a pinch in one of my heavily restricted Dataverse environments. The environment was only accessible from specific IPs, and the only tool I had available at the time was my browser—no executables allowed!
I could not find an example of how to do this directly from a browser, so it made a good topic for a blog post.
It is possible in the browser! This also means it’s possible to use the same authentication as the browser, with no need to re-authenticate.

Method 1: Patching using the javascript console

I got this one from an old Microsoft support case.

This approach is universally applicable, regardless of the browser you’re using. It’s particularly useful in scenarios where you don’t have access to the built-in tools, such as the network console in Edge.

Your browser might be different, but here is the procedure in Edge.
Open the developer console by hitting Ctrl – Shift – I (Or opening settings – More tools – Developer tools)

Click on the Console button. You might want to clear the console by clicking the clear button left to “top”

Copy this Javascript (Remember to change the values!), paste it into the console, and hit enter.

var queryPath = *Your dataverse instance*/api/data/v9.2/*table you want to patch*(*record GUID*);
var param = {};
param["Column to patch"] = "Column value";
 
var req = new XMLHttpRequest();
 
req.open("PATCH", queryPath, true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
 
req.onreadystatechange = function () {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            alert(this.status);
        }
        else {
            alert(this.status);
        }
    }
};
req.send(JSON.stringify(param));

It should look something like this if all goes well. I have detached the DEV console, it makes I a bit easier to work with.

If you get a “204” alert, that means that it went well and that you patched the record.

Method 2: Using the network console in Edge

This one is a slight twist on the first one. Instead, it uses the network console in Edge’s developer console to create the patch record.
Start by opening the Network console but clicking the Network console icon.

Click on the “Create Request” button. 
Change the method from “GET” to “POST”
Open “Body,” change the type to “Raw text” and enter the column and value as JSON.

{
    "column 1":"value",
    "Column 2":0.01
}


It should look something like this, and as the first example, if it returns 204, it went through.

I hope it makes sense, otherwise, do let me know (-:

]]>
Missing Excel Templates in Inventory Journals https://melgaard.nu/missing-excel-templates-in-inventory-journals/ Sun, 28 Feb 2021 02:00:48 +0000 http://melgaard.nu/?p=57

I have now solved this at multiple customers, so I think it’s time to write a post about it.

Have you ever wondered why custom Excel templates does not show on inventory journals?

The reason is most likely that your template are using an entity that is based on journal lines (InventJournalTrans), and not journal header (InventJournalTable). The way D365 figures out what templates to show, is to look at the forms primary data source. In these cases, the primary data source is InventJournalTable.

You could rewrite your template to use an entity that matches the forms primary data source. But there is an alternative!

Did you know that the “Open in office” menu is 100% configurable? It is! Microsoft has pretty good documentation about how to interface with it: https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/office-integration/customize-open-office-menu

The way I fixed this in the counting form was to extend customizeMenuOptions, to include the needed entities.

    public void customizeMenuOptions(OfficeMenuOptions _menuOptions)
    {
        next customizeMenuOptions(_menuOptions);
                
        OfficeMenuDataEntityOptions entityOptions  = OfficeMenuDataEntityOptions::construct(tableStr(InventInventoryCountingJournalLineEntity));
        entityOptions.dataSourceIdInternal(InventJournalTrans_ds.id());
        _menuOptions.dataEntityOptions().addEnd(entityOptions);
    }

The downside to this approach is that the filters D365 sets by default, is way to restrictive. In this case, it sets LineNum as a filter.

Since the form InventJournalCount does not implement the OfficeITemplateCustomExporter interface, I had to set the filters using a event handler. Place it where it suites best following you development best practice (You do follow a best practice, right?)

    [SubscribesTo(classStr(OfficeTemplateExportMenuItem), delegateStr(OfficeTemplateExportMenuItem, getInitialTemplateFilters))]
    public static void OfficeTemplateExportMenuItem_getInitialTemplateFilters(OfficeTemplateExportMenuItem _menuItem, FormRun _formRun, Map _initialFilters)
    {
        if  (_formRun.name() == formStr(InventJournalCount) && _menuItem.dataEntityName() ==  tableStr(InventInventoryCountingJournalLineEntity))
        {
            // Add an initial filter.
            ExportToExcelFilterTreeBuilder bldr = new ExportToExcelFilterTreeBuilder(_menuItem.dataEntityName());
            FormDataSource formDs = _formRun.dataSource(formDataSourceStr(InventJournalCount, InventJournalTable));
            InventJournalTable InventJournalTable = formDs.cursor();
            
            var filter = bldr.and(
                    bldr.areEqual(fieldStr(InventInventoryCountingJournalLineEntity, DataAreaId), curExt()),
                    bldr.areEqual(fieldStr(InventInventoryCountingJournalLineEntity, JournalNumber), InventJournalTable.JournalId));

            _initialFilters.insert(_menuItem.dataEntityName(), filter);
            _menuItem.applyRecordFilter(false);
        }
    }
]]>