0

My vf page contains account records with its related lists that has notes and attachments i have given new attachment link which is input file and one upload button on vf page to upload attachment, but on clicking upload its not getting saved. please help me in sorting this out friends

my code for vf page is this

<input type="file" class="slds-file-selector__input slds-assistive-text" value="{!a.body}" filename="{!a.name}" id="file-upload-input-01" aria-describedby="file-selector-id"/>
        <label class="slds-file-selector__body" for="file-upload-input-01">
          <span class="slds-file-selector__button slds-button slds-button_neutral">
            <svg class="slds-button__icon slds-button__icon_left" aria-hidden="true">
              <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/assets/icons/utility-sprite/svg/symbols.svg#upload"></use>
            </svg>New Attachment</span>
        </label>            
                        </span>
                     </div>
                  </header>
               </div>
                   <apex:commandButton value="Upload" action="{!saveIt}" styleclass="slds-button slds-button--brand" />
                </div>

and my apex code is this

public with sharing class PersonAccountVFExtension 
{

    public String canImg{get;set;}
    public String actImg{get;set;}
    public List<Case> caseList{get;set;}
    public List<Opportunity> oppList{get;set;}
    public List<Feedback__c> feedbackList{get;set;}
    public List<Note> noteList{get;set;}
    public List<Attachment> attList{get;set;}
    public Attachment a {get;set;}
    public blob attc{get;set;}
    public List<Score__c> scoreList{get;set;}
    Account acc{get;set;}



    public PersonAccountVFExtension(ApexPages.StandardController controller) 
    {

        this.acc=(Account)controller.getRecord();
        this.a= new Attachment();
        caseList = [select Subject,Status,Priority from Case where AccountId=:acc.id LIMIT 3];
        oppList = [select Name,Probability,StageName from Opportunity where AccountID=:acc.id LIMIT 3];
        feedbackList = [select Name,Date__c,Feedback__c from Feedback__c where Qualified_Applicant__r.AccountID=:acc.id LIMIT 3];
        scoreList = [select Name,Comments__c,Evaluater__c from Score__c where Account__c =:acc.id LIMIT 3];
        noteList = [select Body from Note where ParentId =:acc.id];
        attList = [select Name,ParentId from Attachment where ParentId =:acc.id];

        Account newAcc1 = new Account();
        newAcc1 = [select Candidate_Image__c from Account where id=:acc.id];

        if(newAcc1.Candidate_Image__c != null)
        {
            System.debug('Rich field:');
            System.debug(newAcc1.Candidate_Image__c);

            String firstSubString = newAcc1.Candidate_Image__c.substringBetween('<img', 'img>');
            System.debug('First substring: ' + firstSubString);

            if(firstSubString  != null)
            {
                String secondSubString = firstSubString.substringBetween('src="', '"');
                System.debug('Second substring: ' + secondSubString);

                canImg = secondSubString.replace('amp;', '');
                System.debug('Img: ' + canImg);  
            }  
        }  

        Account newAcc2 = new Account();
        newAcc1 = [select Activity_Indicator__pc from Account where id=:acc.id];

        if(newAcc2.Activity_Indicator__pc!= null)
        {
            System.debug('Rich field:');
            System.debug(newAcc1.Candidate_Image__c);

            String firstSubString = newAcc2.Activity_Indicator__pc.substringBetween('<img', 'img>');
            System.debug('First substring: ' + firstSubString);

            if(firstSubString  != null)
            {
                String secondSubString = firstSubString.substringBetween('src="', '"');
                System.debug('Second substring: ' + secondSubString);

                actImg = secondSubString.replace('amp;', '');
                System.debug('Img: ' + actImg);  
            }  
        }      

    }

    public void saveIt() {

        upsert acc;

        String myString = 'StringToBlob';
        Blob myBlob = Blob.valueof(myString);
        system.debug('this is account id'+acc.id);
            a.parentId = acc.Id;
            a.OwnerId = UserInfo.getUserId();

         try
         {
        insert a;
         System.debug('this is inserted attachment'+a);
         }
            catch(DMLException e)
            {
                System.debug('insertion failed'+e.getMessage());
            }
    }
}
  • 2
    What does that System.debug() statement tell you when it swallows the exception? – David Reed Sep 5 '18 at 18:36
2

You are not doing anything with myBlob. You need to add it to the body field of the Attachment you are creating.

a.body = myBlob;

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.