8
global class Opportunity_EmailService implements Messaging.InboundEmailHandler {
public Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env) {
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

    //try{
        List<Opportunity> oppsToUpsert = new List<Opportunity>();
        List<Account> agenciesToUpsert = new List<Account>();
        List<Policyholder__c> policyholdersToUpsert = new List<Policyholder__c>();

        for(Messaging.InboundEmail.BinaryAttachment att : email.binaryAttachments){
            String attBody = att.Body.toString();
            String firstRow = attBody.split('\r\n', 2)[0];
            system.debug(firstRow);
            List<String> columns = firstRow.split('\\|');

            Integer columnCount = columns.size();
            system.debug(columnCount);

            if(columnCount == 13) {
                boolean isFirstRow = true;
                for(String row : attBody.split('\r\n')) {
                    if(isFirstRow){
                        isFirstRow = false;
                        continue;
                    }
                    List<String> agencyData = row.split('\\|');
                    system.debug(JSON.serializePretty(agencyData));
                    Account acct = new Account();//if the opp already exists this has to be new Opportunity(Id = existingOpp.id);
                    acct.Name = agencyData[1];
                    acct.Phone = agencyData[10];
                    acct.Email__c = agencyData[11];
                    acct.BillingStreet = agencyData[5];
                    acct.BillingCity = agencyData[7];
                    acct.BillingState = agencyData[8];
                    acct.BillingPostalCode = agencyData[9];
                    //whats the rest of the data?
                    //add the rest of the parsing                       
                    agenciesToUpsert.add(acct);
                }
            }
            if (columnCount == 22) {
                boolean isFirstRow = true;
                for(String row : attBody.split('\r\n')) {
                    if(isFirstRow){
                        isFirstRow = false;
                        continue;
                    }

                    List<String> oppData = row.split('\\|');
                    system.debug(JSON.serializePretty(oppData));
                    Opportunity opp = new Opportunity();//if the opp already exists this has to be new Opportunity(Id = existingOpp.id);
                    opp.CloseDate = system.today();//THIS NEEDS TO CHANGE
                    opp.Name = oppData[4];
                    opp.StageName = oppData[11];
                    //whats the rest of the data?
                    //add the rest of the parsing
                    oppsToUpsert.add(opp);
                }
            }
            if (columnCount == 16) {
                boolean isFirstRow = true;
                for(String row : attBody.split('\r\n')) {
                    if(isFirstRow){
                        isFirstRow = false;
                        continue;
                    }

                    List<String> policyholderData = row.split('\\|');
                    system.debug(JSON.serializePretty(policyholderData));
                    Policyholder__c policyholder = new Policyholder__c();//if the opp already exists this has to be new Opportunity(Id = existingOpp.id);
                    policyholder.Name = policyholderData[2];
                    //whats the rest of the data?
                    //add the rest of the parsing
                    policyholdersToUpsert.add(policyholder);
                }
            }
        }
        if (agenciesToUpsert.size() > 0) {
            Database.UpsertResult[] urList = Database.upsert(agenciesToUpsert, false);

            // Iterate through each returned result
            for (Database.UpsertResult sr : urList) {
                if (sr.isSuccess()) {
                    // Operation was successful, so get the ID of the record that was processed
                    System.debug('Successfully inserted account. Account ID: ' + sr.getId());
                }
                else {
                    // Operation failed, so get all errors                
                    for(Database.Error err : sr.getErrors()) {
                        System.debug('The following error has occurred.');                    
                        System.debug(err.getStatusCode() + ': ' + err.getMessage());
                        System.debug('Account fields that affected this error: ' + err.getFields());
                    }
                }
            }
        }
        if(oppsToUpsert.size() > 0) {
            System.debug(oppsToUpsert);
            upsert oppsToUpsert;
        }
        if (policyholdersToUpsert.size() > 0) {
            upsert policyholdersToUpsert;
        }
    return result;
}

}

So when I use the replay debugger it displays the instantiated Lists you see at the top as empty arrays in the variables section of the VS Code debugger. As I step through the code and hit a variety of breakpoints these arrays/lists never update in the variables debugger with the added values so I can't check to see that they are getting filled. Am I missing something? Is there additional functionality in VS Code debugger that I'm not aware of or a way to easily accomplish this without using system.debug. Maybe the watch or checkpoint sections?

Entering one of the lists in watch didn’t produce anything for me...

I could also just be a total noob...

Or is this a limitation of the Replay functionality?

1 Answer 1

7

It can't simulate all variable states. To capture more info, use Checkpoints.

See https://releasenotes.docs.salesforce.com/en-us/winter19/release-notes/rn_vscode_replay_debugger.htm

3
  • This actually helped. Thank you.
    – weytani
    Mar 5, 2019 at 17:05
  • 1
    It does "simulate" variable states, just not collections that are updated. Adding a checkpoint didn't allow me to see changes to collections after instantiation in my test, which is a HUGE limitation and is confirmed by the Apex Replay Debugger's GitHub page: Modifying a collection does not update the collection variable in the VARIABLES section of the Debug view. Aug 29, 2019 at 4:33
  • I've submitted an idea about this, hopefully it gets implemented one day success.salesforce.com/ideaView?id=0873A000000Pf3AQAS Jun 15, 2020 at 14:49

You must log in to answer this question.

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