2

Using test.setMock between test.startTest and test.stopTest

DML performs fine everywhere except:

This code fails unless the insert is not done:

sr = [Select Body From StaticResource Where Name = 'SR Name' LIMIT 1];


                    String JSONString = sr.body.toString();

                    Rule__c[] rules = new Rule__c[]{};

                    for(Rule__c r : (Rule__c[])JSON.deserialize(JSONString, Rule__c[].class)){
                        rules.add(New Rule__c(
                            Name = r.Name,
                            Name__c = r.Name__c,
                            Handled__c = r.Handled__c,
                            Text__c = r.Text__c,
                            JSON_String__c = rJSON_String__c
                        ));

                    }

                    //insert rules; 

Now, if I take the JSON string that is the body and use it alone without Querying the static resource it works fine

sr = [Select Body From StaticResource Where Name = 'SR Name' LIMIT 1];                
String JSONString = test_utils.getJSONString();

                Rule__c[] rules = new Rule__c[]{};

                for(Rule__c r : (Rule__c[])JSON.deserialize(JSONString, Rule__c[].class)){
                    rules.add(New Rule__c(
                        Name = r.Name,
                        Name__c = r.Name__c,
                        Handled__c = r.Handled__c,
                        Text__c = r.Text__c,
                        JSON_String__c = rJSON_String__c
                    ));

                }

                insert rules; 

The only difference between the two is:

  1. The body.toString()
  2. Both methods do indeed have records and are not null

If I do not perform that method then I do not get the uncommitted work pending.

Anyone seen this before or have any other ideas on how to get around it....

7
  • Your questing related to this question salesforce.stackexchange.com/questions/36494/… 2) salesforce.stackexchange.com/questions/17293/…
    – D-Horse
    Jul 12, 2014 at 11:13
  • One explanation would be that in the second case vr_rules is a zero length array meaning no insert is done. Can you confirm that vr_rules is not zero length in both cases? (Your pasted code adds to the rules variable but inserts a different variable vr_rules; you should clean that up too to make sure the code is doing what you think it is.)
    – Keith C
    Jul 12, 2014 at 14:17
  • vr_rules is NOT zero length. The document that is a static resource is actually a JSON representation of data. I am using the exact same string in both methods, just different delivery of the string
    – Eric
    Jul 12, 2014 at 15:15
  • @D-Horse: NO this is not the same thing... Not in any way other than the error message. This is not a batch, it works fine unless use body.toString then the error is thrown.
    – Eric
    Jul 12, 2014 at 15:16
  • @KeithC - Using system.Asserts confirms both variants do indeed insert records into the database. And my type in initial post I forgot to replace the vr_rules with rules. I edited the code to remove identifying information vr_rules was changed to rules in the code I pasted after I pasted it
    – Eric
    Jul 12, 2014 at 15:23

0

You must log in to answer this question.

Browse other questions tagged .