You need to sign in to do that
Don't have an account?
Kamil 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):
And the test for it:
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(); } }
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:
And here is result when run your test class:
Hope this will help you.
Thanks
Gulshan Raj
All Answers
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.
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
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:
And here is result when run your test class:
Hope this will help you.
Thanks
Gulshan Raj