+ Start a Discussion
MicheleMcgeoyMicheleMcgeoy 

How to run my class from within Visual Force page

I'm new to coding, so my apologies for the basic question. I've put a button on the Campaign layout that calls a Visual Force page that puts some values on the screen. I then want it to run the following Apex code:

 

public CloneClassAttendance() {
Campaign NewCamp = new Campaign(Name = 'Test Campaign record');
insert NewCamp;

Campaign NewCamp = [SELECT Type FROM Campaign WHERE Name =:'Test Campaign record' limit 1];

NewCamp.Type = 'Class Attendance';

update NewCamp;
}
}

 

What do I put in the Visual Force page to run the CloneClassAttendance class?

 

Thanks, Michele

digamber.prasaddigamber.prasad

Hi,

 

You need to call this function CloneClassAttendance() in your vf page as action. Something like below:-

 

<apex:page controller="<name of your class>" action="{!CloneClassAttendance}">
   <!-- content of the page-->
</apex:page>

 Let me know if you have any specific question about this.

 

Happy to help you!

 

 

MicheleMcgeoyMicheleMcgeoy

Thanks for your help.

I tried your suggestion and am getting the following error when I try to save the page:

   

Error: Unknown constructor 'CloneClassAttendance.CloneClassAttendance(ApexPages.StandardController controller)'

 

I put it in as an extension so I could place a button on the Campaign layout. Here is the page:

<apex:page standardController="Campaign" extensions="CloneClassAttendance" action="{!insertRecord}">

  <h1>Cloning Attendance</h1>
  <p> Class name is {! campaign.name} </p>
  <p> Class id is:   {! campaign.id} </p>
  <p> Class start date is:   {! campaign.startdate} </p>
  <p> Class end date is:   {! campaign.enddate} </p>
  <p> Next week is: {! campaign.startdate+7} </p>
</apex:page>

 

Here is the class:

public class CloneClassAttendance {

public void insertRecord(){
Campaign NewCamp = new Campaign(Name = 'Test Campaign record');
insert NewCamp;

Campaign NewCamp1 = [SELECT Type FROM Campaign WHERE Name = 'Test Campaign record'];
NewCamp.Type = 'Class Attendance';
update NewCamp1;
}
}

 

Thanks for your help.

Michele

digamber.prasaddigamber.prasad

Hi,

 

Please see below extension controller. You had missed constructor:-

 

public class CloneClassAttendance {
	private final Campaign camp;
	
	public CloneClassAttendance(ApexPages.StandardController stdController){
		this.camp = (Campaign)stdController.getRecord();
	}

	public void insertRecord(){
		Campaign NewCamp = new Campaign(Name = 'Test Campaign record');
		insert NewCamp;

		Campaign NewCamp1 = [SELECT Type FROM Campaign WHERE Name = 'Test Campaign record'];
		NewCamp.Type = 'Class Attendance';
		update NewCamp1;
	}
}

 

Please let me know if you have any problem.

 

Happy to help you!

 

MicheleMcgeoyMicheleMcgeoy

 

Thanks for your help. I'm now trying to assign values from the page into the class and get the following error:

Compile Error: unexpected token: '{' at line 14 column 23

 

public class CloneClassAttendance {
    private final Campaign camp;
    
    public CloneClassAttendance(ApexPages.StandardController stdController){
        this.camp = (Campaign)stdController.getRecord();
    }

    public void insertRecord(){
        Campaign NewCamp = new Campaign(Name = 'Test Campaign record');
        insert NewCamp;

        Campaign NewCamp1 = [SELECT Type FROM Campaign WHERE Name = 'Test Campaign record'];
        NewCamp.Type = 'Class Attendance';
        NewCamp.Name = {! campaign.name};
        update NewCamp1;
    }
}

 

When the page calls the class, do I still have access to the original object data it was called from?

Thanks, Michele

digamber.prasaddigamber.prasad

Hi,

 

Below is updated code and should work for you:-

 

public class CloneClassAttendance {
    private final Campaign camp;
    
    public CloneClassAttendance(ApexPages.StandardController stdController){
        this.camp = (Campaign)stdController.getRecord();
    }

    public void insertRecord(){
        Campaign NewCamp = new Campaign(Name = 'Test Campaign record');
        insert NewCamp;

        Campaign NewCamp1 = [SELECT Type FROM Campaign WHERE Name = 'Test Campaign record'];
        NewCamp.Type = 'Class Attendance';
        NewCamp.Name = camp.name;
        update NewCamp1;
    }
}

 

Problem was that you were trying VF syntax in apex which is not allowed.

 

Let me know if you have any question.

 

Happy to help you!

MicheleMcgeoyMicheleMcgeoy

Thanks, that's helpful. It seems to work well with several fields, but when I get down to status I get the following error message which seems to happen with all fields I have commented out below:

 

SObject row was retrieved via SOQL without querying the requested field: Campaign.Status

 

public class CloneClassAttendance {
    private final Campaign camp;
    
    public CloneClassAttendance(ApexPages.StandardController stdController){
        this.camp = (Campaign)stdController.getRecord();
    }

    public void insertRecord(){
        Campaign NewCamp = new Campaign(Name = 'Test Campaign record');
        insert NewCamp;

        Campaign NewCamp1 = [SELECT Type, Status FROM Campaign WHERE Name = 'Test Campaign record'];
        NewCamp1.Type = 'Class Attendance';
        NewCamp1.IsActive = true;      
        NewCamp1.Name = camp.name + camp.startdate + 7;
        NewCamp1.Parentid = camp.id;
        NewCamp1.Startdate = camp.startdate + 7;
        NewCamp1.Status = camp.status;
//        NewCamp1.teacher__c = camp.teacher__c;
//        NewCamp1.Day_of_Week__c = camp.Day_of_Week__c;
//        NewCamp1.Hours_Per_Class__c = camp.Hours_Per_Class__c;
//        NewCamp1.Location__c = camp.Location__c;
//        NewCamp1.End_Time__c = camp.End_Time__c;
//        NewCamp1.Description = camp.Description;
        
        update NewCamp1;
    }
}

 

Why would this work with the first 6 fields but nothing after?

 

Thanks, Michele

digamber.prasaddigamber.prasad

Hi,

 

Could you please try below code:-

 

public class CloneClassAttendance {
    private final Campaign camp;
    
    public CloneClassAttendance(ApexPages.StandardController stdController){
        this.camp = (Campaign)stdController.getRecord();
    }

    public void insertRecord(){
        Campaign NewCamp = new Campaign(Name = 'Test Campaign record');
        insert NewCamp;

        Campaign NewCamp1 = [SELECT Type, Status, IsActive, Name, ParentId, StartDate, Teacher__c, Day_of_Week__c, Hours_Per_Class__c, Location__c, End_Time__c, Description FROM Campaign WHERE Name = 'Test Campaign record'];
        NewCamp.Type = 'Class Attendance';
        NewCamp.Name = camp.name;
		
       NewCamp1.Type = 'Class Attendance';
        NewCamp1.IsActive = true;      
        NewCamp1.Name = camp.name + camp.startdate + 7;
        NewCamp1.Parentid = camp.id;
        NewCamp1.Startdate = camp.startdate + 7;
        NewCamp1.Status = camp.status;
		NewCamp1.teacher__c = camp.teacher__c;
		NewCamp1.Day_of_Week__c = camp.Day_of_Week__c;
		NewCamp1.Hours_Per_Class__c = camp.Hours_Per_Class__c;
		NewCamp1.Location__c = camp.Location__c;
		NewCamp1.End_Time__c = camp.End_Time__c;
		NewCamp1.Description = camp.Description;
		
		update NewCamp1;
    }
}

 Please let me know if you still see any problem.

 

 

 

MicheleMcgeoyMicheleMcgeoy

Changing the SELECT statement didn't seem to help. It looks like what made the difference is to change the page to include all of those fields on it.

Though I'm confused as to why that actually made a difference. Does it make sense to you?

 

Thanks, Michele

 

<apex:page standardController="Campaign" extensions="CloneClassAttendance" action="{!insertRecord}">

  <h1>Cloning Attendance</h1>
  <p> Class name is {! campaign.name} </p>
  <p> Class id is:   {! campaign.id} </p>
  <p> Class start date is:   {! campaign.startdate} </p>
  <p> Class end date is:   {! campaign.enddate} </p>
  <p> Next week is: {! campaign.startdate+7} </p>
  <p> Class Status is {! campaign.status} </p>
  <p> Class teacher is {! campaign.teacher__c} </p>
  <p> Class DOW is {! campaign.Day_of_Week__c} </p>
  <p> Class Hours_Per_Class__c is {! campaign.Hours_Per_Class__c} </p>
  <p> Class Location is {! campaign.Location__c} </p>
  <p> Class Location is {! campaign.End_Time__c} </p>
  <p> Class Description is {! campaign.Description} </p>
 
</apex:page>