Dynamics 365 V9.0中Lead disqualify时getSaveMode()返回Undefined问题
Hey there, I’ve dealt with this exact saveMode issue in Dynamics 365 v9.0 when disqualifying leads, so here are some practical workarounds and checks that resolved it for me:
Verify Event Registration Setup
- Make sure your script is registered on the Lead entity’s OnSave event, and set to run synchronously. Async execution in v9.0 can sometimes trigger the script before the save event arguments are fully initialized, leading to undefined values.
- Double-check the execution order of your OnSave scripts—if another script is modifying the event context before yours runs, it might interfere with
getSaveMode().
Use Alternative Identification for Disqualify Actions
SincegetSaveMode()isn’t returning the expected 15, you can bypass it by checking fields tied to the disqualify operation:function onLeadSave(executionContext) { const formContext = executionContext.getFormContext(); // Check if disqualify reason is populated (this is set during disqualify) const disqualifyReason = formContext.getAttribute("disqualonreason").getValue(); if (disqualifyReason) { // Your disqualify-specific logic here console.log("Lead is being disqualified"); } }Alternatively, if your environment supports it, register your script against the OnDisqualify message instead of OnSave—this message is purpose-built for the disqualify action and avoids saveMode ambiguity entirely.
Validate Execution Context and Script Logic
Ensure you’re correctly passing and accessing the execution context:function onLeadSave(executionContext) { const args = executionContext.getEventArgs(); console.log("Save Mode Value:", args.getSaveMode()); console.log("Event Args Object:", args); // Confirm args has the getSaveMode method in console }Open your browser’s dev tools (F12) when performing the disqualify action, and check the console output. If
argsdoesn’t have thegetSaveMode()method, it’s likely a context issue—make sure you’ve selected "Pass execution context as first parameter" when registering the script in Dynamics 365.Update to the Latest v9.0 Patch
Early versions of Dynamics 365 v9.0 had several bugs related to save event arguments. Applying the latest cumulative update for your v9.0 environment might resolve this issue outright, as Microsoft has patched similar context-related bugs in subsequent releases.
Hope these steps help you get past this problem! If you’re still stuck, sharing your full script or event registration details could help narrow things down further.
内容的提问来源于stack exchange,提问作者Jalak Raval

