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
Walter@AdicioWalter@Adicio 

can two different classes work with the same variables?

 

here is what my problem is and maybe you can set me straight on what i should be writting instead of what i am trying to write. i ran into the "script too large" error. so i am trying to figure out how to break apart the script into different classes.

 

one script has some logic to run a update / insert command. what i am trying to do is run that same logic but before commiting to the database just present a type of "Are you sure?" option with a "yes", and only when they click "yes" will it actually commit to the database.

 

i know you can probably get this all in one script but please help me understand my choices for getting different scripts to work with the same variables.... because the script really has a lot going on and even if i re-write the whole thing to be more efficient. i am still going to run into "script too large" error eventually until i know how to get more than one class to talk to each other. im sure there is a better what to go about this.

 

 

here is an example:

 

in one script different variables are created. for example a task.

 

	public Task task;
	public Task getTask(){
		if(task==null) task = new Task();
		return task;
	}

 

so the user would fill out the subject line of that task .

 

in another script id like it to be able to see the same variable and subject value from the first script. so another script could do something like this.

 

 

if(task!=null && task.subject!=null && task.subject.length()>0){
     // some stuff
}

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
kbromerkbromer

A class can take a value from another class by instantiating the second class with the value you want to pass, and creating a constructor to handle that value.

 

For example, here my class myClass has a task called myTask, which I'm passing to my second class, called secondClass:

 

 

public with sharing class myClass{

public Task myTask{get; set;}

private void callSecondClass()
{
secondClass myClass = new secondClass(myTask);
}

}

 Then your class secondClass would have a constructor that accepts a task:

 

 

public with sharing class secondClass{

Task newTask;

public secondClass(Task t){

newTask = t;

}
}

 Now you can operate on that task T in your second class, secondClass.

 

EDIT: Important to note, your task in secondClass is of course now been assigned to newTask, not T.

 

 

All Answers

kbromerkbromer

A class can take a value from another class by instantiating the second class with the value you want to pass, and creating a constructor to handle that value.

 

For example, here my class myClass has a task called myTask, which I'm passing to my second class, called secondClass:

 

 

public with sharing class myClass{

public Task myTask{get; set;}

private void callSecondClass()
{
secondClass myClass = new secondClass(myTask);
}

}

 Then your class secondClass would have a constructor that accepts a task:

 

 

public with sharing class secondClass{

Task newTask;

public secondClass(Task t){

newTask = t;

}
}

 Now you can operate on that task T in your second class, secondClass.

 

EDIT: Important to note, your task in secondClass is of course now been assigned to newTask, not T.

 

 

This was selected as the best answer
Walter@AdicioWalter@Adicio
public class myClass{


public class myInnerClass{

object1

object2

object3

etc.........

}

private void callSecondClass()
{
secondClass myClass = new secondClass(myInnerClass);
}


}

 

 

Hi kbromer ,

 

thank you for this information. I understand what you are saying and this will help.

 

I have another question maybe you can help me with.

 

In my scenario there are at least 10 objects / variables where I need to maintain the state between the classes.

 

With your suggestion would you say I should be creating an innerClass which holds all objects / variables, and then pass the innerClass between the different classes in the manner you suggested?

 

for example what i placed above ?

 

 

Walter@AdicioWalter@Adicio

thank you 

SteveBowerSteveBower

I'd do it differently, with an instance of an external "data context" class....

 

class myDataClass {

        String x;

        Opportunity[] y;

        //Object1;

        //Object2;

        //Object3;   etc.

 

     public myDataClass() {

            this.x = "My Data Class";

     }

 

     public void LaughAtMyData() {

            system.debug('The state of myData is:');

            system.debug(''x:' + x);

     }

}

 

Then you can also, perhaps, build your other classes as "utility" class with static methods that take all their invocation persistent data from the myDataClass instance.   Putting

the data in it's own class is also useful because you may find it useful to build some helper methods of some sort which could be useful if called from multiple classes and it's awkward putting them all in the internal class you've defined in your example.

 

 

 

Class topClass() {

        myDataClass myData;

 

        public topClass() {

              myData = new myDataClass();

        }

 

        public void main() {

         // stuff.....

               MyFirstClass.callUtilityMethod(myData);

               myData.LaughAtMyData();

              MySecondClass.doSomeMore(myData);

        }

}

 

 

 

Class MyFirstClass {

            public static void myFirstClass(MyDataClass myData) {

                    // ... your processing....

                    myData.LaughAtMyData();

                    if (myData.x == 'My Data Class" && y.isEmpty()) return;

            }

}

 

 

Best, Steve.