Business Central - 'OnCustomDocumentMergerEx' even does not trigger - dynamics-business-central

I Recently tried to refactor a deprecated part of our code, which is an event subscription to 'OnBeforeMergeDocument', Because i had some problems regarding the printer name which I posted about in this Stack Overflow post.
I then tried to bind to the new event using the following code
[EventSubscriber(ObjectType::Codeunit, Codeunit::ReportManagement, 'OnCustomDocumentMergerEx', '', true, true)]
local procedure OnCustomDocumentMergerEx(ObjectID: Integer; ReportAction: Option SaveAsPdf,SaveAsWord,SaveAsExcel,Preview,Print,SaveAsHtml; ObjectPayload: JsonObject; XmlData: InStream; LayoutData: InStream; var DocumentStream: OutStream; var IsHandled: Boolean)
var
Test: Text;
begin
Test := 'test';
IsHandled := true;
end;
Just like the 'OnBeforeMergeDocument' event, i expected it to fire when i preview or print or send a report (for example if you go to business central > posted sales invoices > print/send > print). However it doesnt, and im getting a 'The custom report layout for '' is empty.' why is this? and why doesnt the event fire when I think it would fire?

This behaviour is pretty much different for on-prem/SaaS deployment and versions from 19 to 21. I am assuming that you are running a SaaS instance which is updated to the latest release (21.3).
In this version, preparation of the report layout is handled by the platform by default, and application events are not triggered. To change the flow, subscribe to the event ApplicationReportMergeStrategy in codeunit Reporting Triggers and change the merge strategy to Application.
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Reporting Triggers", 'ApplicationReportMergeStrategy', '', false, false)]
local procedure SetMergeStrategy(ObjectId: Integer; LayoutCode: Text; var InApplication: Boolean)
begin
// if ObjectId = ... - if the strategy is set for a specific report object
InApplication := true;
end;
Once the merge strategy is changed to Application, the event OnCustomDocumentMergerEx starts firing.

Related

Cannot injectScript in Custom Variable Template

I'm trying to injectScript via Custom Variable Template (not tag).
Here is simplified code:
const log = require('logToConsole');
const setTimeout = require('callLater');
const setInWindow = require('setInWindow');
const copyFromWindow = require('copyFromWindow');
const copyFromDataLayer = require('copyFromDataLayer');
const injectScript = require('injectScript');
const pixelSend = function(eventType, eventParams, tries) {
// logic
log('success')
};
log('event - ', copyFromDataLayer('event'));
if (copyFromDataLayer('event') === 'gtm.js') {
injectScript('https://vk.com/js/api/openapi.js', // this one should create **"VK"** object in global scope, used to actually send the events
pixelSend(),
data.gtmOnFailure);
}
return true;
Unfortunately openapi.js never gets injected (checking in network tab) and thus VK object never gets created and I cannot use it.
If I just run in console:
var head = document.getElementsByTagName('head')[0]
var js = document.createElement('script');
js.src = 'https://vk.com/js/api/openapi.js';
head.appendChild(js);
It gets injected and VK object becomes available.
What am I doing wrong?
Just in case:
queryPermission('inject_script', 'https://vk.com/js/api/openapi.js') = true
I tried this, and there were just a few minor bugs - line 11 is missing a semicolon, and you did not mention if you allowed access to read the "event" key from the datalayer.
After that was fixed, the script worked as expected.
Obviously it will only work on the page view trigger (since this is the only case when event equals gtm.js. I probably would move the condition from the tag to the trigger).
Instead of "return true" you should end this will a call to data.gtmOnSuccess(), else you might have trouble using this tag in a tag sequence.
If in the template UI you hit the "run code" switch you will actually get information on all error in your code (alas one at a time, since execution stops at the first error). You can also write tests with mock input, for templates that require settings via input fields.

Relational Query - 2 degrees away

I have three models:
Timesheets
Employee
Manager
I am looking for all timesheets that need to be approved by a manager (many timesheets per employee, one manager per employee).
I have tried creating datasources and prefetching both Employee and Employee.Manager, but I so far no success as of yet.
Is there a trick to this? Do I need to load the query and then do another load? Or create an intermediary datasource that holds both the Timesheet and Employee data or something else?
You can do it by applying a query filter to the datasource onDataLoad event or another event. For example, you could bind the value of a dropdown with Managers to:
#datasource.query.filters.Employee.Manager._equals
- assuming that the datasource of the widget is set to Timesheets.
If you are linking to the page from another page, you could also call a script instead of using a preset action. On the link click, invoke the script below, passing it the desired manager object from the linking page.
function loadPageTimesheets(manager){
app.showPage(app.pages.Timesheets);
app.pages.Timesheets.datasource.query.filters.Employee.Manager._equals = manager;
app.pages.Timesheets.datasource.load();
}
I would recommend to redesign your app a little bit to use full power of App Maker. You can go with Directory Model (Manager -> Employees) plus one table with data (Timesheets). In this case your timesheets query can look similar to this:
// Server side script
function getTimesheets(query) {
var managerEmail = query.parameters.ManagerEmail;
var dirQuery = app.models.Directory.newQuery();
dirQuery.filters.PrimaryEmail._equals = managerEmail;
dirQuery.prefetch.DirectReports._add();
var people = dirQuery.run();
if (people.length === 0) {
return [];
}
var manager = people[0];
// Subordinates lookup can look fancier if you need recursively
// include everybody down the hierarchy chart. In this case
// it also will make sense to update prefetch above to include
// reports of reports of reports...
var subortinatesEmails = manager.DirectReports.map(function(employee) {
return employee.PrimaryEmail;
});
var tsQuery = app.models.Timesheet.newQuery();
tsQuery.filters.EmployeeEmail._in = subortinatesEmails;
return tsQuery.run();
}

Why error "The state is invalid"?

We have CRM 2011, on-premises. In a WCF Service (C#) I am programmatically creating Contracts and ContractDetails. After creating the Contract, I set its State = 'Invoiced' using this code:
try
{
SetStateRequest setStateRequest = new SetStateRequest()
{
EntityMoniker = new EntityReference
{
Id = gNewContractId,
LogicalName = Xrm.Contract.EntityLogicalName
},
State = new OptionSetValue((int)Xrm.ContractState.Invoiced),
Status = new OptionSetValue((int)Xrm.ContractState.Invoiced + 1)
};
_service.Execute(setStateRequest);
}
This process used to work but sometimes now I get this error, as I did today:
"System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: The state is invalid, this contract cannot be set to invoice state. (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault)."
The State of the contract when this snippet was executed (and which failed today) was 'Draft'.
This Contract has child Contract Details, and its ActiveOn date is 6/1/2015, so it should have been made Active - and it was. So I am not understanding the error or what I need to do to prevent it.
Thanks for all help and advice.
First this line of your code
Status = new OptionSetValue((int)Xrm.ContractState.Invoiced + 1)
Should be something like
Status = new OptionSetValue((int)Xrm.ContractStatus.Invoiced)
or the name for the Status Reason enum that was generated, this because you are writing 1+1, so it reduce the function of the early bound.
Regarding the Contract, looks like that the SetStateRequest acts differently based on the Start and End Dates of the Contract. If the range falls inside current date, the request set the Contract to Active, if the range falls outside current date, the contract is invoiced. Please check your contract dates and see if this was the case.

how to set autosum property in x++ for a morphx report

I have the following code in the init() of a report:
QueryBuildDataSource qbdsTable;
QueryOrderByField QueryOrderByFieldTransDate;
QueryOrderByField QueryOrderByFieldDimZone
QueryOrderByField QueryOrderByFieldDimCC;
;
super();
qbdsTable = query.dataSourceTable(tableNum(Table));
QueryOrderByFieldTransDate = qbdsTable.addOrderByField(fieldNum(Table, TransDate));
QueryOrderByFieldTransDate.autoSum(true);
QueryOrderByFieldDimZone = qbdsTable.addOrderByField(fieldNum(Table, DimZone),SortOrder::Descending);
QueryOrderByFieldDimZone.autoSum(true);
QueryOrderByFieldDimCC = qbdsTable.addOrderByField(fieldNum(Table, DimCostCenter));
QueryOrderByFieldDimCC.autoSum(true);
and the autosum property is functioning properly (I have set the SumAll property for the field I use to calculate these subtotals).
The problem is that, whenever I try to add an groupBy field or a selection field, the autosum property isn't honored anymore (the subtotals are not displayed anymore):
qbdsTable.addSelectionField(fieldNum(Table, AmountMST), selectionField::Sum);
or
qbdsTable.addGroupByField(fieldNum(Table, TransDate));
I have tried to use:
qbdsTable.addSortField(fieldNum(Table, TransDate));
qbdsTable.autoHeader(1, true);
but I have the same problem
Does anyone has an Idea how I can use both autosum and addGroupByField on the same datasorce of a report?
For historical reasons old style AX reports behaves differently when called directly (run on the report node) or through on a report menu item.
The execution order of the first is:
init
fetch
dialog
The second runs via class RunbaseReportStd in the following order:
init
dialog
fetch
This matters because you have change the query after the user has made any changes.
So move your code changes from init to fetch, like this:
public boolean fetch()
{
QueryBuildDataSource qbdsCustTrans = query.dataSourceTable(tableNum(CustTrans));
;
qbdsCustTrans.addSelectionField(fieldNum(CustTrans, AmountMST), selectionField::Sum);
qbdsCustTrans.addGroupByField(fieldNum(CustTrans, AccountNum));
qbdsCustTrans.addGroupByField(fieldNum(CustTrans, TransDate));
qbdsCustTrans.addGroupByField(fieldNum(CustTrans, CurrencyCode));
//info(qbdsCustTrans.toString());
return super();
}
This will only work, if called through the menu item.
Also, I could not get the auto-sum functionality to work, when added by code.
Instead you will have to add the order by and autosum using Sorting node of the report query.
I don't know why, but maybe this is because you use auto design, which is generated at run time.

How can I reliably wait for JavaScript alerts using Selenium2 / WebDriver?

I am currently assisting in a proof of concept using Selenium 2 / WebDriver with C# against an ASP.NET MVC application using the InternetExplorerDriver.
The application uses a standard pattern for notifying users that a record has saved. This works by settings TempData to include "Record saved successefully", and if TempData is present in the View, the view will alert the message.
Whilst working on Selenium tests for this functionality, we are receiving inconstitant behaviour from the below C# / Selenium test code:
_driver.Navigate().GoToUrl(_baseUrl + "/Amraam/List");
_driver.FindElement(By.LinkText("Create New")).Click();
_driver.FindElement(By.Id("txtAmraamSerialNumber")).SendKeys("CC12345");
var selectElement = new SelectElement(_driver.FindElement(By.Id("LocationId")));
selectElement.SelectByText("Tamworth");
_driver.FindElement(By.Id("btnSave")).Click();
var wait = new WebDriverWait(_driver, defaultTimeout);
IAlert alert = wait.Until(drv => drv.SwitchTo().Alert());
_alertText = alert.Text;
alert.Accept();
Assert.That(_alertText, Is.EqualTo("Record successfully saved"));
Approximately 50% of the time, Selinium will fail with a
OpenQA.Selenium.NoAlertPresentException : No alert is active
I struggle to find an exact way to replicate the issue, and worry regarding the inconsistency aspect. If it failed consistently, then we could debug and track the problem down.
The handling of alerts and prompts within Selenium 2 is pretty new and is still under active development.
Your failures are probably due to timing, so I would suggest writing a wrapper method around the call to SwitchTo().Alert() so that you catch the OpenQA.Selenium.NoAlertPresentException and ignore it until the timeout expires.
Something as simple as this should work:
private IAlert AlertIsPresent(IWebDriver drv)
{
try
{
// Attempt to switch to an alert
return drv.SwitchTo().Alert();
}
catch (OpenQA.Selenium.NoAlertPresentException)
{
// We ignore this execption, as it means there is no alert present...yet.
return null;
}
// Other exceptions will be ignored and up the stack
}
This line
IAlert alert = wait.Until(drv => drv.SwitchTo().Alert());
would then become
IAlert alert = wait.Until(drv => AlertIsPresent(drv));
Hope this will be helpful for waiting and click
WebDriverWait(driver,4).until(EC.alert_is_present())
driver.switch_to.alert.accept()

Resources