function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shane Jay HayesShane Jay Hayes 

Trying to insert a field update while saving a photo

Probably not the best subject but I was having trouble trying to figure out what to call this.  I have a working Apex class that I use to upload photos.  I know want to add a line that will also update a custom field on the custom object when the photo is uploaded.  Below is the section of the current code.
 
public Boolean saveCurrentPicture(){
      Savepoint sp = Database.setSavepoint();
      try{
        delete [ Select Id From Attachment where ParentId =: this.parentId and name = 'During Mitigation' limit 1 ];
      this.newAttach.parentId = this.parentId;
          this.newAttach.name = 'During Mitigation';
          this.newAttach.Description = 'DuringMitPhoto';
          insert this.newAttach;
          return true;
      } 
      catch( Exception e ){
        this.error += ERROR_NO_SAVE+'<br/>';
        Database.rollback( sp );
        return false;
      }
    }

I would like to add this.
 
scope.DuringMitigationURL__c = '/servlet/servlet.FileDownload?file='
                                          + attachment.id;

I have a similar class that I am trying to combine into one function.  It currently uses the code I want to put into the original code top.  You can see that here:
 
public class DuringMitigationControllerEXT {
public blob picture { get; set; }
    public String errorMessage { get; set; }
    private final Room_Scope_Sheet__c scope;
    private final String  ERROR_IMG_TYPE    = 'The image must be in .jpg, .gif or .png format';   
    private Set<String> imagesTypes         = new Set<String> {'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/gif'};
    private Set<String> notAllowedTypes     = new Set<String> {'application/octet-stream'};
    private ApexPages.StandardController stdController;

    public DuringMitigationControllerEXT(ApexPages.StandardController stdController) {
        this.scope = (Room_Scope_Sheet__c)stdController.getRecord();
        this.stdController = stdController;
    }

    public PageReference save() {
        errorMessage = '';
        try {
            upsert scope;
            if (picture != null) {
                Attachment attachment = new Attachment();
                attachment.body = picture;
                attachment.name = 'duringmitigation_' + scope.id;
                attachment.Description = 'During Mitigation Photo';
                attachment.parentid = scope.id;
                insert attachment;
                scope.DuringMitigationURL__c = '/servlet/servlet.FileDownload?file='
                                          + attachment.id;
                update scope;
            }
            return new ApexPages.StandardController(scope).view();
        } catch(System.Exception ex) {
            errorMessage = ex.getMessage();
            return null;
        }
    }
}

So someone tell a noob what he is doing wrong.  I believe I am trying to fit a square hole in a round peg.