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
Suresh RaghuramSuresh Raghuram 

difference between void and non void method

hi i am always doing wrong with void and non void method i am not understanding when , where and how these methods are used , could any body explain in detail

Best Answer chosen by Admin (Salesforce Developers) 
eric.luiseric.luis

It is required deppending on your application.

 

You have many ways to do the samething, but in some cases, you need to use a non-void method to reach your goal.

Normally, you have to use non-void in action buttons (for visualforce) and get methods (this MUST return the variable value, or they don't need to exist).

You should read a little bit about Object Oriented Programming.

All Answers

eric.luiseric.luis

It is required deppending on your application.

 

You have many ways to do the samething, but in some cases, you need to use a non-void method to reach your goal.

Normally, you have to use non-void in action buttons (for visualforce) and get methods (this MUST return the variable value, or they don't need to exist).

You should read a little bit about Object Oriented Programming.

This was selected as the best answer
Damien_Damien_

Generally, you youse non-void methods when you need to get something from the object.

 

public List<Account> getAssocAccounts(Set<Id> accIds)
{
  return [SELECT Id FROM Account WHERE Id IN: accIds];
}

 

In this case, we are passing a set of ids into this method because we need to get associated accounts with those ids.

 

public void setAssocAccounts(Set<Id> accIds)
{
  myAccountList = [SELECT Id FROM Account WHERE Id IN: accIds];
}

 In this instance, we just need to do an action upon something but we dont need to return the results to the caller.

 

Hopefully these simple examples explain.  It might be a good idea to do like eric.luis said and read more about OOP.