0

Was wondering if anyone has come across a similar issue: I'm trying to run some apex code to insert some records. The code doesn't seem to do anything when it's running in my scratch org but mysteriously works when I run it as anonymous apex code in my dev console. Any ideas why? I'm still pretty new to Salesforce development so it might be something very obvious which I'm missing..

Some pseudo code of what it currently looks like:

List<Play__c> batch = new List<Play__c>();

for (Object p : plays) {
    Map<String, Object> play = (Map<String, Object>) p;
    Map<String, Object> player = (Map<String, Object>) play.get('player');
    Map<String, Object> track = (Map<String, Object>) play.get('track');
    Map<String, Object> user = (Map<String, Object>) play.get('user');
    Map<String, Object> profile = (Map<String, Object>) user.get('profile');
    
    String playerID = (String) player.get('id');
    // skip events which have null playerID
    if (participantID != null) {
        Play__c newPlay = new Play__c();
        newPlay.inviteId__c = (String) trackInvite.get('id');
        newPlay.inviteTitle__c = (String) trackInvite.get('publicTitle');
    }
}
insert batch;

1 Answer 1

0

You are not adding your play object into list before inserting. So your batch list is empty and hence no dml is happening.Still not sure how it is working in anonymous window.

List<Play__c> batch = new List<Play__c>();

for (Object p : plays) {
    Map<String, Object> play = (Map<String, Object>) p;
    Map<String, Object> player = (Map<String, Object>) play.get('player');
    Map<String, Object> track = (Map<String, Object>) play.get('track');
    Map<String, Object> user = (Map<String, Object>) play.get('user');
    Map<String, Object> profile = (Map<String, Object>) user.get('profile');
    
    String playerID = (String) player.get('id');
    // skip events which have null playerID
    if (participantID != null) {
        Play__c newPlay = new Play__c();
        newPlay.inviteId__c = (String) trackInvite.get('id');
        newPlay.inviteTitle__c = (String) trackInvite.get('publicTitle');
        batch.add(newPlay); //Add to list
    }
}
insert batch;
2
  • 1
    I realize my mistake now, I had the missing batch.add(newPlay); line in the dev console but not in my IDE. Thanks!
    – srrc
    Commented Nov 24, 2022 at 5:27
  • Great happy to help. Commented Nov 24, 2022 at 5:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .