• Temurif
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hello, 

I am new to apex and got this below code from Jeff Douglas' blog. Can you help for writing test code for below controller:
public class UploadAttachmentController2 {
    
    public String selectedType {get;set;}
    public Boolean selectedAwesomeness {get;set;}
    public String description {get;set;}
    private SFDC_Issue__c Issue {get;set;} 
    public String fileName {get;set;}
    public Blob fileBody {get;set;}
    
    public UploadAttachmentController2(ApexPages.StandardController controller) { 
        this.Issue = (SFDC_Issue__c)controller.getRecord();
    }   
    
    // creates a new Belgeler__c record
    private Database.SaveResult saveCustomAttachment() {
        Belgeler__c  obj = new Belgeler__c ();
        obj.Adim__c = Issue.Id; 
        obj.description__c = description;
   
        // fill out cust obj fields
        return Database.insert(obj);
    }
    
    // create an actual Attachment record with the Belgeler__c as parent
    private Database.SaveResult saveStandardAttachment(Id parentId) {
        Database.SaveResult result;
        
        Attachment attachment = new Attachment();
        attachment.body = this.fileBody;
        attachment.name = this.fileName;
        attachment.parentId = parentId;
        // inser the attahcment
        result = Database.insert(attachment);
        // reset the file for the view state
        fileBody = Blob.valueOf(' ');
        return result;
    }
    
    /**
    * Upload process is:
    *  1. Insert new Belgeler__c record
    *  2. Insert new Attachment with the new Belgeler__c record as parent
    *  3. Update the Belgeler__c record with the ID of the new Attachment
    **/
    public PageReference processUpload() {
        try {
            Database.SaveResult customAttachmentResult = saveCustomAttachment();
        
            if (customAttachmentResult == null || !customAttachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save attachment.'));
                return null;
            }
        
            Database.SaveResult attachmentResult = saveStandardAttachment(customAttachmentResult.getId());
        
            if (attachmentResult == null || !attachmentResult.isSuccess()) {
                ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 
                  'Could not save attachment.'));            
                return null;
            } else {
                // update the custom attachment record with some attachment info
                Belgeler__c customAttachment = [select id from Belgeler__c where id = :customAttachmentResult.getId()];
               
                customAttachment.Sonuc__c = attachmentResult.getId();
                update customAttachment;
            }
        
        } catch (Exception e) {
            ApexPages.AddMessages(e);
            return null;
        }
        
        return new PageReference('/'+Issue.Id);
    }
    
    public PageReference back() {
        return new PageReference('/'+Issue.Id);
    }     

}


I have a custom object (Properties). Properties are owning another custom object (Property Unit). 
Property Units have lookup fields which are (Tenant) and (Landlord). 

A property owns about 40 units on average. So we have 40 different tenants. 

I want to display list of Tenants on a text field on Property upon each new update. Is there a way to do it? 

I tried to test sending mass emails using following test code

sited form Visualforce Manual.

But failed to have received an error message.

Any suggestion appreciated.

In case of sending a singlemassage test,it was OK.

 

 Error Message:-

       System.EmailException:
       SendEmail failed. First exception on row 0;
       first error: INVALID_ID_FIELD,
       Visualforce templates are not currently supported for mass email.: []

 

 Test Code:-

    User you = [ SELECT Email
             FROM User
             WHERE Id = :UserInfo.getUserId()
             LIMIT 1 ];
    EmailTemplate template = [ SELECT Id
                           FROM EmailTemplate
                           WHERE DeveloperName = 'Test_Template'
                             LIMIT 1 ];
     Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
     mail.templateId = template.Id;
    mail.targetObjectIds = new Id[] { you.Id };
    mail.setSaveAsActivity(false);
     Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });

 

  • April 04, 2010
  • Like
  • 0