Before showing my code that needs debugging, I want to know if it's possible to import data exposed by a Flask API into a custom Salesforce object with an APEX class.
I know it is possible to create a web request in APEX class to execute a Flask API. Now I want to import the data exposed by the same Flask API to a custom Salesforce object with the same class that made the request. The data from the Flask API is in a csv file.
I am expecting to find the records or data I presumably imported from the CSV exposed by my Flask API in my custom object in Salesforce
I tried this code to import the data:
public class CSVImport {
public static void importCSVFromFlask() {
// Here I make web request to Flask API endpoint
HttpRequest request = new HttpRequest();
request.setEndpoint('My exposed Flask endpoint IP'); // Flask endpoint
request.setMethod('GET');
HttpResponse response = new Http().send(request); // THIS PART OF CODE EXECUTES SUCCESSFULLY
if (response.getStatusCode() == 200) {
// THIS IS WHERE I NEED HELP, PERHAPS I SHOULD CHANGE CODE ENTIRELY TO:
List<String> csvLines = response.getBody().split('\n');
// I don't want to import 1st row of CSV file that contains headers for the fields
if (!csvLines.isEmpty() && csvLines.size() > 1) {
csvLines.remove(0);
}
// Creating records in custom object
List<Salesforce_custom_object__c> recordsToInsert = new List<Salesforce_custom_object__c>();
for (String csvLine : csvLines) {
List<String> csvFields = csvLine.split(',');
if (csvFields.size() >= 3) { // My CSV file only has 3 field
Salesforce_custom_object__c obj = new Salesforce_custom_object__c();
// I just want to import data from the 3rd column and 2nd row of CSV file
obj.PriceField__c = Decimal.valueOf(csvFields[2]);
recordsToInsert.add(obj);
}
}
// Inserting records into Salesforce
if (!recordsToInsert.isEmpty()) {
insert recordsToInsert;
}
}
}
}