Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gives an option to load data initially #1621

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,45 +1,56 @@
import { api, LightningElement } from 'lwc';
import getSobjects from '@salesforce/apex/DataPollerController.getSobjects';
import { FlowAttributeChangeEvent } from 'lightning/flowSupport';

export default class DataPoller extends LightningElement {
@api pollingFrequency = 3;
@api queryString;

@api newQueryString;

@api runInitialQueryImmediately = false;

_queryId;
@api
get queryId() {
return this._queryId;
}
set queryId(value) {
this._queryId = value;
this.newQueryString = this.queryString.replace('{Id}', value);
this.newQueryString = this.queryString?.replace('{Id}', value);
}

@api retrievedRecords = [];
@api error;

connectedCallback() {
if (!this.newQueryString && this.queryString) {
this.newQueryString = this.queryString;
}

this.dispatchEvent(new FlowAttributeChangeEvent('retrievedRecords', []));
setInterval(function() {
getSobjects({queryString : this.newQueryString}).then(
result => {
this.retrievedRecords = result;
this.dispatchEvent(new FlowAttributeChangeEvent('retrievedRecords', this.retrievedRecords));
}
).catch(
error => {
console.error('error', error);
if(this.newQueryString) {
this.error = error?.body?.message;
this.dispatchEvent(new FlowAttributeChangeEvent('error', this.error?.body?.message));
}
}
);
}.bind(this), this.pollingFrequency * 1000
)


if (this.runInitialQueryImmediately) {
this.executeQuery();
}

setInterval(this.executeQuery.bind(this), this.pollingFrequency * 1000);
}

}
executeQuery() {
if (!this.newQueryString) {
console.error('Error: queryString or newQueryString is undefined');
return;
}

getSobjects({ queryString: this.newQueryString })
.then(result => {
this.retrievedRecords = result;
this.dispatchEvent(new FlowAttributeChangeEvent('retrievedRecords', this.retrievedRecords));
})
.catch(error => {
console.error('Error executing query:', error);
this.error = error?.body?.message;
this.dispatchEvent(new FlowAttributeChangeEvent('error', this.error));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<property name="queryId" type="String" />
<property name="retrievedRecords" type="{T[]}" role="outputOnly" />
<property name="error" type="String" role="outputOnly" description="API Name of the metadata"/>
<property name="runInitialQueryImmediately" type="Boolean" default="false" label="Run Initial Query Immediately" description="Set to true to execute the first query immediately when the component is initialized." />
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
</LightningComponentBundle>