You need to sign in to do that
Don't have an account?

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');
}
}
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');
}
}
system.debug( '+k');
this is what i have called from the workbench..
my requirement is to access the k..
Please help me..
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
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)
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
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
Hi Anji,
The problem in your code is just in the System.debug statement.
It should be as follows:
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
If this solved your issue kindly mark it as the solution.
Thanks & Regards,
Lakshmi.
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
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.
Code to be executed in Workbench: You shouldn't be writing the debug statement in the Apex execute area. The variable will ofcourse not be recognized there.
Thanks & Regards,
Lakshmi.
Guess your problem was solved by the above suggestion.
If so, kindly take a minute to mark it as the solution.
Thanks & Regards,
Lakshmi.