1

I am trying to write the results from a database.saveresult to a note. I am trying to figure out how to take the list of values and pass them to the note body field. I am getting a list to string error which I understand but I don't know how to convert the list into a string of values.

Any help? Problem code is right at end of code.

//Session
        for (Database.SaveResult sr : srSession) {
            if (sr.isSuccess()) {
                // Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully transfered session. Session ID: ' + sr.getId());
                showRecords = FALSE;
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Successfully transferred Sessions. Session ID: ' + sr.getId()));

            }
            else {
                // Operation failed, so get all errors                
                for(Database.Error err : sr.getErrors()) {
                    showRecords = FALSE;
                    sessionSuccess = FALSE;
                    System.debug('The following error has occurred.');                    
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Session fields that affected this error: ' + err.getFields());
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, err.getStatusCode() + ': ' + err.getMessage()));

                }
            }
        }

        if(taskSuccess == TRUE){
            //create note
            note n = new note();
            n.ParentId = empSession.Employee__c;
            n.Title = 'Task Transfer Record';
            n.Body = srSession;
            Insert n;
        }
1

The simplest way if you're just doing this for debugging would be to dump it into JSON (serialize the srSession object).

    if(taskSuccess){
        //create note
        note n = new note();
        n.ParentId = empSession.Employee__c;
        n.Title = 'Task Transfer Record';
        n.Body = JSON.serializePretty(srSession);
        Insert n;
    }

If you're trying to summarize the save results, you can build a string during your iterations and use that as the body.

//Session
    String outputResult = '';
    for (Database.SaveResult sr : srSession) {
        if (sr.isSuccess()) {
            // Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully transfered session. Session ID: ' + sr.getId());
            showRecords = FALSE;
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Successfully transferred Sessions. Session ID: ' + sr.getId()));
            outputResult += '\nSuccessfully transferred Sessions.  Session ID: ' + sr.getId();

        }
        else {
            // Operation failed, so get all errors                
            for(Database.Error err : sr.getErrors()) {
                showRecords = FALSE;
                sessionSuccess = FALSE;
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Session fields that affected this error: ' + err.getFields());
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, err.getStatusCode() + ': ' + err.getMessage()));

            }
        }
    }

    if(taskSuccess){
        //create note
        note n = new note();
        n.ParentId = empSession.Employee__c;
        n.Title = 'Task Transfer Record';
        n.Body = outputResult;
        Insert n;
    }
| improve this answer | |
  • 1
    Also note the if statement, the == TRUE is repetitive. Anything inside the if is already boolean evaluated. – drakored Jun 20 '16 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.