I have a Scheduled Apex job, 'scheduleUnsub,' which calls a class 'resubscribeCall,' which runs some code including a method in a Managed Package (ExactTarget) which makes an API call to ExactTarget. When I test 'resubscribeCall' it works as expected. However, whenever the Scheduled Apex job runs, the API call instead of succeeding, returns with
No content to map to Object due to end of input
Does anyone know why it would be behaving differently when run as a scheduled job than when run alone? I had previously tested this in a trigger with the API call as an @future method, but encountered the same problem.
Code below the break
global class scheduleUnsubscribe implements Schedulable{
global void execute(SchedulableContext SC) {
resubscribeCall rc = new resubscribeCall();
}
}
public with sharing class resubscribeCall {
Id contactIds;
static Date startDate = Date.valueOf('2015-04-28');
Set<String> testEmails = new Set<String>{'[email protected]', '[email protected]', '[email protected]'};
public resubscribeCall(){
Contact[] contacts = [Select Id, Email, HasOptedOutOfEmail, (Select Id, Name from Unsubscribed_Lists__r),
(Select Id, Name from et4ae5__IndividualEmailResults__r WHERE
et4ae5__DateUnsubscribed__c != NULL AND et4ae5__DateUnsubscribed__c > :startDate ORDER BY et4ae5__DateUnsubscribed__c DESC LIMIT 1)
from Contact WHERE HasOptedOutOfEmail = true AND Email IN :testEmails];
Map<Id, Contact> cMap = new Map<Id, Contact>();
for(Contact c : contacts){
System.debug(c);
if(c.et4ae5__IndividualEmailResults__r.size() > c.Unsubscribed_Lists__r.size())
cMap.put(c.id, c);
}
Unsubscribed_List__c[] toInsert = new Unsubscribed_List__c[]{};
for(Contact c : contacts)
{
if(c.et4ae5__IndividualEmailResults__r.isEmpty() || !cMap.containsKey(c.id)) continue;
Unsubscribed_List__c unsub = new Unsubscribed_List__c();
unsub.Contact__c = c.id;
String name = c.et4ae5__IndividualEmailResults__r[0].Name.split(',')[0].trim();
System.debug(name);
unsub.Name = name;
unsub.Email__c = c.et4ae5__IndividualEmailResults__r[0].Id;
toInsert.add(unsub);
c.HasOptedOutOfEmail = false;
String output;
try{
if(!Test.isRunningTest())
output = et4ae5.jsButtonMethods.performResub(c.id, 'contact');
}catch(Exception e)
{
output = e.getMessage();
}
System.debug(output);
}
insert toInsert;
update contacts;
}