web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

formContext.getAttribute cannot find my project id when loading a form

(1) ShareShare
ReportReport
Posted on by 5
This is an odd experience. I am using JavaScript to retrieve a project ID in Project Operations to use dynamically in a FetchXML query to filter a subgrid. I can confirm the FetchXML work as I have tested this in FetchXML Builder and https://www.ashishvishwakarma.com/FetchXmlTester/. The data is returned with no issues. However, whenever I register my OnLoad function, the projectid value is not found. I am loading a project form though:

 
However, checking Level up For Dynamics, the value exists. it has to. I am using the Project entity form: 
 
Here is a snippet of my code. This code contains an OnLoad function to retrieve the project ID and passes it to my on tab change function called 'applyFetchXmlToSubgridOnTabStateChanged'. Can anyone help identify where this plugin code is going wrong or a way around this issue:
 
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.");
    }
}
I have the same question (0)
  • Suggested answer
    Leah Ju Profile Picture
    Microsoft Employee on at
    formContext.getAttribute cannot find my project id when loading a form
    Hi,
    The 'msdyn_projectid' column stores GUID of each project records, which is different from common columns.
    So you need use the following synax to get it in js code:
    formContext.data.entity.getId();
    More details:
    But the resulting GUID value will contain {}, which you'll need to remove in order to use it later in the code:
    Referring to this Blog:
    It should be like this:
    var projectId = formContext.data.entity.getId().replace("{", "").replace("}", "");

    I hope you can mark my answer verified if it answer your question! If you have any questions, please feel free to contact me.
    Regards,
    Leah
     

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 235

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 177

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 156 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans