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
AnjaneyluAnjaneylu 

development

 Hi experts..
I am new to apex coding..
i have written the apex class and the from workbench i want to acces that class ..
in the workbench i have created object for the class.
so please kindly help me.
I need to access the apex class.
my apex class
public class firstclass{
integer i= 10;
integer j= 20;
integer k=i+j;
public firstclass()
    {
    system.debug( The sum of the i and j is :  '+k');
    }
}



 
AnjaneyluAnjaneylu
firstclass ab= new firstclass();
system.debug( '+k');
this is what i have called from the workbench..
my requirement is to access the k..
Please help me..
PratikPratik (Salesforce Developers) 
Hi Anji,

Do you mean "Execute anonymous" in workbench?
You can execute your code in workbence: 
Go to Workbench-> utilities->Apex Execute  and there you can put your code to executes it anonymously.

Thanks,
Pratik
AnjaneyluAnjaneylu
i have executed it through  Workbench-> utilities->Apex Execute.
and there i entered
firstclass ab= new firstclass();
system.debug( '+k');
 but in the output it is showing 

29.0 APEX_CODE,DEBUG Execute Anonymous: firstclass ab= new firstclass();
Execute Anonymous: system.debug( '+k');
23:38:55.067 (67516410)|EXECUTION_STARTED 23:38:55.067 (67528857)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex 23:38:55.068 (68991275)|METHOD_ENTRY|[1]|01p90000006dE7Y|firstclass.firstclass()
23:38:55.069 (69008090)|METHOD_EXIT|[1]|firstclass
23:38:55.069 (69107802)|CONSTRUCTOR_ENTRY|[1]|01p90000006dE7Y|<init>()
23:38:55.069 (69206790)|USER_DEBUG|[7]|DEBUG|+k
23:38:55.069 (69219373)|CONSTRUCTOR_EXIT|[1]|01p90000006dE7Y|<init>()
23:38:55.069 (69256298)|USER_DEBUG|[2]|DEBUG|+k
23:38:55.069 (69301983)|CODE_UNIT_FINISHED|execute_anonymous_apex
23:38:55.070 (70577296)|EXECUTION_FINISHED



but i want to get result as 30.(10+20)
PratikPratik (Salesforce Developers) 
Hi Anji,

You can simply execute below code in execute anonymous. You don't need class and constructor in anonymous block:

integer i= 10;
integer j= 20;
integer k = i+j;
system.debug( 'The sum of the i and j is :'  +k);

Thanks,
Pratik
 
AnjaneyluAnjaneylu
Thank you Pratik.
but my requirement is to access the variable k (integer) from the apex class to workbench..
what i have to write the code in the workbench for that class to access that variable which is inside that apex class.

Thanks and Regards,
anji
lakslaks

Hi Anji,

The problem in your code is just in the System.debug statement.
It should be as follows: 
 
public class firstclass
{
    integer i= 10;
    integer j= 20;
    integer k=i+j;

    public firstclass()
    {
        system.debug('The sum of the i and j is :  '+k);
    }
}

Note the placement of single quotes '  ' in the line. That's how it should be written. 
The sum of the i and j is : is your debug statement that you want to print out, which should be within '  ' and k is the variable whose value you want to print.

After making this change the same lines that you used to execute in Apex Execute should give you the result
 
32.0 APEX_CODE,DEBUG
Execute Anonymous: firstclass ab= new firstclass();
Execute Anonymous: system.debug( '+k');
15:31:00.090 (90488583)|EXECUTION_STARTED
15:31:00.090 (90499236)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
15:31:00.090 (90891063)|METHOD_ENTRY|[1]|01p90000006PAVS|firstclass.firstclass()
15:31:00.090 (90901585)|METHOD_EXIT|[1]|firstclass
15:31:00.091 (91003100)|CONSTRUCTOR_ENTRY|[1]|01p90000006PAVS|<init>()
15:31:00.091 (91171116)|USER_DEBUG|[9]|DEBUG|The sum of the i and j is :  30
15:31:00.091 (91183562)|CONSTRUCTOR_EXIT|[1]|01p90000006PAVS|<init>()
15:31:00.091 (91216758)|USER_DEBUG|[2]|DEBUG|+k
15:31:00.091 (91251670)|CODE_UNIT_FINISHED|execute_anonymous_apex
15:31:00.092 (92427003)|EXECUTION_FINISHED

If this solved your issue kindly mark it as the solution.

Thanks & Regards,
Lakshmi.
AnjaneyluAnjaneylu
Hi Lakshmi, Thankyou for your reply.
Now my requirement is to access that k variable in the Workbench.
so that i have written this code in the workbench as follows,
firstclass abc= new firstclass();
system.debug(' the sum of I  and J is : '+k);


COMPILE ERROR: Variable does not exist: k
LINE: 2 COLUMN: 43
So please kindly tell me how to get the value of k.

Thanks and regards,
anji
 
lakslaks
Hi Anji,

I don't find any reason why such an error should occur. I tried executing the code.

Hope you have corrected the System.debug statement in your apex class firstclass too and not just in the code snippet executed in workbench.

Thanks & Regards,
Lakshmi.
lakslaks
Apex Class:
public class firstclass
{
    integer i= 10;
    integer j= 20;
    integer k=i+j;

    public firstclass()
    {
        system.debug('The sum of the i and j is :  '+k);
    }
}

 Code to be executed in Workbench:
firstclass abc= new firstclass();
You shouldn't be writing the debug statement in the Apex execute area. The variable will ofcourse not be recognized there.

Thanks & Regards,
Lakshmi.
 
lakslaks
Hi,

Guess your problem was solved by the above suggestion.
If so, kindly take a minute to mark it as the solution.


Thanks & Regards,
Lakshmi.