@@ -10,6 +10,7 @@ const path = require('path');
1010 */
1111module . exports = async function ( vlocity , currentContextData , jobInfo , callback ) {
1212 let listOfCustomLwc = [ ] ;
13+ let processedOmniScripts = new Set ( ) ; // Track processed OmniScripts to avoid duplicates
1314
1415 if ( jobInfo . queries . length > 0 ) {
1516 for ( const element of jobInfo . queries ) {
@@ -18,31 +19,46 @@ module.exports = async function(vlocity, currentContextData, jobInfo, callback)
1819 }
1920 }
2021 }
21-
22+ jobInfo . currentStatus = jobInfo . currentStatus || { } ;
2223
23- vlocity . jsForceConnection . query ( query , function ( err , result ) {
24- if ( err ) { return console . error ( err ) ; }
25- async . eachSeries ( result . records , function ( record , seriesCallback ) {
26-
27- var body = {
28- sClassName : 'Vlocity BuildJSONWithPrefill' ,
29- sType : record [ vlocity . namespace + '__Type__c' ] || record [ 'Type' ] ,
30- sSubType : record [ vlocity . namespace + '__SubType__c' ] || record [ 'SubType' ] ,
31- sLang : record [ vlocity . namespace + '__Language__c' ] || record [ 'Language' ]
32- } ;
33-
34- vlocity . jsForceConnection . apex . post ( '/' + vlocity . namespace + '/v1/GenericInvoke/' , body , async function ( err , prefilledJson ) {
35- if ( err ) { return console . error ( err ) ; }
24+ try {
25+ const result = await vlocity . jsForceConnection . query ( query ) ;
26+
27+ await async . eachSeries ( result . records , async function ( record ) {
28+ try {
29+ // Use the new jsforce v3 approach for Apex REST calls
30+ const apexUrl = '/services/apexrest/' + vlocity . namespace + '/v1/GenericInvoke/' ;
31+ const prefilledJson = await vlocity . jsForceConnection . request ( {
32+ method : 'POST' ,
33+ url : apexUrl ,
34+ body : JSON . stringify ( {
35+ sClassName : 'Vlocity BuildJSONWithPrefill' ,
36+ sType : record [ vlocity . namespace + '__Type__c' ] || record [ 'Type' ] ,
37+ sSubType : record [ vlocity . namespace + '__SubType__c' ] || record [ 'SubType' ] ,
38+ sLang : record [ vlocity . namespace + '__Language__c' ] || record [ 'Language' ]
39+ } ) ,
40+ headers : {
41+ 'Content-Type' : 'application/json'
42+ }
43+ } ) ;
3644
3745 if ( ! vlocity . isOmniStudioInstalled && ( ! record [ vlocity . namespace + '__Type__c' ] || ! record [ vlocity . namespace + '__SubType__c' ] || ! record [ vlocity . namespace + '__Language__c' ] ) ||
38- vlocity . isOmniStudioInstalled && ( ! record [ 'Type' ] || ! record [ 'SubType' ] || ! record [ 'Language' ] )
39- ) return ;
46+ vlocity . isOmniStudioInstalled && ( ! record [ 'Type' ] || ! record [ 'SubType' ] || ! record [ 'Language' ] )
47+ ) {
48+ return ;
49+ }
50+
51+ let lwcname = vlocity . isOmniStudioInstalled ? ( record [ 'Type' ] + record [ 'SubType' ] + record [ 'Language' ] ) : record [ vlocity . namespace + '__Type__c' ] + record [ vlocity . namespace + '__SubType__c' ] + record [ vlocity . namespace + '__Language__c' ] ;
52+
53+ // Skip if this OmniScript has already been processed
54+ if ( processedOmniScripts . has ( lwcname ) ) {
55+ return ;
56+ }
4057
4158 vlocity . datapacksexpand . targetPath = jobInfo . projectPath + '/' + jobInfo . expansionPath ;
4259
4360 listOfCustomLwc = await extractLwcDependencies ( JSON . parse ( prefilledJson ) || { } ) ;
4461
45- let lwcname = vlocity . isOmniStudioInstalled ? ( record [ 'Type' ] + record [ 'SubType' ] + record [ 'Language' ] ) : record [ vlocity . namespace + '__Type__c' ] + record [ vlocity . namespace + '__SubType__c' ] + record [ vlocity . namespace + '__Language__c' ] ;
4662 // add the OmniScript itself
4763 const currentOmniScriptLwc = await fetchOmniOutContents ( lwcname , vlocity ) ;
4864 const parsedOmniScriptRes = await extractSources ( currentOmniScriptLwc [ 'compositeResponse' ] [ 1 ] [ 'body' ] [ 'records' ] , vlocity . namespace ) ;
@@ -55,15 +71,23 @@ module.exports = async function(vlocity, currentContextData, jobInfo, callback)
5571 let parsedSources = await extractSources ( retrieveLwcFile [ 'compositeResponse' ] [ 1 ] [ 'body' ] [ 'records' ] , vlocity . namespace ) ;
5672 await createFiles ( parsedSources , vlocity , 'modules' , `c/${ element } ` ) ;
5773 }
74+ if ( Object . keys ( parsedOmniScriptRes ) . length > 0 ) {
75+ jobInfo . currentStatus [ `${ lwcname } ` ] = 'Success' ;
76+ }
77+
78+ // Mark this OmniScript as processed
79+ processedOmniScripts . add ( lwcname ) ;
5880
59- seriesCallback ( ) ;
60- } , function ( err , result ) {
61- seriesCallback ( ) ;
62- } ) ;
63- } , function ( err , result ) {
64- callback ( ) ;
81+ } catch ( err ) {
82+ VlocityUtils . error ( 'Error processing OmniScript:' , err ) ;
83+ }
6584 } ) ;
66- } ) ;
85+
86+ callback ( ) ;
87+ } catch ( err ) {
88+ VlocityUtils . error ( 'Query error:' , err ) ;
89+ callback ( err ) ;
90+ }
6791} ;
6892
6993/**
@@ -169,7 +193,7 @@ const extractSources = async (items, namespace) => {
169193 let parsed = [ ] ;
170194
171195 if ( items ?. length > 0 ) {
172- items . map ( async ( i ) => {
196+ for ( const i of items ) {
173197 let path = i [ 'FilePath' ] . replace ( 'lwc' , '' ) ;
174198
175199 if ( path . startsWith ( "dc" ) ) {
@@ -179,11 +203,11 @@ const extractSources = async (items, namespace) => {
179203 }
180204
181205 if ( i [ 'Source' ] === "(hidden)" ) {
182- console . log ( 'Skipping path' ) ;
206+ VlocityUtils . log ( 'Skipping path' ) ;
183207 } else {
184208 parsed [ path ] = await parseSource ( i [ 'Source' ] , namespace ) ;
185209 }
186- } ) ;
210+ }
187211 }
188212
189213 resolve ( parsed ) ;
@@ -216,7 +240,7 @@ const createFiles = async(files, vlocity, parentFolder = 'modules', childFolder
216240 try {
217241 for ( const key in files ) {
218242 if ( files . hasOwnProperty ( key ) ) {
219- console . log ( `${ key } : ` ) ;
243+ VlocityUtils . log ( `${ key } : ` ) ;
220244
221245 const filename = path . basename ( key ) || null ;
222246 const fileTypeName = filename . split ( '.' ) || null ;
@@ -227,6 +251,6 @@ const createFiles = async(files, vlocity, parentFolder = 'modules', childFolder
227251 }
228252 }
229253 } catch ( error ) {
230- console . error ( `Got an error trying to write to a file: ${ error . message } ` ) ;
254+ VlocityUtils . error ( `Got an error trying to write to a file: ${ error . message } ` ) ;
231255 }
232- }
256+ }
0 commit comments