1

I've created two different classes, each writing to a different homebrew table and I'm not having any problem inserting in one of them, but I can't seem to get the other class to insert records into the other table, neither insert nor upsert seems to work, but I get no errors!

The class I'm testing is a global class that implements Schedulable, in a full clone Sandbox and for this test I'm just running it through the Developer Console as a test function. In essence here's a snippet of code that's executing, ... but isn't working!


    for (Item item : items.values()) {                      // all remaining records are new entries
        AccountChangeReport__c acrRec = new AccountChangeReport__c(
            sourceId__c = item.sourceId,
            source__c   = item.source,
            name__c     = item.name,
            status__c   = 'New',
            lastSunProcessed__c = dSun);
        acrRecs.add(acrRec);
    }

    try {
        System.debug(LoggingLevel.DEBUG, 'recs to upsert ' + acrRecs.size());
        System.debug(LoggingLevel.DEBUG, acrRecs.get(0));
        upsert acrRecs sourceId__c;                         // this isn't working, but no errors occur
        //insert acrRecs                                    // tried this too, still doesn't work

        System.debug(LoggingLevel.DEBUG, 'recs to delete ' + toDelete.size());
        delete toDelete;
    }
    Catch(DmlException e) {
        System.debug(LoggingLevel.DEBUG, e.getMessage());
    }

and the relevant part of the logs are showing this:

11:07:44.0 (28215843)|USER_DEBUG|[107]|DEBUG|recs to upsert 20
11:07:44.0 (28222867)|STATEMENT_EXECUTE|[109] 11:07:44.0
(28246596)|HEAP_ALLOCATE|[109]|Bytes:24 11:07:44.0
(28288041)|USER_DEBUG|[109]|DEBUG|AccountChangeReport__c:{sourceId__c=0010P00001oFPEFQA4,
source__c=Account, name__c=Cowen, status__c=New,
lastSunProcessed__c=2017-11-26 00:00:00} 11:07:44.0
(28296140)|STATEMENT_EXECUTE|[110] 11:07:44.0
(28299398)|HEAP_ALLOCATE|[110]|Bytes:22 11:07:44.0
(28302854)|HEAP_ALLOCATE|[110]|Bytes:11 11:07:44.0
(28359170)|DML_BEGIN|[110]|Op:Upsert|Type:AccountChangeReport__c|Rows:20
11:07:44.0 (28382074)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:84 11:07:44.0
(96690508)|DML_END|[110] 11:07:44.0
(96764187)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:84 11:07:44.0
(96787025)|STATEMENT_EXECUTE|[112] 11:07:44.0
(96802944)|HEAP_ALLOCATE|[112]|Bytes:15 11:07:44.0
(96879520)|HEAP_ALLOCATE|[112]|Bytes:1 11:07:44.0
(96893734)|HEAP_ALLOCATE|[112]|Bytes:16 11:07:44.0
(96919093)|USER_DEBUG|[112]|DEBUG|recs to delete 0 11:07:44.0
(96925926)|STATEMENT_EXECUTE|[113] 11:07:44.0
(96994758)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 11:07:44.0
(97009424)|METHOD_EXIT|[11]|01p0x0000004riu|AccountChangeReporter.execute(System.SchedulableContext)
11:07:44.97 (97024002)|CUMULATIVE_LIMIT_USAGE 11:07:44.97
(97024002)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 3 out of 100
Number of query rows: 20 out of 50000 Number of SOSL queries: 0 out of 20
Number of DML statements: 1 out of 150
Number of DML rows: 20 out of 10000
Maximum CPU time: 0 out of 10000
Maximum heap size: 0 out of 6000000
Number of callouts: 0 out of 100 Number of Email Invocations: 0 out of 10
Number of future calls: 0 out of 50
Number of queueable jobs added to the queue: 0 out of 50
Number of Mobile Apex push calls: 0 out of 10

11:07:44.97 (97024002)|CUMULATIVE_LIMIT_USAGE_END

11:07:44.0 (97056499)|CODE_UNIT_FINISHED|ClassTester.test_AccountChangeReporter
11:07:44.0 (97964501)|EXECUTION_FINISHED


It seems to indicate success, but the table is still empty, even if I remove the upsert statement and change it to an insert instead.

the fields being inserted are defined as...

  • lastSunProcessed__c is a Date/Time field
  • name__c is a text(64) field
  • source__c is a Picklist of 'Contact' or 'Account' (which actually did create an error if I put a wrong value in)
  • sourceId__c is a text(18) field -- which should be long enough to hold any record ID
  • status__c is also a Picklist of 'New', 'Changed', or 'Deleted'

Everything in the debug log looks good, no errors, but no records ever get inserted.

All I can think of is there's some configuration value somewhere I forgot to set, somewhere.

10
  • I find that debugs and exceptions tend to behave kinda funny in async execution contexts. For testing purposes, I'd suggest passing a null to the execute method (i.e. AccountChangeReporter.execute(null) rather than System.enqueueJob(new AccountChangeReporter())) if you're not doing that already. It also might help if you remove the try/catch for testing through execute anonymous (and just let the exception halt execution).
    – Derek F
    Nov 27, 2017 at 16:44
  • You could also do a Database.insert() command and then iterate through the Database.SaveResult objects one by one to see what errors are saved. Also check to see if there are any triggers that could be blocking the transaction. More info on Database.insert here: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… Nov 27, 2017 at 16:46
  • Also, for those not aware (I didn't know this myself until just now), upsert acrRecs sourceId__c; is not a typo. Upsert can take 2 "arguments", the second one tells the upsert which field to use in determining whether or not a record needs to be inserted or updated. Relevant documentation
    – Derek F
    Nov 27, 2017 at 16:49
  • 2
    You should never use an empty catch bock, and debug only blocks are empty as they don't perform any follow up action. You're swallowing the exception here. I recommend you remove the try/catch entirely.
    – Adrian Larson
    Nov 27, 2017 at 17:07
  • 1
    Yeah, the formatting options for comments are quite limited. Things like that are better off as an edit to your original question.
    – Derek F
    Nov 28, 2017 at 13:33

0

You must log in to answer this question.

Browse other questions tagged .