Skip to content

Instantly share code, notes, and snippets.

@forcethesales
Last active July 20, 2022 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forcethesales/5f9bfb56d21416fe026592c6141997cb to your computer and use it in GitHub Desktop.
Save forcethesales/5f9bfb56d21416fe026592c6141997cb to your computer and use it in GitHub Desktop.
Add Contacts from Certain States
//This works but times out with too big of a spreadsheet
public class sfdoIdo_addContactsByState {
@InvocableMethod (label = 'Select State for Maps' description = 'select states for map.')
//Query your files in static resources and get their content as String
public static void getStatesList (List<String> states) {
StaticResource ctcSR = [SELECT Id, Body FROM StaticResource WHERE Name = 'sfdoido_Contact' LIMIT 1];
String bodyCtcSR = ctcSR.Body.toString();
system.debug('states=' + states);
Integer stateSize = states.size();
system.debug('stateSize:' + stateSize);
//Parse using your separator
SSSCsvReader bodyCtcSR_csvR = new SSSCsvReader(bodyCtcSR, ',');
/*********************************/
//Loading Contact Sample Data
/*********************************/
//Removing header if you are using a title (optional)
bodyCtcSR_csvR.readLine();
//Load the next line in a variable
String[] bodyCtcSR_line = bodyCtcSR_csvR.readLine();
List<Contact> lSampleCtc = new List<Contact>();
While (bodyCtcSR_line != null) {
for (String currentState :states) {
if (currentState == bodyCtcSR_line[4]) {
Contact sampCtc = new Contact();
sampCtc.FirstName=bodyCtcSR_line[0];
sampCtc.LastName=bodyCtcSR_line[1];
sampCtc.MailingStreet=bodyCtcSR_line[2];
sampCtc.MailingCity=bodyCtcSR_line[3];
sampCtc.MailingState=bodyCtcSR_line[4];
sampCtc.MailingPostalCode=bodyCtcSR_line[5];
sampCtc.External_ID__c=bodyCtcSR_line[6];
lSampleCtc.add(sampCtc);
}
}
bodyCtcSR_line = bodyCtcSR_csvR.readLine();
}
if (lSampleCtc != null) {
Insert lSampleCtc;
}
}
}
//based on https://www.salesforce4ever.com/load-data-stored-as-csv-files-in-static-resources/
public class SSSCsvReader {
private String delim = ',';
// the input data
private String[] buffer;
public SSSCsvReader(String data){
}
public SSSCsvReader(String data, String delim){
this.buffer = data.split('\n');
this.delim = delim;
}
/**
* Read and parse next available line. Return null if end of stream.
*/
public String[] readLine(){
if(buffer.size() == 0)
return null;
String line = this.buffer.remove(0);
String[] parts = new String[] {};
while(line != ''){
Integer next = 0;
if(line.startsWith('"')){
line = line.substring(1); // strip initial
Integer quoteIndex = findQuote(line, 0);
while(quoteIndex == -1){
if(buffer.size() == 0){
// EOT!
quoteIndex = line.length();
} else {
// grab the next line
Integer skip = line.length();
line += '\n' + this.buffer.remove(0);
quoteIndex = findQuote(line, skip);
}
}
// advance to comma
next = quoteIndex + 1;
parts.add(line.substring(0, quoteIndex).replace('""', '"'));
} else {
next = line.indexOf(this.delim, next);
if(next == -1)
next = line.length();
// NB in Substring, "endindex" is the index of the character AFTER the last index to get
parts.add(line.substring(0, next));
}
if(next == line.length() - 1)
// case of a terminating comma.
parts.add('');
line = next < line.length() ? line.substring(next+1) : '';
}
if(parts.size() == 0)
// empty string - we still want to return something...
parts.add('');
return parts;
}
static private Pattern quotePattern = Pattern.compile('(?<!")"(?!")');
/**
* Find next quote in the line
*/
private Integer findQuote(String line, Integer skip){
Matcher m = quotePattern.matcher(line);
m.region(skip, m.regionEnd());
if(!m.find())
return -1;
return m.start();
}
}
@forcethesales
Copy link
Author

  1. There's a static resource of addresses in the database called Contact.
  2. An SE will choose from a screen flow which states addresses they want to enter.
  3. The Flow will call on apex, which will send back a list of ALL possible records.
  4. The flow will limit the records to the ones with the right states and create them.

@forcethesales
Copy link
Author

forcethesales commented Jul 19, 2022

launch in anonymous apex with
List listOfStrings = new List {'PA','NJ'};
sfdoIdo_addContactsByState.getStatesList(listOfStrings);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment