I am starting in script development, I am performing some test for learning based on microsoft documentation. I am writing an opportunity script to:
- Display a message when a form is loaded
- Assign description text for when a field has its content changed
- Display a message when saved
I tried several ways to run the code, but always get an error message. Can anybody help me?
_________________________________________________________
The code in the link in my previous post will work on Opportunity if you comment out the following 2 lines (the attributes do not exist for Opportunities, but the others in the JavaScript do)
// formContext.getAttribute("websiteurl").setValue("www.contoso.com");
// formContext.getAttribute("telephone1").setValue("425-555-0100");
(also, after you make changes to the JavaScript or the Form make sure you Publish, and sometimes when testing your changes you also need to refresh the Browser window <ctrl><f5> to force the browser to reload cached JavaScript etc).
When I use template code on an account form it works. I'm having trouble when I adapt code to run on an opportunity form.
@Arun, he is attempting to run sample code that is similar but not 100% the same as that provided by Microsoft docs.microsoft.com/.../walkthrough-write-your-first-client-script (I don't particularly like the structure of it or the use of "window" - but it is what it is)
@Edersc, I used the code as provided by MSFT in the link above, hooked up the onLoad and it works without any errors (I didn't hook up any other events) - your original code does not 100% match that from MSFT so not sure if you have copied it from an old version or someone else - anyway try with the code from link in this message. (note, this was on the Account entity and the Account for Interactive Experience form, and accessed it from a '...…. Hub' app).
I believe you are not calling the right method name. refer this: stackoverflow.com/.../7920473
Also as you are using ES6 standards to call using prototype, I'm not sure about it.
Try to fix this line of code
//from
var formContext = executionContext();
//to
var formContext = executionContext.getFormContext(); // get formContext
Thank you Arun for helping me!
I made the changes but the messages are still displayed. Below are the messages:
Error in custom event for this field.
Field: window
Event: onload
Error: undefined
Error in custom event for this field.
Field: crmForm
Event: onsave
Error: undefined
Error in custom event for this field.
Field: name
Event: onchange
Error: undefined
As you advised me, I already selected the field "Pass execution context with a first parameter" and updated the code. With the change follows the new code:
// A namespace defined for the sample code
// As a best practice, you should always define
// a unique namespace for your libraries
var Sdk = window.Sdk || {};
(function () {
// Define some global variables
var myUniqueId = "_myUniqueId"; // Define an ID for the notification
var currentUserName = Xrm.Utility.getGlobalContext().userSettings.userName; // get current user name
var message = currentUserName + ": Your JavaScript code in action!";
// Code to run in the form OnLoad event
this.formOnLoad = function (executionContext) {
var formContext = executionContext.getFormContext();
// display the form level notification as an INFO
formContext.ui.setFormNotification(message, "INFO", myUniqueId);
// Wait for 5 seconds before clearing the notification
window.setTimeout(function () { formContext.ui.clearFormNotification(myUniqueId); }, 5000);
}
// Code to run in the attribute OnChange event
this.attributeOnChange = function (executionContext) {
var formContext = executionContext.getFormContext();
// Automatically set some field values if the account name contains "Contoso"
var Name = formContext.getAttribute("name").getValue();
if (Name.toLowerCase().search("contoso") != -1) {
formContext.getAttribute("description").setValue("www.contoso.com");
}
}
// Code to run in the form OnSave event
this.formOnSave = function () {
// Display an alert dialog
Xrm.Navigation.openAlertDialog({ text: "Record saved." });
}
}).call(Sdk);
Regards,
You haven't shared the error message to understand the issue.
Couple of things to troubleshoot further:
1. Make sure you are passing the parameter by checking the "Pass execution context as first parameter" like explained in the below post
docs.microsoft.com/.../clientapi-execution-context
2. Change the below line:
//buggy code
var formContext = executionContext();
//fixed code
var formContext = executionContext.getFormContext();
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156