• Kayla Borg 14
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 0
    Questions
  • 3
    Replies
We have identified an issue where Cache.Session get() method does NOT return data stored in cache intermittently. We have put together a very tiny sample VisualForce page to demonstrate the problem. This sample page demonstrates that Cache.Session.getPartition(partitionName).get(KEY) returns NULL even if the data exists in cache for the given KEY. The page performs 100 Visualforce.remoting.Manager.invokeAction calls to a controller remote action. If the Apex code is not able to retrieve the data from cache it returns "Not Found" otherwise it returns "Found". Case # created with Salesforce.com 14311948.

See code and output sample below:

VisualForce Page:
<apex:page controller="CacheTestController" showChat="false" showHeader="false">

<div style="margin: 10px; font-size: 14px">
    <pre id="put">In progress ...</pre>
    <pre id="get"></pre>
</div>
 
<script>
    Visualforce.remoting.Manager.invokeAction(
        'CacheTestController.putCache', function(result, event) {
            document.getElementById('put').innerHTML = result;
        }, 
        { escape: false }
    );
 
    var tentative = 1;
 
    var timer = setInterval(function () {
 
        if (tentative == 100) clearInterval(timer);
 
        Visualforce.remoting.Manager.invokeAction(
            'CacheTestController.getCache', tentative, function (result, event) {
                document.getElementById('get').innerHTML += result + '<br/>';
            }, 
            { escape: false }
        );
 
        tentative++;
 
    }, 1000);
</script>
</apex:page>



Controller:
public class CacheTestController {
 
    private static final String PARTITION_NAME = 'FinancialServices';
    private static final String KEY = 'STAGE';
    private static final String VALUE = 'Any value';
      
    @RemoteAction
    public static String putCache() {
 
        Cache.Partition partition = Cache.Session.getPartition(PARTITION_NAME);
 
        partition.put(KEY, VALUE);
 
        return 'ok';
    }
 
    @RemoteAction
    public static String getCache(String tentative) {
 
        String found = 'Found: ' + tentative;
 
        Cache.Partition partition = Cache.Session.getPartition(PARTITION_NAME);
       
        String cacheValue = (String) partition.get(KEY);
       
        if (cacheValue == null) {
            found = 'Not Found: ' + tentative;
        }
       
        return found;
    }
}

Output:
Found: 1
Found: 2
Found: 3
Found: 4
Found: 5
Found: 6
Found: 7
Found: 8
Found: 9
Found: 10
Found: 11
Found: 12
Found: 13
Found: 14
Found: 15
Not Found: 16
Found: 17
Found: 18
Found: 19
Found: 20
Found: 21
Found: 22
Not Found: 23
Found: 24
Found: 25
Found: 26
Found: 27
Found: 28
Not Found: 29
Not Found: 30
Found: 31
Found: 32
Found: 33
Found: 34
Found: 35
Found: 36
Found: 37
Found: 38
Found: 39
Found: 40
Found: 41
Found: 42
Found: 43
Not Found: 44
Found: 45
Not Found: 46
Found: 47
Found: 48
Found: 49
Found: 50
Found: 51
Found: 52
Found: 53
Found: 54
Not Found: 55
Found: 56
Found: 57
Found: 58
Found: 59
Found: 60
Found: 61
Found: 62
Found: 63
Not Found: 64
Found: 65
Found: 66
Found: 67
Found: 68
Found: 69
Found: 70
Found: 71
Found: 72
Not Found: 73
Found: 74
Found: 75
Found: 76
Found: 77
Not Found: 78
Found: 79
Found: 80
Found: 81
Found: 82
Found: 83
Not Found: 84
Found: 85
Found: 86
Found: 87
Found: 88
Found: 89
Found: 90
Found: 91
Found: 92
Found: 93
Found: 94
Found: 95
Not Found: 96
Found: 97
Found: 98
Found: 99
Found: 100

 

Hi,

I cannot create a new Activity History record when using message.setSaveAsActivity(true);

 

This is my code:

 

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {recipient.Email};
        mail.setToAddresses(toAddresses);
        mail.setBccSender(true);
        String[] bccAddresses = new String[] {person.Email};
        mail.setBccAddresses(bccAddresses);
        mail.setSaveAsActivity(true);
        mail.setSubject(subject);
        mail.setPlainTextBody(templateBody);

       Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

 

Am I doing something wrong?My controller is extending the custom controller.

 

Many thanks!

  • December 19, 2011
  • Like
  • 0

I have been going around with Salesforce for a while about an "Undocumented Feature".  They stand behind the viewpoint that thie issue in question is by design and not a bug.  I however feel it is otherwise.  Wanted to get other's views on this.

 

 


Issue is that if you send an email out, it generates an activity log (task).  This is what we want.Salesforce uses batch processing, so whether you insert 1 record or 100 records, the associated trigger should be called only once.Problem is, when sending out 100 emails it generates 100 activity logs, but each is created individually, thus the trigger is called 100 times.

If the Task trigger were to perform any action, such as reading from the database, you just hit your limit due to the email api not working in batch mode when creating activity logs!  If everything is Salesforce is setup to work in batches, why not the SendEmail function, which takes a list of email objects to send in batch?

 

 

Code to reproduce this is as follows (look at log file to see Task Trigger is being called for every email sent). 

 

 

trigger SalesforceEmailIssue_Task on Task (before insert) { System.debug('********** SalesforceEmailIssue_Task *****************'); } public class SalesforceEmailIssue { public static void GenerateEmails(List<Contact> listContacts){ System.debug('********** SalesforceEmailIssue *****************'); System.debug('# of contacts: ' + listContacts.size()); List<Messaging.SingleEmailMessage> listMessages = new List<Messaging.SingleEmailMessage>(); // Build list of messages for(Contact c : listContacts){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setSaveAsActivity (true); mail.setHtmlBody('Test'); mail.setSubject('Test'); mail.setTargetObjectId (c.Id); listMessages.add(mail); } // Send emails try{ Messaging.sendEmail(listMessages); } catch(Exception ex){ System.debug('Exception occured: ' + ex); System.Assert(false, ex); } } //######################################## // TEST //######################################## public static testMethod void test(){ integer numOfEmails = 10; string emailAddress = 'claytond@axiumae.com'; try{ System.debug('********* CREATE ACCOUNTS ******************'); List<Account> listAccounts = new List<Account>(); for(Integer i = 0; i < numOfEmails; i++) listAccounts.add(new Account(name='Test' + i, BillingState = 'OR', Type='Prospect')); insert listAccounts; System.debug('********* CREATE CONTACTS FOR EACH ACCOUNT ******************'); List<Contact> listContacts = new List<Contact>(); for(Account a : listAccounts){ //Contact c = AxTestRecords.createContact(a.Id, true); listContacts.add(new Contact( FirstName = 'John' , LastName = 'Doe' , AccountId = a.Id , MailingState = 'OR' , Contact_Role__c = 'Billing Contact' , Email = emailAddress) ); } insert listContacts; GenerateEmails(listContacts); } catch(Exception ex){ System.debug('Exception occured: ' + ex); System.assert(false, ex); } } }

 

We have identified an issue where Cache.Session get() method does NOT return data stored in cache intermittently. We have put together a very tiny sample VisualForce page to demonstrate the problem. This sample page demonstrates that Cache.Session.getPartition(partitionName).get(KEY) returns NULL even if the data exists in cache for the given KEY. The page performs 100 Visualforce.remoting.Manager.invokeAction calls to a controller remote action. If the Apex code is not able to retrieve the data from cache it returns "Not Found" otherwise it returns "Found". Case # created with Salesforce.com 14311948.

See code and output sample below:

VisualForce Page:
<apex:page controller="CacheTestController" showChat="false" showHeader="false">

<div style="margin: 10px; font-size: 14px">
    <pre id="put">In progress ...</pre>
    <pre id="get"></pre>
</div>
 
<script>
    Visualforce.remoting.Manager.invokeAction(
        'CacheTestController.putCache', function(result, event) {
            document.getElementById('put').innerHTML = result;
        }, 
        { escape: false }
    );
 
    var tentative = 1;
 
    var timer = setInterval(function () {
 
        if (tentative == 100) clearInterval(timer);
 
        Visualforce.remoting.Manager.invokeAction(
            'CacheTestController.getCache', tentative, function (result, event) {
                document.getElementById('get').innerHTML += result + '<br/>';
            }, 
            { escape: false }
        );
 
        tentative++;
 
    }, 1000);
</script>
</apex:page>



Controller:
public class CacheTestController {
 
    private static final String PARTITION_NAME = 'FinancialServices';
    private static final String KEY = 'STAGE';
    private static final String VALUE = 'Any value';
      
    @RemoteAction
    public static String putCache() {
 
        Cache.Partition partition = Cache.Session.getPartition(PARTITION_NAME);
 
        partition.put(KEY, VALUE);
 
        return 'ok';
    }
 
    @RemoteAction
    public static String getCache(String tentative) {
 
        String found = 'Found: ' + tentative;
 
        Cache.Partition partition = Cache.Session.getPartition(PARTITION_NAME);
       
        String cacheValue = (String) partition.get(KEY);
       
        if (cacheValue == null) {
            found = 'Not Found: ' + tentative;
        }
       
        return found;
    }
}

Output:
Found: 1
Found: 2
Found: 3
Found: 4
Found: 5
Found: 6
Found: 7
Found: 8
Found: 9
Found: 10
Found: 11
Found: 12
Found: 13
Found: 14
Found: 15
Not Found: 16
Found: 17
Found: 18
Found: 19
Found: 20
Found: 21
Found: 22
Not Found: 23
Found: 24
Found: 25
Found: 26
Found: 27
Found: 28
Not Found: 29
Not Found: 30
Found: 31
Found: 32
Found: 33
Found: 34
Found: 35
Found: 36
Found: 37
Found: 38
Found: 39
Found: 40
Found: 41
Found: 42
Found: 43
Not Found: 44
Found: 45
Not Found: 46
Found: 47
Found: 48
Found: 49
Found: 50
Found: 51
Found: 52
Found: 53
Found: 54
Not Found: 55
Found: 56
Found: 57
Found: 58
Found: 59
Found: 60
Found: 61
Found: 62
Found: 63
Not Found: 64
Found: 65
Found: 66
Found: 67
Found: 68
Found: 69
Found: 70
Found: 71
Found: 72
Not Found: 73
Found: 74
Found: 75
Found: 76
Found: 77
Not Found: 78
Found: 79
Found: 80
Found: 81
Found: 82
Found: 83
Not Found: 84
Found: 85
Found: 86
Found: 87
Found: 88
Found: 89
Found: 90
Found: 91
Found: 92
Found: 93
Found: 94
Found: 95
Not Found: 96
Found: 97
Found: 98
Found: 99
Found: 100