Skip to content

Instantly share code, notes, and snippets.

@johndstein
Created September 23, 2016 13:47
Show Gist options
  • Select an option

  • Save johndstein/90e9866a93d2331edbe7f81cfc290898 to your computer and use it in GitHub Desktop.

Select an option

Save johndstein/90e9866a93d2331edbe7f81cfc290898 to your computer and use it in GitHub Desktop.
salesforce Batchable
global with sharing class CertifiedFacilityWordPressSync implements Schedulable, Database.Batchable<sObject>, Database.AllowsCallouts {
class WordpressSyncJobException extends Exception {}
global static String scheduleIt(String jobName, String CRON_EXP) {
CRON_EXP = CRON_EXP != NULL ? CRON_EXP : '0 0 0/1 1/1 ? ';
jobName = jobName == null ? 'Certified Facility WordPress Sync' : jobName;
CertifiedFacilityWordPressSync cfwp = new CertifiedFacilityWordPressSync();
return System.schedule(jobName, CRON_EXP, cfwp);
}
global static void executeBatchOnDemand() {
CertifiedFacilityWordPressSync batch = new CertifiedFacilityWordPressSync();
Id batchprocessId = Database.executeBatch(batch, 50);
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator([SELECT Id, name, billingStreet, billingCity, billingState, billingPostalCode, billingCountry, billingLatitude, billingLongitude, phone, send_referrals__c, therapy_applications_certified__c, visible_on_website__c FROM Account WHERE (certified_date__c != null OR certified__c = true) AND npe01__SYSTEM_AccountType__c != 'One-to-One Individual']);
}
global void execute(SchedulableContext sc) {
Id batchprocessId = Database.executeBatch(this, 50);
}
global void execute(Database.BatchableContext BC, list<Account> accountScope) {
//WordpressAPI.loginData = new WordpressModel.LoginResponse();
//WordpressAPI.loginData.cookie_name = 'foo';
//WordpressAPI.loginData.cookie = 'foo1';
WordpressModel.LoginResponse loginData = WordpressAPI.login();
Integer i = 0;
String postBody = '';
for (Account acc : accountScope) {
if (postBody != '') {
postBody += '&';
}
postBody += 'organizations[' + i + '][name]=';
postBody += acc.name != null ? EncodingUtil.urlEncode(acc.name, 'UTF-8') : acc.name;
postBody += '&organizations[' + i + '][address]=';
postBody += acc.billingStreet != null ? EncodingUtil.urlEncode(acc.billingStreet, 'UTF-8') : acc.billingStreet;
postBody += '&organizations[' + i + '][city]=';
postBody += acc.billingCity != null ? EncodingUtil.urlEncode(acc.billingCity, 'UTF-8') : acc.billingCity;
postBody += '&organizations[' + i + '][state]=';
postBody += acc.billingState != null ? EncodingUtil.urlEncode(convertToStateAbbreviationFormat(acc.billingState), 'UTF-8') : acc.billingState;
postBody += '&organizations[' + i + '][zip]=';
postBody += acc.billingPostalCode != null ? EncodingUtil.urlEncode(acc.billingPostalCode, 'UTF-8') : acc.billingPostalCode;
postBody += '&organizations[' + i + '][country]=';
postBody += acc.billingCountry != null ? EncodingUtil.urlEncode(acc.billingCountry, 'UTF-8') : acc.billingCountry;
postBody += '&organizations[' + i + '][lat]=';
postBody += acc.billingLatitude != null ? EncodingUtil.urlEncode(String.valueOf(acc.billingLatitude), 'UTF-8') : String.valueOf(acc.billingLatitude);
postBody += '&organizations[' + i + '][lng]=';
postBody += acc.billingLongitude != null ? EncodingUtil.urlEncode(String.valueOf(acc.billingLongitude), 'UTF-8') : String.valueOf(acc.billingLongitude);
postBody += '&organizations[' + i + '][phone]=';
postBody += acc.phone != null ? EncodingUtil.urlEncode(acc.phone, 'UTF-8') : acc.phone;
postBody += '&organizations[' + i + '][sfId]=';
postBody += acc.id != null ? EncodingUtil.urlEncode(acc.id, 'UTF-8') : acc.id;
postBody += '&organizations[' + i + '][volunteer]=';
postBody += acc.send_referrals__c ? '1' : '0';
postBody += '&organizations[' + i + '][therapy]=';
postBody += acc.therapy_applications_certified__c ? '1' : '0';
postBody += '&organizations[' + i + '][visible]=';
postBody += acc.visible_on_website__c ? '1' : '0';
i += 1;
}
WordpressModel.OrganizationsResponse organizationsRes = WordpressAPI.upsertOrganizations(postBody);
if (organizationsRes.status != 'ok') {
throw new WordpressSyncJobException('Error trying to upsert organizations in Wordpress: ' + organizationsRes);
}
}
private static String convertToStateAbbreviationFormat(String stateName) {
if(stateName != null && stateName.length() > 2) {
String stateJSONMap = '{"Alabama":"AL","Alaska":"AK","American Samoa":"AS","Arizona":"AZ","Arkansas":"AR","California":"CA","Colorado":"CO","Connecticut":"CT","Delaware":"DE","District Of Columbia":"DC","Federated States Of Micronesia":"FM","Florida":"FL","Georgia":"GA","Guam":"GU","Hawaii":"HI","Idaho":"ID","Illinois":"IL","Indiana":"IN","Iowa":"IA","Kansas":"KS","Kentucky":"KY","Louisiana":"LA","Maine":"ME","Marshall Islands":"MH","Maryland":"MD","Massachusetts":"MA","Michigan":"MI","Minnesota":"MN","Mississippi":"MS","Missouri":"MO","Montana":"MT","Nebraska":"NE","Nevada":"NV","New Hampshire":"NH","New Jersey":"NJ","New Mexico":"NM","New York":"NY","North Carolina":"NC","North Dakota":"ND","Northern Mariana Islands":"MP","Ohio":"OH","Oklahoma":"OK","Oregon":"OR","Palau":"PW","Pennsylvania":"PA","Puerto Rico":"PR","Rhode Island":"RI","South Carolina":"SC","South Dakota":"SD","Tennessee":"TN","Texas":"TX","Utah":"UT","Vermont":"VT","Virgin Islands":"VI","Virginia":"VA","Washington":"WA","West Virginia":"WV","Wisconsin":"WI","Wyoming":"WY"}';
Map<String, String> stateMap = (Map<String, String>)JSON.deserialize(stateJSONMap, Map<String, String>.class);
return stateMap.get(stateName) != null ? stateMap.get(stateName) : stateName;
} else {
return stateName;
}
}
global void finish(Database.BatchableContext BC) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment