From a402218d83c7da132b169e34c8c2e3a3da39e29e Mon Sep 17 00:00:00 2001 From: Daniel Ventura Date: Thu, 24 Nov 2022 15:52:58 +0000 Subject: [PATCH 01/17] #299 creating new apex method to return the current status, and start changing mcdo_retrieveTable in order to handle the new method --- .../classes/mcdo_RunCopadoFunctionFromLWC.cls | 35 +++++++++++++++++++ .../mcdo_RetrieveTable/mcdo_RetrieveTable.js | 16 +++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls b/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls index df2474a..3d036a0 100644 --- a/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls +++ b/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls @@ -129,4 +129,39 @@ public with sharing class mcdo_RunCopadoFunctionFromLWC { throw new AuraHandledException(e.getMessage()); } } + + /** + * This function is getting called from the refreshJobProgress in order to give the most recent data from the respective result record. + * @param jobExecutionId the id of the job execution + * @return Returns resultId, status, lastModified, stepName, progress and isCompleted. + */ + @AuraEnabled + public static jobProgressWrapper getJobProgress(String jobExecutionId) { + // get the newest result associated with this job execution + copado__Result__c[] results = [SELECT Id, LastModifiedDate, + copado__JobStep__r.copado__JobExecution__r.copado__Status__c, + copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c, + copado__Progress_Status__c, + copado__JobStep__r.Name + FROM copado__Result__c + WHERE copado__JobStep__r.copado__JobExecution__c = + :jobExecutionId + WITH SECURITY_ENFORCED ORDER BY CreatedDate DESC LIMIT 1]; + + JobProgressWrapper response = new JobProgressWrapper(); + if(!results.isEmpty()) { + response.resultId = results[0].Id; + response.status = results[0].copado__JobStep__r.copado__JobExecution__r.copado__Status__c; + response.lastModified = results[0].LastModifiedDate; + response.stepName = results[0].copado__JobStep__r.Name; + + // calculate progress with the Job Execution error message, or the last progress status + response.progress = String.isNotEmpty(results[0].copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c) + ?results[0].copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c + :results[0].copado__Progress_Status__c; + response.isCompleted = (response.status == 'Successful' || response.status == 'Error' || response.status == 'Canceled'); + } + + return response; + } } diff --git a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js index d7be55d..1992357 100644 --- a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js +++ b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js @@ -23,7 +23,7 @@ import { } from "lightning/empApi"; // Apex Methods for retrieving and committing metadata (And Communication with the Copado Package) -import ExecuteRetrieveFromCopado from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.executeRetrieve"; +import executeRetrieve from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.executeRetrieve"; import getMetadataFromEnvironment from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getMetadataFromEnvironment"; // Apex functions to retrieve Recorddata from LWC @@ -307,9 +307,18 @@ export default class mcdo_RetrieveTable extends LightningElement { this.loadingState(true, "Starting Retrieve"); try { - const jobExecutionId = await ExecuteRetrieveFromCopado({ + const jobExecutionId = await executeRetrieve({ userStoryId: this.userStoryId }); + + console.log(`jobExecutionId: ${jobExecutionId} - daniel`); + + // this.resultId = details.resultId; + // this.status = details.status; + // this.progress = details.progress; + // this.stepName = details.stepName; + // this.lastModified = details.lastModified; + // this.jobIsRunning = !details.isCompleted; // TODO get result ID from Job step related to job execution //! has to be last result created for that job step if there are multiple this.subscribeToCompletionEvent(jobExecutionId); @@ -325,6 +334,7 @@ export default class mcdo_RetrieveTable extends LightningElement { this.selectedRows = this.selectedRows.map(({ id }) => id); } } + console.log("ended - daniel"); } /** @@ -349,6 +359,8 @@ export default class mcdo_RetrieveTable extends LightningElement { // show progress on screen; try-catch is needed because copado__Payload__c sometimes contains bad JSON const stepStatus = JSON.parse(response.data.payload.copado__Payload__c); this.progressStatus = stepStatus.data.progressStatus || this.progressStatus; + console.log("its not completed yet - daniel"); + console.log(`stepStatus: ${JSON.stringify(stepStatus)} - daniel`); } catch { // ignore } From 857e7faaf5f1bdd4e0897daf1b14cd770ca5d534 Mon Sep 17 00:00:00 2001 From: Daniel Ventura Date: Thu, 24 Nov 2022 17:29:33 +0000 Subject: [PATCH 02/17] #299 adding the other functions, but still with the window.setTimeout error --- .../mcdo_RetrieveTable/mcdo_RetrieveTable.js | 74 +++++++++++++++++-- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js index 1992357..c82243c 100644 --- a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js +++ b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js @@ -24,6 +24,7 @@ import { // Apex Methods for retrieving and committing metadata (And Communication with the Copado Package) import executeRetrieve from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.executeRetrieve"; +import getJobProgress from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getJobProgress"; import getMetadataFromEnvironment from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getMetadataFromEnvironment"; // Apex functions to retrieve Recorddata from LWC @@ -305,6 +306,13 @@ export default class mcdo_RetrieveTable extends LightningElement { */ async retrieve() { this.loadingState(true, "Starting Retrieve"); + // update the UI immediately, clearing any previous values + this.jobExecutionId = null; + this.progressStatus = "Requesting"; + this.lastModified = new Date(); + this.progress = ""; + this.jobIsRunning = true; + this.showProgress = true; try { const jobExecutionId = await executeRetrieve({ @@ -313,12 +321,11 @@ export default class mcdo_RetrieveTable extends LightningElement { console.log(`jobExecutionId: ${jobExecutionId} - daniel`); - // this.resultId = details.resultId; - // this.status = details.status; - // this.progress = details.progress; - // this.stepName = details.stepName; - // this.lastModified = details.lastModified; - // this.jobIsRunning = !details.isCompleted; + // Start the polling process to track the job via UI + this.jobExecutionId = jobExecutionId; + this.jobExecutionIdUrl = "/" + jobExecutionId; + this.poll(() => this.refreshJobProgress()); + // TODO get result ID from Job step related to job execution //! has to be last result created for that job step if there are multiple this.subscribeToCompletionEvent(jobExecutionId); @@ -337,6 +344,61 @@ export default class mcdo_RetrieveTable extends LightningElement { console.log("ended - daniel"); } + // read the job progress and assign the UI properties + async refreshJobProgress() { + try { + let details = await getJobProgress({ jobExecutionId: this.jobExecutionId }); + this.resultId = details.resultId; + this.status = details.status; + this.progress = details.progress; + this.stepName = details.stepName; + this.lastModified = details.lastModified; + this.jobIsRunning = !details.isCompleted; + } catch (error) { + console.error( + "There was an error trying to read the progress of the job. Will retry.", + error + ); + } + } + + // LWC METHODS + disconnectedCallback() { + this.stopPoll(); // ensure we disconnect the polling. + } + + // call functionCall and if it returns true, call again every 5s for the first 5m, 10s after 5m, 30s after 30m, 60s after 1h + poll(functionCall, pollStartTime) { + if (!pollStartTime) { + this.pollStop = false; + pollStartTime = new Date().getTime(); + } + if (!functionCall.apply() || this.pollStop) { + return this.stopPoll(); + } + let elapsedMs = new Date().getTime() - pollStartTime; + let intervalMs = + elapsedMs > 3600000 + ? 60000 + : elapsedMs > 1800000 + ? 30000 + : elapsedMs > 300000 + ? 10000 + : 5000; + this.pollProcessId = window.setTimeout( + () => this.poll(functionCall, pollStartTime), + intervalMs + ); + } + // use this method to stop the polling, e.g. from the UI or when disconnectedCallback occurs + stopPoll() { + this.pollStop = true; + if (this.pollProcessId) { + window.clearTimeout(this.pollProcessId); + this.pollProcessId = null; + } + } + /** * helper for retrieve() * @param {string} jobExecutionId sfid From 7dccdac305060fac8da94026d21de7992bad7559 Mon Sep 17 00:00:00 2001 From: Daniel Ventura Date: Thu, 24 Nov 2022 18:22:24 +0000 Subject: [PATCH 03/17] #299 changed the from this.status to this.progressStatus --- .../main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js index c82243c..0d7e865 100644 --- a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js +++ b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js @@ -349,7 +349,7 @@ export default class mcdo_RetrieveTable extends LightningElement { try { let details = await getJobProgress({ jobExecutionId: this.jobExecutionId }); this.resultId = details.resultId; - this.status = details.status; + this.progressStatus = details.status; this.progress = details.progress; this.stepName = details.stepName; this.lastModified = details.lastModified; From 638202b1ddd8f50c69cb2a34d611bddb8e7464f3 Mon Sep 17 00:00:00 2001 From: Daniel Ventura Date: Fri, 25 Nov 2022 10:12:04 +0000 Subject: [PATCH 04/17] #299 change the mcdo_RunCopadoFunctionFromLWC.cls and mcdo_RetrieveTable.js to its previous state, once that this logic wont work, and will try to use the event's logic --- .../classes/mcdo_RunCopadoFunctionFromLWC.cls | 37 +-------- .../mcdo_RetrieveTable/mcdo_RetrieveTable.js | 80 +------------------ 2 files changed, 4 insertions(+), 113 deletions(-) diff --git a/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls b/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls index 3d036a0..234984b 100644 --- a/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls +++ b/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls @@ -129,39 +129,4 @@ public with sharing class mcdo_RunCopadoFunctionFromLWC { throw new AuraHandledException(e.getMessage()); } } - - /** - * This function is getting called from the refreshJobProgress in order to give the most recent data from the respective result record. - * @param jobExecutionId the id of the job execution - * @return Returns resultId, status, lastModified, stepName, progress and isCompleted. - */ - @AuraEnabled - public static jobProgressWrapper getJobProgress(String jobExecutionId) { - // get the newest result associated with this job execution - copado__Result__c[] results = [SELECT Id, LastModifiedDate, - copado__JobStep__r.copado__JobExecution__r.copado__Status__c, - copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c, - copado__Progress_Status__c, - copado__JobStep__r.Name - FROM copado__Result__c - WHERE copado__JobStep__r.copado__JobExecution__c = - :jobExecutionId - WITH SECURITY_ENFORCED ORDER BY CreatedDate DESC LIMIT 1]; - - JobProgressWrapper response = new JobProgressWrapper(); - if(!results.isEmpty()) { - response.resultId = results[0].Id; - response.status = results[0].copado__JobStep__r.copado__JobExecution__r.copado__Status__c; - response.lastModified = results[0].LastModifiedDate; - response.stepName = results[0].copado__JobStep__r.Name; - - // calculate progress with the Job Execution error message, or the last progress status - response.progress = String.isNotEmpty(results[0].copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c) - ?results[0].copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c - :results[0].copado__Progress_Status__c; - response.isCompleted = (response.status == 'Successful' || response.status == 'Error' || response.status == 'Canceled'); - } - - return response; - } -} +} \ No newline at end of file diff --git a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js index 0d7e865..27a1847 100644 --- a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js +++ b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js @@ -23,8 +23,7 @@ import { } from "lightning/empApi"; // Apex Methods for retrieving and committing metadata (And Communication with the Copado Package) -import executeRetrieve from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.executeRetrieve"; -import getJobProgress from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getJobProgress"; +import ExecuteRetrieveFromCopado from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.executeRetrieve"; import getMetadataFromEnvironment from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getMetadataFromEnvironment"; // Apex functions to retrieve Recorddata from LWC @@ -306,26 +305,11 @@ export default class mcdo_RetrieveTable extends LightningElement { */ async retrieve() { this.loadingState(true, "Starting Retrieve"); - // update the UI immediately, clearing any previous values - this.jobExecutionId = null; - this.progressStatus = "Requesting"; - this.lastModified = new Date(); - this.progress = ""; - this.jobIsRunning = true; - this.showProgress = true; try { - const jobExecutionId = await executeRetrieve({ + const jobExecutionId = await ExecuteRetrieveFromCopado({ userStoryId: this.userStoryId }); - - console.log(`jobExecutionId: ${jobExecutionId} - daniel`); - - // Start the polling process to track the job via UI - this.jobExecutionId = jobExecutionId; - this.jobExecutionIdUrl = "/" + jobExecutionId; - this.poll(() => this.refreshJobProgress()); - // TODO get result ID from Job step related to job execution //! has to be last result created for that job step if there are multiple this.subscribeToCompletionEvent(jobExecutionId); @@ -341,62 +325,6 @@ export default class mcdo_RetrieveTable extends LightningElement { this.selectedRows = this.selectedRows.map(({ id }) => id); } } - console.log("ended - daniel"); - } - - // read the job progress and assign the UI properties - async refreshJobProgress() { - try { - let details = await getJobProgress({ jobExecutionId: this.jobExecutionId }); - this.resultId = details.resultId; - this.progressStatus = details.status; - this.progress = details.progress; - this.stepName = details.stepName; - this.lastModified = details.lastModified; - this.jobIsRunning = !details.isCompleted; - } catch (error) { - console.error( - "There was an error trying to read the progress of the job. Will retry.", - error - ); - } - } - - // LWC METHODS - disconnectedCallback() { - this.stopPoll(); // ensure we disconnect the polling. - } - - // call functionCall and if it returns true, call again every 5s for the first 5m, 10s after 5m, 30s after 30m, 60s after 1h - poll(functionCall, pollStartTime) { - if (!pollStartTime) { - this.pollStop = false; - pollStartTime = new Date().getTime(); - } - if (!functionCall.apply() || this.pollStop) { - return this.stopPoll(); - } - let elapsedMs = new Date().getTime() - pollStartTime; - let intervalMs = - elapsedMs > 3600000 - ? 60000 - : elapsedMs > 1800000 - ? 30000 - : elapsedMs > 300000 - ? 10000 - : 5000; - this.pollProcessId = window.setTimeout( - () => this.poll(functionCall, pollStartTime), - intervalMs - ); - } - // use this method to stop the polling, e.g. from the UI or when disconnectedCallback occurs - stopPoll() { - this.pollStop = true; - if (this.pollProcessId) { - window.clearTimeout(this.pollProcessId); - this.pollProcessId = null; - } } /** @@ -421,8 +349,6 @@ export default class mcdo_RetrieveTable extends LightningElement { // show progress on screen; try-catch is needed because copado__Payload__c sometimes contains bad JSON const stepStatus = JSON.parse(response.data.payload.copado__Payload__c); this.progressStatus = stepStatus.data.progressStatus || this.progressStatus; - console.log("its not completed yet - daniel"); - console.log(`stepStatus: ${JSON.stringify(stepStatus)} - daniel`); } catch { // ignore } @@ -678,4 +604,4 @@ export default class mcdo_RetrieveTable extends LightningElement { this.showTable = !isLoading; this.refreshButtonDisabled = isLoading; } -} +} \ No newline at end of file From 3b79faf740ebe1ce08346859e233d882a942c6d0 Mon Sep 17 00:00:00 2001 From: "Castro Ventura, Daniel" Date: Mon, 28 Nov 2022 16:09:25 +0000 Subject: [PATCH 05/17] #299 - added some console.logs --- .../mcdo_RetrieveTable/mcdo_RetrieveTable.js | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js index 27a1847..44f96df 100644 --- a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js +++ b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js @@ -163,9 +163,10 @@ export default class mcdo_RetrieveTable extends LightningElement { channelName = "/event/copado__Event__e"; _subscribeToMessageService() { - subscribeMessageService(this._context, COMMIT_PAGE_COMMUNICATION_CHANNEL, (message) => - this._handleCommitPageCommunicationMessage(message) - ); + subscribeMessageService(this._context, COMMIT_PAGE_COMMUNICATION_CHANNEL, (message) => { + console.log(`message: ${message}`); + this._handleCommitPageCommunicationMessage(message); + }); } /** @@ -334,12 +335,20 @@ export default class mcdo_RetrieveTable extends LightningElement { */ async subscribeToCompletionEvent(jobExecutionId) { const messageCallback = async (response) => { + console.log( + `response.data.payload.copado__Topic_Uri__c: ${response.data.payload.copado__Topic_Uri__c}` + ); + console.log( + `response.data.payload.copado__Payload__c: ${response.data.payload.copado__Payload__c}` + ); + console.log(`response.data.payload: ${response.data.payload}`); if ( response.data.payload.copado__Topic_Uri__c === `/execution-completed/${jobExecutionId}` ) { // retrieve is done: refresh table with new data this.updateMetadataGrid(response, jobExecutionId); + console.log("its completed"); } else if ( response.data.payload.copado__Topic_Uri__c.startsWith( "/events/copado/v1/step-monitor/" // + resultId @@ -351,12 +360,15 @@ export default class mcdo_RetrieveTable extends LightningElement { this.progressStatus = stepStatus.data.progressStatus || this.progressStatus; } catch { // ignore + console.log("its not yet completed"); } } }; try { + console.log(`this.channelName: ${this.channelName}`); this.empSubscription = await subscribeEmp(this.channelName, -1, messageCallback); + console.log(`this.empSubscription: ${JSON.stringify(this.empSubscription)}`); } catch (err) { this.showError( `${err.name}: An error occurred while subscribing to Emp API`, @@ -604,4 +616,4 @@ export default class mcdo_RetrieveTable extends LightningElement { this.showTable = !isLoading; this.refreshButtonDisabled = isLoading; } -} \ No newline at end of file +} From 89cc1679dad8f04c2a7c5e1e20546668bac971af Mon Sep 17 00:00:00 2001 From: "Castro Ventura, Daniel" Date: Mon, 28 Nov 2022 16:21:05 +0000 Subject: [PATCH 06/17] #299 - updated some console.logs --- .../mcdo_RetrieveTable/mcdo_RetrieveTable.js | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js index 44f96df..2f47abd 100644 --- a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js +++ b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js @@ -148,6 +148,9 @@ export default class mcdo_RetrieveTable extends LightningElement { sortDirection = "desc"; sortedBy = "ld"; + // Stages + // stages = ["Retrieve.js started", ""]; + // Search Functionality related variables keyword; allSelectedRows = []; @@ -164,7 +167,6 @@ export default class mcdo_RetrieveTable extends LightningElement { _subscribeToMessageService() { subscribeMessageService(this._context, COMMIT_PAGE_COMMUNICATION_CHANNEL, (message) => { - console.log(`message: ${message}`); this._handleCommitPageCommunicationMessage(message); }); } @@ -335,13 +337,13 @@ export default class mcdo_RetrieveTable extends LightningElement { */ async subscribeToCompletionEvent(jobExecutionId) { const messageCallback = async (response) => { - console.log( - `response.data.payload.copado__Topic_Uri__c: ${response.data.payload.copado__Topic_Uri__c}` - ); - console.log( - `response.data.payload.copado__Payload__c: ${response.data.payload.copado__Payload__c}` - ); - console.log(`response.data.payload: ${response.data.payload}`); + // console.log( + // `response.data.payload.copado__Topic_Uri__c: ${response.data.payload.copado__Topic_Uri__c}` + // ); + // console.log( + // `response.data.payload.copado__Payload__c: ${response.data.payload.copado__Payload__c}` + // ); + console.log(`response.data.payload: ${JSON.stringify(response.data.payload)}`); if ( response.data.payload.copado__Topic_Uri__c === `/execution-completed/${jobExecutionId}` @@ -366,9 +368,7 @@ export default class mcdo_RetrieveTable extends LightningElement { }; try { - console.log(`this.channelName: ${this.channelName}`); this.empSubscription = await subscribeEmp(this.channelName, -1, messageCallback); - console.log(`this.empSubscription: ${JSON.stringify(this.empSubscription)}`); } catch (err) { this.showError( `${err.name}: An error occurred while subscribing to Emp API`, From eecf6c01a7d8dfc7038895cbf736bc1f1e117cb5 Mon Sep 17 00:00:00 2001 From: Daniel Ventura Date: Tue, 29 Nov 2022 12:36:12 +0000 Subject: [PATCH 07/17] #299 adding a method in order to get the job progress --- .../classes/mcdo_RunCopadoFunctionFromLWC.cls | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls b/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls index 234984b..9ac337b 100644 --- a/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls +++ b/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls @@ -129,4 +129,40 @@ public with sharing class mcdo_RunCopadoFunctionFromLWC { throw new AuraHandledException(e.getMessage()); } } + + public static jobProgressWrapper getJobProgress(String jobExecutionId) + { + // get the newest result associated with this job execution + copado__Result__c[] results = [SELECT Id, LastModifiedDate, + copado__JobStep__r.copado__JobExecution__r.copado__Status__c, + copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c, + copado__Progress_Status__c, + copado__JobStep__r.Name + FROM copado__Result__c + WHERE copado__JobStep__r.copado__JobExecution__c = :jobExecutionId + WITH SECURITY_ENFORCED ORDER BY CreatedDate DESC LIMIT 1]; + JobProgressWrapper response = new JobProgressWrapper(); + if(!results.isEmpty()) { + response.resultId = results[0].Id; + response.status = results[0].copado__JobStep__r.copado__JobExecution__r.copado__Status__c; + response.lastModified = results[0].LastModifiedDate; + response.stepName = results[0].copado__JobStep__r.Name; + // calculate progress with the Job Execution error message, or the last progress status + response.progress = + String.isNotEmpty(results[0].copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c) + ?results[0].copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c + :results[0].copado__Progress_Status__c; + response.isCompleted = (response.status == 'Successful' || response.status == 'Error' || response.status == 'Canceled'); + } + return response; + } + + public class JobProgressWrapper { + @AuraEnabled public String resultId = ''; + @AuraEnabled public String status = ''; + @AuraEnabled public String progress = ''; + @AuraEnabled public Datetime lastModified = null; + @AuraEnabled public String stepName = ''; + @AuraEnabled public Boolean isCompleted = false; + } } \ No newline at end of file From ed2284eec0bb29ec06a9ca65f47e8765f74a88f7 Mon Sep 17 00:00:00 2001 From: Daniel Ventura Date: Tue, 29 Nov 2022 12:37:01 +0000 Subject: [PATCH 08/17] #299 using that method to retrieve the job progress on the message call back --- .../mcdo_RetrieveTable/mcdo_RetrieveTable.js | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js index 2f47abd..6612ec7 100644 --- a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js +++ b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js @@ -23,10 +23,10 @@ import { } from "lightning/empApi"; // Apex Methods for retrieving and committing metadata (And Communication with the Copado Package) +// Apex functions to retrieve Recorddata from LWC import ExecuteRetrieveFromCopado from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.executeRetrieve"; import getMetadataFromEnvironment from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getMetadataFromEnvironment"; - -// Apex functions to retrieve Recorddata from LWC +import getJobProgress from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getJobProgress"; // "Commit Changes" Page Tab related import COMMIT_PAGE_COMMUNICATION_CHANNEL from "@salesforce/messageChannel/copado__CommitPageCommunication__c"; @@ -337,13 +337,6 @@ export default class mcdo_RetrieveTable extends LightningElement { */ async subscribeToCompletionEvent(jobExecutionId) { const messageCallback = async (response) => { - // console.log( - // `response.data.payload.copado__Topic_Uri__c: ${response.data.payload.copado__Topic_Uri__c}` - // ); - // console.log( - // `response.data.payload.copado__Payload__c: ${response.data.payload.copado__Payload__c}` - // ); - console.log(`response.data.payload: ${JSON.stringify(response.data.payload)}`); if ( response.data.payload.copado__Topic_Uri__c === `/execution-completed/${jobExecutionId}` @@ -362,13 +355,22 @@ export default class mcdo_RetrieveTable extends LightningElement { this.progressStatus = stepStatus.data.progressStatus || this.progressStatus; } catch { // ignore - console.log("its not yet completed"); } + // call an apex function that got the result status + getJobProgress({ jobExecutionId: jobExecutionId }) + .then((result) => { + console.log(JSON.stringify(result)); + this.progressStatus = result.progress || result.status; + }) + .catch((error) => { + console.error(error); + }); } }; try { - this.empSubscription = await subscribeEmp(this.channelName, -1, messageCallback); + console.log(`this.channelName: ${this.channelName}`); + this.empSubscription = await subscribeEmp(this.channelName, -2, messageCallback); } catch (err) { this.showError( `${err.name}: An error occurred while subscribing to Emp API`, From b856c46b1b3521c63e5cfd128273e28ab8410831 Mon Sep 17 00:00:00 2001 From: Daniel Ventura Date: Tue, 29 Nov 2022 17:03:07 +0000 Subject: [PATCH 09/17] #299 changed the event that we are getting information about and change the logic as well so it can get the appropriate data, but it still have a bug, it is refreshing the table before the event actually ended --- .../mcdo_RetrieveTable/mcdo_RetrieveTable.js | 32 ++++--------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js index 6612ec7..f4814d7 100644 --- a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js +++ b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js @@ -148,9 +148,6 @@ export default class mcdo_RetrieveTable extends LightningElement { sortDirection = "desc"; sortedBy = "ld"; - // Stages - // stages = ["Retrieve.js started", ""]; - // Search Functionality related variables keyword; allSelectedRows = []; @@ -163,7 +160,7 @@ export default class mcdo_RetrieveTable extends LightningElement { // Subscription related variables empSubscription = {}; - channelName = "/event/copado__Event__e"; + channelName = "/event/copado__MC_Result__e"; _subscribeToMessageService() { subscribeMessageService(this._context, COMMIT_PAGE_COMMUNICATION_CHANNEL, (message) => { @@ -337,29 +334,13 @@ export default class mcdo_RetrieveTable extends LightningElement { */ async subscribeToCompletionEvent(jobExecutionId) { const messageCallback = async (response) => { - if ( - response.data.payload.copado__Topic_Uri__c === - `/execution-completed/${jobExecutionId}` - ) { + if (response.data.payload.copado__Progress_Status__c === "Refresh done") { // retrieve is done: refresh table with new data this.updateMetadataGrid(response, jobExecutionId); - console.log("its completed"); - } else if ( - response.data.payload.copado__Topic_Uri__c.startsWith( - "/events/copado/v1/step-monitor/" // + resultId - ) - ) { - try { - // show progress on screen; try-catch is needed because copado__Payload__c sometimes contains bad JSON - const stepStatus = JSON.parse(response.data.payload.copado__Payload__c); - this.progressStatus = stepStatus.data.progressStatus || this.progressStatus; - } catch { - // ignore - } + } else { // call an apex function that got the result status getJobProgress({ jobExecutionId: jobExecutionId }) .then((result) => { - console.log(JSON.stringify(result)); this.progressStatus = result.progress || result.status; }) .catch((error) => { @@ -369,8 +350,7 @@ export default class mcdo_RetrieveTable extends LightningElement { }; try { - console.log(`this.channelName: ${this.channelName}`); - this.empSubscription = await subscribeEmp(this.channelName, -2, messageCallback); + this.empSubscription = await subscribeEmp(this.channelName, -1, messageCallback); } catch (err) { this.showError( `${err.name}: An error occurred while subscribing to Emp API`, @@ -394,8 +374,8 @@ export default class mcdo_RetrieveTable extends LightningElement { err.message ); } - const jobExecution = JSON.parse(response.data.payload.copado__Payload__c); - if (jobExecution.copado__Status__c === "Successful") { + const jobExecution = response.data.payload; + if (jobExecution.copado__Progress_Status__c === "Refresh done") { try { const result = JSON.parse( await getMetadataFromEnvironment({ userStoryId: this.userStoryId }) From 7286b498c585192ac099912190ce2d8ff3a829c7 Mon Sep 17 00:00:00 2001 From: Daniel Ventura Date: Wed, 30 Nov 2022 13:14:29 +0000 Subject: [PATCH 10/17] #299 changing logic to use both events, one to update the progress messages, and the other to refresh the table once it the retrieve is done. New bug: sometimes the messages get confused with some other job steps (I think) --- .../mcdo_RetrieveTable/mcdo_RetrieveTable.js | 65 ++++++++++++++----- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js index f4814d7..f5e9d5e 100644 --- a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js +++ b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js @@ -159,8 +159,10 @@ export default class mcdo_RetrieveTable extends LightningElement { progressStatus = "Loading data"; // Subscription related variables - empSubscription = {}; - channelName = "/event/copado__MC_Result__e"; + getProgressSubscription = {}; + reloadTableSubscription = {}; + resultChannelName = "/event/copado__MC_Result__e"; + eventChannelName = "/event/copado__Event__e"; _subscribeToMessageService() { subscribeMessageService(this._context, COMMIT_PAGE_COMMUNICATION_CHANNEL, (message) => { @@ -333,14 +335,14 @@ export default class mcdo_RetrieveTable extends LightningElement { * @returns {Promise} resolves when the job is done */ async subscribeToCompletionEvent(jobExecutionId) { - const messageCallback = async (response) => { + const progressMessageCallback = async (response) => { if (response.data.payload.copado__Progress_Status__c === "Refresh done") { - // retrieve is done: refresh table with new data - this.updateMetadataGrid(response, jobExecutionId); + this.unsubscribeThisSubscription(this.getProgressSubscription); } else { // call an apex function that got the result status getJobProgress({ jobExecutionId: jobExecutionId }) .then((result) => { + console.log(JSON.stringify(result)); this.progressStatus = result.progress || result.status; }) .catch((error) => { @@ -349,8 +351,35 @@ export default class mcdo_RetrieveTable extends LightningElement { } }; + const reloadTableCallBack = async (response) => { + if ( + response.data.payload.copado__Topic_Uri__c === + `/execution-completed/${jobExecutionId}` + ) { + // retrieve is done: refresh table with new data + this.updateMetadataGrid(response, jobExecutionId); + } + }; + try { - this.empSubscription = await subscribeEmp(this.channelName, -1, messageCallback); + this.getProgressSubscription = await subscribeEmp( + this.resultChannelName, + -1, + progressMessageCallback + ); + } catch (err) { + this.showError( + `${err.name}: An error occurred while subscribing to Emp API`, + err.message + ); + } + + try { + this.reloadTableSubscription = await subscribeEmp( + this.eventChannelName, + -1, + reloadTableCallBack + ); } catch (err) { this.showError( `${err.name}: An error occurred while subscribing to Emp API`, @@ -359,23 +388,27 @@ export default class mcdo_RetrieveTable extends LightningElement { } } - /** - * helper for retrieve() called when refreshing metadata is done to update the table - * @param {object} response empApi response - * @param {string} jobExecutionId sfid - * @returns {Promise} resolves when the job is done - */ - async updateMetadataGrid(response, jobExecutionId) { + async unsubscribeThisSubscription(subscription) { try { - unsubscribeEmp(this.empSubscription); + unsubscribeEmp(subscription); } catch (err) { this.showError( `${err.name}: An error occurred while unsubscribing from Emp API`, err.message ); } - const jobExecution = response.data.payload; - if (jobExecution.copado__Progress_Status__c === "Refresh done") { + } + + /** + * helper for retrieve() called when refreshing metadata is done to update the table + * @param {object} response empApi response + * @param {string} jobExecutionId sfid + * @returns {Promise} resolves when the job is done + */ + async updateMetadataGrid(response, jobExecutionId) { + this.unsubscribeThisSubscription(this.reloadTableSubscription); + const jobExecution = JSON.parse(response.data.payload.copado__Payload__c); + if (jobExecution.copado__Status__c === "Successful") { try { const result = JSON.parse( await getMetadataFromEnvironment({ userStoryId: this.userStoryId }) From 4c774d0f20ef0e5047fdb5751601ea542656dfd4 Mon Sep 17 00:00:00 2001 From: Daniel Ventura Date: Wed, 30 Nov 2022 16:08:22 +0000 Subject: [PATCH 11/17] #299 fixing the bug, it wasn't unsubscribing, and so it was continuing to get message from the older subscriptions as well --- .../main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js index f5e9d5e..6a439f3 100644 --- a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js +++ b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js @@ -338,11 +338,11 @@ export default class mcdo_RetrieveTable extends LightningElement { const progressMessageCallback = async (response) => { if (response.data.payload.copado__Progress_Status__c === "Refresh done") { this.unsubscribeThisSubscription(this.getProgressSubscription); + this.progressStatus = "Completed!"; } else { // call an apex function that got the result status getJobProgress({ jobExecutionId: jobExecutionId }) .then((result) => { - console.log(JSON.stringify(result)); this.progressStatus = result.progress || result.status; }) .catch((error) => { @@ -390,7 +390,7 @@ export default class mcdo_RetrieveTable extends LightningElement { async unsubscribeThisSubscription(subscription) { try { - unsubscribeEmp(subscription); + unsubscribeEmp(subscription, () => {}); } catch (err) { this.showError( `${err.name}: An error occurred while unsubscribing from Emp API`, From 134ae1c107d2295382c703cff78659d71cddcd38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Berkefeld?= Date: Mon, 5 Dec 2022 09:58:48 +0100 Subject: [PATCH 12/17] #299: prototype of getResultIds still buggy; jobProgressWrapper & getJobProgress will be irrelevant when this works --- .../classes/mcdo_RunCopadoFunctionFromLWC.cls | 82 +++++++++++++------ .../mcdo_RetrieveTable/mcdo_RetrieveTable.js | 21 ++--- 2 files changed, 70 insertions(+), 33 deletions(-) diff --git a/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls b/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls index 9ac337b..7422338 100644 --- a/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls +++ b/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls @@ -130,39 +130,75 @@ public with sharing class mcdo_RunCopadoFunctionFromLWC { } } - public static jobProgressWrapper getJobProgress(String jobExecutionId) - { + @AuraEnabled + public static List getResultIds(String jobExecutionId) { // get the newest result associated with this job execution - copado__Result__c[] results = [SELECT Id, LastModifiedDate, - copado__JobStep__r.copado__JobExecution__r.copado__Status__c, - copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c, - copado__Progress_Status__c, - copado__JobStep__r.Name + copado__Result__c[] results = [ + SELECT Id FROM copado__Result__c WHERE copado__JobStep__r.copado__JobExecution__c = :jobExecutionId - WITH SECURITY_ENFORCED ORDER BY CreatedDate DESC LIMIT 1]; + WITH SECURITY_ENFORCED + ORDER BY CreatedDate DESC + LIMIT 1 + ]; + List ids = new List(); + + for (copado__Result__c item : results) { + ids.add(item.Id); + } + + return ids; + } + + @AuraEnabled + public static jobProgressWrapper getJobProgress(String jobExecutionId) { + // get the newest result associated with this job execution + copado__Result__c[] results = [ + SELECT + Id, + LastModifiedDate, + copado__JobStep__r.copado__JobExecution__r.copado__Status__c, + copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c, + copado__Progress_Status__c, + copado__JobStep__r.Name + FROM copado__Result__c + WHERE copado__JobStep__r.copado__JobExecution__c = :jobExecutionId + WITH SECURITY_ENFORCED + ORDER BY CreatedDate DESC + LIMIT 1 + ]; JobProgressWrapper response = new JobProgressWrapper(); - if(!results.isEmpty()) { + if (!results.isEmpty()) { response.resultId = results[0].Id; - response.status = results[0].copado__JobStep__r.copado__JobExecution__r.copado__Status__c; + response.status = results[0] + .copado__JobStep__r.copado__JobExecution__r.copado__Status__c; response.lastModified = results[0].LastModifiedDate; response.stepName = results[0].copado__JobStep__r.Name; // calculate progress with the Job Execution error message, or the last progress status - response.progress = - String.isNotEmpty(results[0].copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c) - ?results[0].copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c - :results[0].copado__Progress_Status__c; - response.isCompleted = (response.status == 'Successful' || response.status == 'Error' || response.status == 'Canceled'); + response.progress = String.isNotEmpty( + results[0].copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c + ) + ? results[0].copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c + : results[0].copado__Progress_Status__c; + response.isCompleted = (response.status == 'Successful' || + response.status == 'Error' || + response.status == 'Canceled'); } return response; } - + public class JobProgressWrapper { - @AuraEnabled public String resultId = ''; - @AuraEnabled public String status = ''; - @AuraEnabled public String progress = ''; - @AuraEnabled public Datetime lastModified = null; - @AuraEnabled public String stepName = ''; - @AuraEnabled public Boolean isCompleted = false; + @AuraEnabled + public String resultId = ''; + @AuraEnabled + public String status = ''; + @AuraEnabled + public String progress = ''; + @AuraEnabled + public Datetime lastModified = null; + @AuraEnabled + public String stepName = ''; + @AuraEnabled + public Boolean isCompleted = false; } -} \ No newline at end of file +} diff --git a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js index 6a439f3..4a06513 100644 --- a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js +++ b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js @@ -26,7 +26,7 @@ import { // Apex functions to retrieve Recorddata from LWC import ExecuteRetrieveFromCopado from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.executeRetrieve"; import getMetadataFromEnvironment from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getMetadataFromEnvironment"; -import getJobProgress from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getJobProgress"; +import getResultIds from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getResultIds"; // "Commit Changes" Page Tab related import COMMIT_PAGE_COMMUNICATION_CHANNEL from "@salesforce/messageChannel/copado__CommitPageCommunication__c"; @@ -312,7 +312,6 @@ export default class mcdo_RetrieveTable extends LightningElement { const jobExecutionId = await ExecuteRetrieveFromCopado({ userStoryId: this.userStoryId }); - // TODO get result ID from Job step related to job execution //! has to be last result created for that job step if there are multiple this.subscribeToCompletionEvent(jobExecutionId); } catch (error) { @@ -335,19 +334,21 @@ export default class mcdo_RetrieveTable extends LightningElement { * @returns {Promise} resolves when the job is done */ async subscribeToCompletionEvent(jobExecutionId) { + // get result ID from Job step related to job execution + this.currentResultIds = await getResultIds(jobExecutionId); + console.log("currentResultIds", this.currentResultIds); + const progressMessageCallback = async (response) => { if (response.data.payload.copado__Progress_Status__c === "Refresh done") { this.unsubscribeThisSubscription(this.getProgressSubscription); this.progressStatus = "Completed!"; } else { - // call an apex function that got the result status - getJobProgress({ jobExecutionId: jobExecutionId }) - .then((result) => { - this.progressStatus = result.progress || result.status; - }) - .catch((error) => { - console.error(error); - }); + if ( + this.currentResultIds.includes(response?.data?.payload?.copado__ResultId__c) && + response?.data?.payload?.copado__Progress_Status__c + ) { + this.progressStatus = response?.data?.payload?.copado__Progress_Status__c; + } } }; From a80e1281f4c7433f69660b418ccce1ac17b3afbc Mon Sep 17 00:00:00 2001 From: Daniel Ventura Date: Mon, 5 Dec 2022 09:32:27 +0000 Subject: [PATCH 13/17] #299 updating logic so we don't need to retrieve all the progress status from database but just the latest result id, and use the events to get it basing itself on the retrieved resultId --- .../classes/mcdo_RunCopadoFunctionFromLWC.cls | 71 +++---------------- .../mcdo_RetrieveTable/mcdo_RetrieveTable.js | 22 +++--- 2 files changed, 19 insertions(+), 74 deletions(-) diff --git a/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls b/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls index 7422338..8a68d66 100644 --- a/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls +++ b/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls @@ -130,10 +130,15 @@ public with sharing class mcdo_RunCopadoFunctionFromLWC { } } + /** + * This function calls returns the last resultId from the received jobExecution + * @param jobExecutionId JobExecution Id from the jobExecution that we want to find the latest result id + * @return Returs the ID of the latest result created on the respective jobExecution + */ @AuraEnabled - public static List getResultIds(String jobExecutionId) { + public static Id getResultId(String jobExecutionId) { // get the newest result associated with this job execution - copado__Result__c[] results = [ + copado__Result__c result = [ SELECT Id FROM copado__Result__c WHERE copado__JobStep__r.copado__JobExecution__c = :jobExecutionId @@ -141,64 +146,6 @@ public with sharing class mcdo_RunCopadoFunctionFromLWC { ORDER BY CreatedDate DESC LIMIT 1 ]; - List ids = new List(); - - for (copado__Result__c item : results) { - ids.add(item.Id); - } - - return ids; - } - - @AuraEnabled - public static jobProgressWrapper getJobProgress(String jobExecutionId) { - // get the newest result associated with this job execution - copado__Result__c[] results = [ - SELECT - Id, - LastModifiedDate, - copado__JobStep__r.copado__JobExecution__r.copado__Status__c, - copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c, - copado__Progress_Status__c, - copado__JobStep__r.Name - FROM copado__Result__c - WHERE copado__JobStep__r.copado__JobExecution__c = :jobExecutionId - WITH SECURITY_ENFORCED - ORDER BY CreatedDate DESC - LIMIT 1 - ]; - JobProgressWrapper response = new JobProgressWrapper(); - if (!results.isEmpty()) { - response.resultId = results[0].Id; - response.status = results[0] - .copado__JobStep__r.copado__JobExecution__r.copado__Status__c; - response.lastModified = results[0].LastModifiedDate; - response.stepName = results[0].copado__JobStep__r.Name; - // calculate progress with the Job Execution error message, or the last progress status - response.progress = String.isNotEmpty( - results[0].copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c - ) - ? results[0].copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c - : results[0].copado__Progress_Status__c; - response.isCompleted = (response.status == 'Successful' || - response.status == 'Error' || - response.status == 'Canceled'); - } - return response; - } - - public class JobProgressWrapper { - @AuraEnabled - public String resultId = ''; - @AuraEnabled - public String status = ''; - @AuraEnabled - public String progress = ''; - @AuraEnabled - public Datetime lastModified = null; - @AuraEnabled - public String stepName = ''; - @AuraEnabled - public Boolean isCompleted = false; + return result.Id; } -} +} \ No newline at end of file diff --git a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js index 4a06513..33ebf9e 100644 --- a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js +++ b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js @@ -26,7 +26,7 @@ import { // Apex functions to retrieve Recorddata from LWC import ExecuteRetrieveFromCopado from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.executeRetrieve"; import getMetadataFromEnvironment from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getMetadataFromEnvironment"; -import getResultIds from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getResultIds"; +import getResultId from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getResultId"; // "Commit Changes" Page Tab related import COMMIT_PAGE_COMMUNICATION_CHANNEL from "@salesforce/messageChannel/copado__CommitPageCommunication__c"; @@ -157,6 +157,7 @@ export default class mcdo_RetrieveTable extends LightningElement { showTable = false; refreshButtonDisabled = true; progressStatus = "Loading data"; + progressResultId = undefined; // Subscription related variables getProgressSubscription = {}; @@ -312,7 +313,6 @@ export default class mcdo_RetrieveTable extends LightningElement { const jobExecutionId = await ExecuteRetrieveFromCopado({ userStoryId: this.userStoryId }); - //! has to be last result created for that job step if there are multiple this.subscribeToCompletionEvent(jobExecutionId); } catch (error) { this.loadingState(false); @@ -334,21 +334,19 @@ export default class mcdo_RetrieveTable extends LightningElement { * @returns {Promise} resolves when the job is done */ async subscribeToCompletionEvent(jobExecutionId) { - // get result ID from Job step related to job execution - this.currentResultIds = await getResultIds(jobExecutionId); - console.log("currentResultIds", this.currentResultIds); + // getting the last result id with this jobExecution + try { + this.progressResultId = await getResultId({ jobExecutionId: jobExecutionId }); + } catch (error) { + console.error(`ERROR STATUS: ${error.status} ${error.statusText}`); + } const progressMessageCallback = async (response) => { if (response.data.payload.copado__Progress_Status__c === "Refresh done") { this.unsubscribeThisSubscription(this.getProgressSubscription); this.progressStatus = "Completed!"; - } else { - if ( - this.currentResultIds.includes(response?.data?.payload?.copado__ResultId__c) && - response?.data?.payload?.copado__Progress_Status__c - ) { - this.progressStatus = response?.data?.payload?.copado__Progress_Status__c; - } + } else if (response.data.payload.copado__ResultId__c === this.progressResultId) { + this.progressStatus = response.data.payload.copado__Progress_Status__c; } }; From bf46ae6aa8861523c7cdcf7febaa0dd36979db3f Mon Sep 17 00:00:00 2001 From: Daniel Ventura Date: Mon, 5 Dec 2022 10:23:17 +0000 Subject: [PATCH 14/17] #299 changing logic so it gets all the result ids from the jobExceptionId --- .../classes/mcdo_RunCopadoFunctionFromLWC.cls | 15 ++++++++++----- .../lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js | 15 +++++++++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls b/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls index 8a68d66..f6a82b1 100644 --- a/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls +++ b/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls @@ -133,19 +133,24 @@ public with sharing class mcdo_RunCopadoFunctionFromLWC { /** * This function calls returns the last resultId from the received jobExecution * @param jobExecutionId JobExecution Id from the jobExecution that we want to find the latest result id - * @return Returs the ID of the latest result created on the respective jobExecution + * @return Returs the list of IDs form the results created on the respective jobExecution */ @AuraEnabled - public static Id getResultId(String jobExecutionId) { + public static List getResultIds(String jobExecutionId) { // get the newest result associated with this job execution - copado__Result__c result = [ + copado__Result__c[] results = [ SELECT Id FROM copado__Result__c WHERE copado__JobStep__r.copado__JobExecution__c = :jobExecutionId WITH SECURITY_ENFORCED ORDER BY CreatedDate DESC - LIMIT 1 ]; - return result.Id; + List ids = new List(); + + for (copado__Result__c item : results) { + ids.add(item.Id); + } + + return ids; } } \ No newline at end of file diff --git a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js index 33ebf9e..4dbbe6b 100644 --- a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js +++ b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js @@ -26,7 +26,7 @@ import { // Apex functions to retrieve Recorddata from LWC import ExecuteRetrieveFromCopado from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.executeRetrieve"; import getMetadataFromEnvironment from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getMetadataFromEnvironment"; -import getResultId from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getResultId"; +import getResultIds from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getResultIds"; // "Commit Changes" Page Tab related import COMMIT_PAGE_COMMUNICATION_CHANNEL from "@salesforce/messageChannel/copado__CommitPageCommunication__c"; @@ -157,7 +157,7 @@ export default class mcdo_RetrieveTable extends LightningElement { showTable = false; refreshButtonDisabled = true; progressStatus = "Loading data"; - progressResultId = undefined; + currentResultIds = undefined; // Subscription related variables getProgressSubscription = {}; @@ -334,9 +334,9 @@ export default class mcdo_RetrieveTable extends LightningElement { * @returns {Promise} resolves when the job is done */ async subscribeToCompletionEvent(jobExecutionId) { - // getting the last result id with this jobExecution + // get result ID from Job step related to job execution try { - this.progressResultId = await getResultId({ jobExecutionId: jobExecutionId }); + this.currentResultIds = await getResultIds({ jobExecutionId: jobExecutionId }); } catch (error) { console.error(`ERROR STATUS: ${error.status} ${error.statusText}`); } @@ -345,8 +345,11 @@ export default class mcdo_RetrieveTable extends LightningElement { if (response.data.payload.copado__Progress_Status__c === "Refresh done") { this.unsubscribeThisSubscription(this.getProgressSubscription); this.progressStatus = "Completed!"; - } else if (response.data.payload.copado__ResultId__c === this.progressResultId) { - this.progressStatus = response.data.payload.copado__Progress_Status__c; + } else if ( + this.currentResultIds.includes(response?.data?.payload?.copado__ResultId__c) && + response?.data?.payload?.copado__Progress_Status__c + ) { + this.progressStatus = response?.data?.payload?.copado__Progress_Status__c; } }; From 290bce3fa5fb2faa995a1af09ab5d864989fff5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Berkefeld?= Date: Mon, 5 Dec 2022 14:46:04 +0100 Subject: [PATCH 15/17] #299: reduce result id response by filtering "closed" ones --- .../main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls b/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls index f6a82b1..11915e5 100644 --- a/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls +++ b/force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls @@ -141,7 +141,9 @@ public with sharing class mcdo_RunCopadoFunctionFromLWC { copado__Result__c[] results = [ SELECT Id FROM copado__Result__c - WHERE copado__JobStep__r.copado__JobExecution__c = :jobExecutionId + WHERE + copado__JobStep__r.copado__JobExecution__c = :jobExecutionId + AND copado__Status__c NOT IN ('Failed', 'Cancelled', 'Success') WITH SECURITY_ENFORCED ORDER BY CreatedDate DESC ]; @@ -153,4 +155,4 @@ public with sharing class mcdo_RunCopadoFunctionFromLWC { return ids; } -} \ No newline at end of file +} From 163e5814fa8aa0aa071953bf4784598bfe53bcc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Berkefeld?= Date: Mon, 5 Dec 2022 15:28:57 +0100 Subject: [PATCH 16/17] #299: moved final Log.result behind uploading relevant JSON --- copado-function/app/Retrieve.fn.js | 2 +- copado-function/dist/Retrieve.fn.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/copado-function/app/Retrieve.fn.js b/copado-function/app/Retrieve.fn.js index 5af69f3..4968cbb 100644 --- a/copado-function/app/Retrieve.fn.js +++ b/copado-function/app/Retrieve.fn.js @@ -155,7 +155,6 @@ async function run() { Log.error('Saving metadata JSON failed:' + ex.message); throw ex; } - Log.result(`Found ${metadataJson.length} items on server`, 'Refresh done'); try { Log.info(''); Log.info('Attach JSON'); @@ -171,6 +170,7 @@ async function run() { Log.error('Attaching JSON file failed:' + ex.message); throw ex; } + Log.result(`Found ${metadataJson.length} items on server`, 'Refresh done'); Log.info(''); Log.info('==================='); Log.info(''); diff --git a/copado-function/dist/Retrieve.fn.js b/copado-function/dist/Retrieve.fn.js index 03b69ac..6f5adbe 100644 --- a/copado-function/dist/Retrieve.fn.js +++ b/copado-function/dist/Retrieve.fn.js @@ -1,7 +1,7 @@ #!/usr/bin/env node /* - * mcdev-copado v1.2.0 (built 2022-11-21T11:25:57.041Z) + * mcdev-copado v1.2.0 (built 2022-12-05T14:16:30.690Z) * Function: Retrieve.fn.js * Dependenies: mcdev@>=4.1.12, Copado Deployer@20.1 * Homepage: https://github.com/Accenture/sfmc-devtools-copado#readme @@ -472,7 +472,6 @@ async function run() { Log.error("Saving metadata JSON failed:" + ex.message); throw ex; } - Log.result(`Found ${metadataJson.length} items on server`, "Refresh done"); try { Log.info(""); Log.info("Attach JSON"); @@ -488,6 +487,7 @@ async function run() { Log.error("Attaching JSON file failed:" + ex.message); throw ex; } + Log.result(`Found ${metadataJson.length} items on server`, "Refresh done"); Log.info(""); Log.info("==================="); Log.info(""); From 47367b77611b71e73bc3ddde447cceedd09ca81f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Berkefeld?= Date: Mon, 5 Dec 2022 15:30:38 +0100 Subject: [PATCH 17/17] #299: remove custom "completed" listener --- .../default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js index 4dbbe6b..5da84ae 100644 --- a/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js +++ b/force-app/main/default/lwc/mcdo_RetrieveTable/mcdo_RetrieveTable.js @@ -342,13 +342,11 @@ export default class mcdo_RetrieveTable extends LightningElement { } const progressMessageCallback = async (response) => { - if (response.data.payload.copado__Progress_Status__c === "Refresh done") { - this.unsubscribeThisSubscription(this.getProgressSubscription); - this.progressStatus = "Completed!"; - } else if ( + if ( this.currentResultIds.includes(response?.data?.payload?.copado__ResultId__c) && response?.data?.payload?.copado__Progress_Status__c ) { + // show progress update to user this.progressStatus = response?.data?.payload?.copado__Progress_Status__c; } }; @@ -408,6 +406,7 @@ export default class mcdo_RetrieveTable extends LightningElement { * @returns {Promise} resolves when the job is done */ async updateMetadataGrid(response, jobExecutionId) { + this.unsubscribeThisSubscription(this.getProgressSubscription); this.unsubscribeThisSubscription(this.reloadTableSubscription); const jobExecution = JSON.parse(response.data.payload.copado__Payload__c); if (jobExecution.copado__Status__c === "Successful") {