I'm using a service(force.ts) for performing CRUD operation in my angular2 app. Service's API returns a promise which is converted as Observable in the DataService(internal service written to communicate / modify data to/from ForceService).
If the records are beyond 2,000 or response size is larger, Salesforce RestAPI service automatically send the fewer records and it sends out the nextRecordsUrl
property to fetch next batch of records.
As I get the record from the DataService, I wanted to update the view gradually. I tried but the result is not properly returning properly.
Here's is the intended flow:
1: call getAccounts()
2: return the result, update the view
2.1: if nextRecordsUrl is present, call getNextBatchOfAccounts()
2.1.1: repeat from step 2
Here's the code I tried :
import { Injectable } from '@angular/core';
import { ForceService } from '../force';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class DataService {
getAccounts() {
let promise = this.force.query(
'SELECT Id,Name FROM Account'
let self = this;
return Observable.fromPromise(promise).map(result => {
let data = (<any>result).records;
if(result.nextRecordsUrl) {
return getNextBatchOfAccounts(result.nextRecordsUrl.slice(27));
}
else {
return data.map(function(record) {
return self._convertToCustomType(record);
})
}
});
}
getNextBatchOfAccounts(queryId) {
let promise = this.force.getNextUrlRecords(
queryId
);
return Observable.fromPromise(promise).map(result => {
let data = (<any>result).records;
if(result.nextRecordsUrl) {
return getNextBatchOfAccounts(result.nextRecordsUrl.slice(27));
} else {
return data.map(function(record) {
return self._convertToCustomType(record);
})
}
});
}
_convertToCustomType(item:any) {
return {
'label' : item.Name,
'value' : item.Id
};
}
}