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

Test code for APEX Class

Dear all,


I am trying to deloy the following APEX Class, which is used as an extension on an VF page.


The purpose is to upload an attachment (via the VF Page) on the RCS_WhiteBook__c object and store the attachment id in a custom field (Picture_id__c).

The code is working fine in the sandbox but I have issues deploying.


Many thanks for your help!


public class ExtUploadPicture  {

    private Attachment attachment;
    private final ApexPages.standardController theController;  
    private Id contId = ApexPages.currentPage().getParameters().get('objectId');
    //private Id userId = ApexPages.currentPage().getParameters().get('userId');
    private Id attId;
    public ExtUploadPicture (ApexPages.StandardController con) {
        theController = con;
        this.attachment = (Attachment) con.getRecord();
        this.attachment.ParentId = contId;
    public PageReference updateRCSWhiteBook() {
        this.attachment.Name = 'Upload.jpg';
        insert this.attachment;
        RCS_Whitebook__c cont = [Select Picture_Id__c, Id From RCS_Whitebook__c Where Id = :this.contId];
        cont.Picture_Id__c = this.attachment.Id;
        update cont;
        PageReference pageRef = new PageReference('/'+ this.contId);
        return pageRef;


Rajesh SriramuluRajesh Sriramulu




private class ExtUploadPicture_test{

private static testmethod void ExtUploadPicture_test()


ExtUploadPicture eup = new ExtUploadPicture();

PageReference pageRef = new PageReference('/'+ this.contId); 




try this and let me know any issues





Many thanks for your answer.


I get the following error when I try to save...


Error: Compile Error: Variable does not exist: this.contId at line 6 column 48

Rajesh SriramuluRajesh Sriramulu



yes it will not tobe save because it is private variable that cannot be access out side the class we can left blank




You can use something like this to assign a parameter in a test class:


ApexPages.currentPage().getParameters().put('objectId', YourObject.Id);