-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform_script_your_name.js
More file actions
130 lines (105 loc) · 5.29 KB
/
form_script_your_name.js
File metadata and controls
130 lines (105 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* jshint esversion: 6 */
/**
* CHANGEME: On this line explain what this does
*
* Author: Nick Hance
* First deploy: 2022-02-08
*/
/**
* Usage instructions:
* 1. Update documentation at top
* 2. Change FormScriptYourName to a unique object name (Global search/replace or just the 2 lines below)
* 3. Add your code
* 4. Add web resource
* 5. Setup onload/onsave hooks. They'll be FormScriptYourName.onSave or FormScriptYourName.onLoad
*/
var FormScriptYourName = FormScriptYourName || {}; // CHANGE THIS!
FormScriptYourName = {
/**
* REFERENCE:
*
* Xrm types: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/xrm
*
* V8 quick links:
* eventContext: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/xrm/v8/index.d.ts#L1077 (EventContext)
* formContext: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/xrm/v8/index.d.ts#L258 (Page)
* xrmContext: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/xrm/v8/index.d.ts#L85 (Xrm.Context)
*/
/*
* The OnSave event occurs when:
* - The user clicks the Save icon in the lower right corner of the form, even when there is no changed data to be saved.
* - Code executes the formContext.data.entity.save method, even when there is no changed data to be saved.
* - The user navigates away from the form and there is unsaved data in the form.
* - The auto-save option is enabled, 30 seconds after data has changed and there is unsaved data in the form.
* - Code executes the formContext.data.save method and there is unsaved data in the form.
* - Code executes the formContext.data.refresh method passing a true value as the first parameter and there is unsaved data in the form.
*
* To determine which button was clicked to perform the save, use the getSaveMode method.
*
* You can cancel the save action by using the preventDefault method within the event arguments object.
* The preventDefault method which is accessible by using the getEventArgs method that is part of the execution context.
*
* Execution context is automatically passed to the form event handler.
*
* Reference: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/events/form-onsave
*/
onSave: function(eventContext) {
const formContext = eventContext.getFormContext();
const xrmContext = eventContext.getContext();
// COPY/PASTE OnSave CODE HERE
},
/*
* The form OnLoad event occurs after the form has loaded. It cannot prevent the window from loading.
* Use the OnLoad event to apply logic about how the form should be displayed, to set properties on fields, and interact with other page elements.
*
* When the Microsoft Dynamics 365 for tablets client is disconnected, the OnLoad event is the only event that will occur.
*
* Reference: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/events/form-onload
*/
onLoad: function(eventContext) {
if (eventContext) {
const formContext = eventContext.getFormContext();
const xrmContext = eventContext.getContext();
// If you pass execution context, put your code here.
// COPY/PASTE OnLoad CODE HERE
/* // Example: Watch an attribute for changes
let name = formContext.getAttribute('crm_name');
name.addOnChange((context) => {
var updated_name = context.getEventSource();
console.log(`name has changed to ${updated_name.getValue()}`);
});
*/
/* // Example: Get a tab by name, get section on a tab, hide it
let tab = formContext.ui.tabs.get('general');
let section = tab.sections.get('section_name_not_display_name');
section.setVisible(false);
*/
} else {
// If you do not pass execution context, your code goes here
}
}
/* Other methods:
* Full reference: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference
*
* Getting an attribute:
* formContext.getAttribute()
* Reference: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/collections/get
* Js reference: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/xrm/v8/index.d.ts#L258
* Examples:
* let itemNumber = formContext.getAttribute('itemNumber');
* let name = formContext.getAttribute('name');
* All Xrm.Attributes types: NumberAttribute, StringAttribute, EnumAttribute, BooleanAttribute, DateAttribute, OptionSetAttribute
*
* Attribute OnChange: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/events/attribute-onchange
* You can use any other function you define in this file as the argument for addOnChange, but you must pass context
* Example:
* let name = formContext.getAttribute('string');
* name.addOnChange((context) => {
* var name = context.getEventSource();
* console.log("name has changed to ", name.getValue());
* })
*
* getValue: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/attributes/getvalue
* Example: (See attribute OnChange example)
*/
}