@@ -3,7 +3,7 @@ import { NamespaceService, SalesforceLabels, SalesforceSchemaService } from '@vl
3
3
import { groupBy , sortBy } from '@vlocode/util' ;
4
4
import { VlocityDatapack , VlocityInterfaceInvoker } from '@vlocode/vlocity' ;
5
5
6
- import { OmniScriptDefinition , OmniScriptSpecification , isChoiceScriptElement , OmniScriptChoiceElementDefinition } from './types' ;
6
+ import { OmniScriptDefinition , OmniScriptSpecification , isChoiceScriptElement , OmniScriptChoiceElementDefinition , OmniScriptPickListOption } from './types' ;
7
7
import { OmniScriptDefinitionFactory } from './omniScriptDefinitionFactory' ;
8
8
import { OmniScriptDefinitionBuilder } from './omniScriptDefinitionBuilder' ;
9
9
import { OmniScriptDefinitionProvider } from './omniScriptDefinitionProvider' ;
@@ -61,9 +61,16 @@ export class OmniScriptDefinitionGenerator implements OmniScriptDefinitionProvid
61
61
}
62
62
}
63
63
64
- // Add Labels and translate them to the current user language
64
+ // Resolve labels
65
65
const labels = await this . labels . getCustomLabels ( Object . keys ( scriptDef . labelKeyMap ) ) ;
66
- scriptDef . labelKeyMap = Object . fromEntries ( Object . values ( labels ) . map ( label => ( [ label . name , label . value ] ) ) ) ;
66
+
67
+ if ( scriptBuilder . isMultiLanguage ) {
68
+ // For multi-language scripts, do not resolve labels here, but rather let the script builder handle it
69
+ scriptDef . labelKeyMap = Object . fromEntries ( Object . values ( labels ) . map ( label => [ label . name , '' ] ) ) ;
70
+ } else {
71
+ // For none multi-language scripts, resolve labels immediately to the user's active language
72
+ scriptDef . labelKeyMap = Object . fromEntries ( Object . values ( labels ) . map ( label => ( [ label . name , label . value ] ) ) ) ;
73
+ }
67
74
68
75
return scriptBuilder . build ( ) ;
69
76
}
@@ -102,13 +109,7 @@ export class OmniScriptDefinitionGenerator implements OmniScriptDefinitionProvid
102
109
}
103
110
104
111
if ( isChoiceScriptElement ( definition ) ) {
105
- if ( builder . realtimePicklistSeed ) {
106
- // Add ru time picklists to definition so they can be resolved at runtime
107
- builder . addRuntimePicklistSource ( definition . propSetMap . optionSource , definition . propSetMap . controllingField ) ;
108
- } else {
109
- // Loading of picklist options can fail
110
- await this . loadOptions ( builder , definition ) ;
111
- }
112
+ await this . setPicklistValues ( builder , definition ) ;
112
113
}
113
114
114
115
try {
@@ -138,37 +139,60 @@ export class OmniScriptDefinitionGenerator implements OmniScriptDefinitionProvid
138
139
}
139
140
}
140
141
141
- private async loadOptions ( builder : OmniScriptDefinitionBuilder , element : OmniScriptChoiceElementDefinition ) {
142
+ private async setPicklistValues ( builder : OmniScriptDefinitionBuilder , element : OmniScriptChoiceElementDefinition ) {
142
143
if ( ! element . propSetMap . optionSource . source ) {
143
144
return ;
144
145
}
145
146
147
+ if ( builder . realtimePicklistSeed ) {
148
+ // Add runtime picklists to definition so they can be resolved at runtime
149
+ builder . setPicklistValues ( element . propSetMap . optionSource , element . propSetMap . controllingField , '' ) ;
150
+ return ;
151
+ }
152
+
153
+ const picklistValues = await this . loadPicklistValues ( builder , element ) ;
154
+ if ( ! picklistValues ) {
155
+ builder . setPicklistValues ( element . propSetMap . optionSource , element . propSetMap . controllingField , picklistValues ) ;
156
+ return ;
157
+ }
158
+
159
+ if ( element . propSetMap . controllingField . type && element . propSetMap . controllingField . element ) {
160
+ element . propSetMap . dependency = picklistValues ;
161
+ } else {
162
+ element . propSetMap . options = picklistValues ;
163
+ }
164
+
165
+ // Stor as Global Picklist otherwise it will not render in standard runtime
166
+ builder . setPicklistValues ( element . propSetMap . optionSource , element . propSetMap . controllingField , picklistValues ) ;
167
+ }
168
+
169
+ private async loadPicklistValues ( builder : OmniScriptDefinitionBuilder , element : OmniScriptChoiceElementDefinition ) {
146
170
if ( element . propSetMap . optionSource . type === 'SObject' ) {
147
171
this . logger . debug ( `Loading picklist options for SObject field: ${ element . propSetMap . optionSource . source } ` ) ;
148
172
if ( element . propSetMap . controllingField . type === 'SObject' && element . propSetMap . controllingField . element ) {
149
- element . propSetMap . dependency = await this . getDependentPicklistOptions ( element . propSetMap . optionSource . source ) ;
150
- } else {
151
- element . propSetMap . options = await this . getPicklistOptions ( element . propSetMap . optionSource . source ) ;
152
- }
173
+ return this . getDependentPicklistOptions ( element . propSetMap . optionSource . source ) ;
174
+ }
175
+ return this . getPicklistOptions ( element . propSetMap . optionSource . source ) ;
153
176
}
154
177
155
178
if ( element . propSetMap . optionSource . type === 'Custom' ) {
156
- if ( element . propSetMap . controllingField . type === 'Custom' && element . propSetMap . controllingField . element ) {
157
- // Let Vlocity handle custom dependent picklist sources at runtime
158
- builder . addRuntimePicklistSource ( element . propSetMap . optionSource , element . propSetMap . controllingField ) ;
159
- } else if ( element . propSetMap . optionSource . source ?. includes ( '.' ) ) {
179
+ if ( element . propSetMap . controllingField . type === 'Custom' &&
180
+ element . propSetMap . controllingField . element
181
+ ) {
182
+ return undefined ;
183
+ }
184
+
185
+ if ( element . propSetMap . optionSource . source ?. includes ( '.' ) ) {
160
186
this . logger . debug ( `Loading picklist options from Custom source: ${ element . propSetMap . optionSource . source } ` ) ;
161
187
try {
162
188
const response = await this . genericInvoker . invoke ( element . propSetMap . optionSource . source ) ;
163
- if ( ! Array . isArray ( response . options ) ) {
164
- element . propSetMap . options = [ { value : `Error ${ element . propSetMap . optionSource . source } returned no options` , name : 'ERR' } ] ;
165
- } else {
166
- element . propSetMap . options = response . options ;
167
- }
189
+ if ( ! Array . isArray ( response ?. options ) ) {
190
+ return [ { value : `Error ${ element . propSetMap . optionSource . source } returned no options` , name : 'ERR' } ] ;
191
+ }
192
+ return response . options ;
168
193
} catch ( err ) {
169
194
// When loading of picklist elements fails instead delegate loading to be done at runtime
170
195
// to avoid blocking the script builder from generating the rest of the script definition
171
- builder . addRuntimePicklistSource ( element . propSetMap . optionSource ) ;
172
196
this . logger . warn ( `Unable to load custom picklist options for script element "${ element . name } ":` , err ) ;
173
197
}
174
198
}
@@ -185,12 +209,12 @@ export class OmniScriptDefinitionGenerator implements OmniScriptDefinitionProvid
185
209
return true ;
186
210
}
187
211
188
- private async getPicklistOptions ( picklist : string ) {
212
+ private async getPicklistOptions ( picklist : string ) : Promise < OmniScriptPickListOption [ ] > {
189
213
const entries = await this . getActivePicklistEntries ( picklist ) ;
190
214
return entries . map ( entry => ( { value : entry . label ?? entry . value , name : entry . value } ) ) ;
191
215
}
192
216
193
- private async getDependentPicklistOptions ( picklist : string ) {
217
+ private async getDependentPicklistOptions ( picklist : string ) : Promise < Record < string , OmniScriptPickListOption [ ] > > {
194
218
const entries = await this . getActivePicklistEntries ( picklist ) ;
195
219
return groupBy ( entries , 'validFor' , entry => ( { value : entry . label ?? entry . value , name : entry . value } ) ) ;
196
220
}
0 commit comments