var projectId = null;
function onLoadGetProjectId(executionContext) {
var formContext = executionContext.getFormContext(); // Get the form context
// Check if the msdyn_projectid attribute exists and is not null
try {
var projectAttribute = formContext.getAttribute("msdyn_projectid");
if (projectAttribute) {
projectId = projectAttribute.getValue();
if (projectId !== null) {
console.log("Project ID loaded: " + projectId);
} else {
console.error("msdyn_projectid attribute is null.");
}
} else {
console.error("msdyn_projectid attribute not found.");
}
} catch (error) {
console.error("Error accessing msdyn_projectid attribute: " + error.message);
}
// Call the function to apply FetchXML when the tab state changes
applyFetchXmlToSubgridOnTabStateChange(executionContext);
}
function applyFetchXmlToSubgridOnTabStateChange(executionContext) {
var formContext = executionContext.getFormContext();
var tab = formContext.ui.tabs.get("Financials and Sales");
if (tab) {
tab.addTabStateChange(function() {
var subgrid = formContext.getControl("QuoteLineDetailsSubGrid");
if (subgrid && projectId) {
var fetchXml = [
"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>",
" <entity name='msdyn_quotelinetransaction'>",
" <attribute name='msdyn_quotelinetransactionid' />",
" <attribute name='msdyn_description' />",
" <attribute name='createdon' />",
" <attribute name='cen_discipline' />",
" <attribute name='cen_productcode' />",
" <attribute name='cen_productdescription' />",
" <attribute name='cen_quotelinesite' />",
" <attribute name='cen_subdiscipline' />",
" <attribute name='msdyn_amount' />",
" <attribute name='msdyn_enddatetime' />",
" <attribute name='msdyn_price' />",
" <attribute name='msdyn_project' />",
" <attribute name='msdyn_quantity' />",
" <attribute name='msdyn_startdatetime' />",
" <attribute name='msdyn_task' />",
" <attribute name='statecode' />",
" <order attribute='msdyn_description' descending='false' />",
" <filter type='and'>",
" <condition attribute='msdyn_quotelinetransactionid' operator='not-null' />",
" </filter>",
" <link-entity name='quote' from='quoteid' to='msdyn_quoteid' link-type='inner' alias='ag'>",
" <link-entity name='msdyn_project' from='cen_quote' to='quoteid' link-type='inner' alias='ah'>",
" <filter type='and'>",
" <condition attribute='msdyn_projectid' operator='eq' value='" + projectId + "' />",
" </filter>",
" </link-entity>",
" </link-entity>",
" </entity>",
"</fetch>"
].join("");
subgrid.control.SetParameter("fetchXml", fetchXml);
subgrid.refresh(); // Refresh the subgrid to apply the FetchXML
} else {
console.error("Subgrid not found or projectId is null.");
}
});
} else {
console.error("Tab not found.");
}
}