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
Kamil MieczakowskiKamil Mieczakowski 

void method runs no effect

Hi All, I am really baffled by this. When I run the below code as a simple method with no type it runs fine and websites in the database are being updated (code executes as desired). However, when I add the type of 'void' to the method, it seemingly runs fine without any errors but nothing happens in consequence (websites aren't updated/stripped of the desired components such as 'http', 'www', etc.

What is the reason behind this?

I believe this should be a void method type and I need it to be of void type for testing. If I change it to non-void it runs fine in Sandbox but then I cannot refer to it in the test.

​I am sorry if I am asking about something obvious, I am new to this game.

Here's the code (with 'void' type added to the method):
 
public class standardiseWebsites {
 
    public void standardiseWebsites(){
    //extracting the host from the address for websites that have web-protocol (http:// and https://)
    list<Account> accts = [SELECT Website FROM Account WHERE Website LIKE 'http://%' OR Website LIKE 'https://%'];
        for (Account acct : accts){            
            string website = acct.Website;
            Url u = new Url(acct.Website);
            acct.Website = u.getHost();
            update acct;
        }
        
      //removing first 4 digits of the address if it starts with 'www.' (non-web-protocol values)
      list<Account> accts2 = [SELECT Website FROM Account WHERE Website LIKE 'www.%'];
        for (Account acct2 : accts2){
            string website = acct2.website;
            website = website.substring(4);
            acct2.website = website;
            update acct2;
        }
    }
}



And the test for it:
 
@isTest
public class standardiseWebsitesTest {
    static testMethod void standardiseWebsitesTest(){
		
        //Run the test
		standardiseWebsites ca = new standardiseWebsites();
        ca.standardiseWebsites();
        
    }
}


 
Best Answer chosen by Kamil Mieczakowski
GulshanRajGulshanRaj
Hi Kamil,

If this is the real code then I have few points to highlight:
1)  When you removed void, it behave as a constructor. So, you code will execute immediatly as soon as your class instance get created.
2) But when you provide any return type even void then it will act like a function, and this function will call only when you call by instance of object (if function in not static).

I applied debug to your code to show you how it reacts:
public class standardiseWebsites {
  public standardiseWebsites()
  {
  	system.debug('----INSIDE CONSTRUCTOR----');
  }
    public void standardiseWebsites(){
   	system.debug('----INSIDE FUNCTION----');
    //extracting the host from the address for websites that have web-protocol (http:// and https://)
    list<Account> accts = [SELECT Website FROM Account WHERE Website LIKE 'http://%' OR Website LIKE 'https://%'];
        for (Account acct : accts){            
            string website = acct.Website;
            Url u = new Url(acct.Website);
            acct.Website = u.getHost();
            update acct;
        }
        
      //removing first 4 digits of the address if it starts with 'www.' (non-web-protocol values)
      list<Account> accts2 = [SELECT Website FROM Account WHERE Website LIKE 'www.%'];
        for (Account acct2 : accts2){
            string website = acct2.website;
            website = website.substring(4);
            acct2.website = website;
            update acct2;
        }
    }
}

And here is result when run your test class:
User-added image

Hope this will help you.


Thanks
Gulshan Raj

All Answers

Dushyant SonwarDushyant Sonwar

Hi Kamil ,

I think you are not rerendering your visual force page when calling void method. You need to rerender your page to see the output.
Below are the examples that will help you how to rerender a vf page
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm?search_text=actionFunction

http://www.jitendrazaa.com/blog/webtech/salesforce-tutorial-create-simple-ajax-based-visualforce-page/

Hope this helps.

Kamil MieczakowskiKamil Mieczakowski
Hi Dushyant, I just want to update the standard Account entity, I don't think it has much to do with Visualforce.
Shiva RajendranShiva Rajendran
Hi Kamil ,

Check if your code worked properly , i hope it would have throwed error, since you have used soql inside for loop.May be Too many queries exceptions would have happened. See you log after executing that method.

Also could you clarify me on your question on the below lines to help you better.
When I run the below code as a simple method with no type it runs fine and websites in the database are being updated .
//no type means?
Let me know if you need further help.
Thanks and Regards,
Shiva RV

 
GulshanRajGulshanRaj
Hi Kamil,

If this is the real code then I have few points to highlight:
1)  When you removed void, it behave as a constructor. So, you code will execute immediatly as soon as your class instance get created.
2) But when you provide any return type even void then it will act like a function, and this function will call only when you call by instance of object (if function in not static).

I applied debug to your code to show you how it reacts:
public class standardiseWebsites {
  public standardiseWebsites()
  {
  	system.debug('----INSIDE CONSTRUCTOR----');
  }
    public void standardiseWebsites(){
   	system.debug('----INSIDE FUNCTION----');
    //extracting the host from the address for websites that have web-protocol (http:// and https://)
    list<Account> accts = [SELECT Website FROM Account WHERE Website LIKE 'http://%' OR Website LIKE 'https://%'];
        for (Account acct : accts){            
            string website = acct.Website;
            Url u = new Url(acct.Website);
            acct.Website = u.getHost();
            update acct;
        }
        
      //removing first 4 digits of the address if it starts with 'www.' (non-web-protocol values)
      list<Account> accts2 = [SELECT Website FROM Account WHERE Website LIKE 'www.%'];
        for (Account acct2 : accts2){
            string website = acct2.website;
            website = website.substring(4);
            acct2.website = website;
            update acct2;
        }
    }
}

And here is result when run your test class:
User-added image

Hope this will help you.


Thanks
Gulshan Raj
This was selected as the best answer