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
Neil KimNeil Kim 

Commandbutton/onclick is clicked automatically

Hi. It worked some hours ago. Suddenly it do abnormally..
Could you please check it for me? why is this happened?

Here is visualforce page form
<apex:page showHeader="true" sidebar="true" controller="Test">
	<apex:form id="fr1">
		<apex:inputTextarea id="text" value="{!text}" cols="4" style="width:100%; height:80px"/><br/>
		From : &nbsp;<apex:outputText value="+565656"/><br/>
		To : &nbsp;<apex:inputText id="to" value="{!to}" style="width:80%"/>
		<apex:commandButton onClick="{!sendSMS}" value="Send Message" immediate="false"/>
	</apex:form>
</apex:page>

and controller
public with sharing class Test {
	public TwilioTest() {}
	public String text {get; set;}
	public String to {get; set;}

	public void getsendSMS(){
		String ACCOUNT_SID = 'AAA';
		String AUTH_TOKEN = 'VVV';
		if( to != null ){ <------ what I added to avoid the error.
			String phone_to = '+82'+to.leftPad(1);

			TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

			Map<String,String> properties = new Map<String,String> {
		        'To'   => phone_to,
		        'From' => '+AAAA,
		        'Body' => text
			};

			TwilioMessage message = client.getAccount().getMessages().create(properties);	
		}
		
	}
}

When I loaded visualforce page, getsendSMS function is called right away.
I'm not click commandbutton..
For avoiding this error, I insert If statement...

Is there any clue?

Please help.
Best Answer chosen by Neil Kim
BALAJI CHBALAJI CH
Hi YoungHoon,

I think initially you had javascript with ActionSupport or AcionFunction. You can change OnClick event to execute action in CommandButton. Please find below modified code:
VF Page:
<apex:page showHeader="true" sidebar="true" controller="Test">
	<apex:form id="fr1">
		<apex:inputTextarea id="text" value="{!text}" cols="4" style="width:100%; height:80px"/><br/>
		From : &nbsp;<apex:outputText value="+565656"/><br/>
		To : &nbsp;<apex:inputText id="to" value="{!to}" style="width:80%"/>
		<apex:commandButton value="Send Message" action="{!sendSMS}" immediate="false"/>
	</apex:form>
</apex:page>

Controller:
public with sharing class Test {
	public TwilioTest() {}
	public String text {get; set;}
	public String to {get; set;}

	public void sendSMS(){
		String ACCOUNT_SID = 'AAA';
		String AUTH_TOKEN = 'VVV';
			String phone_to = '+82'+to.leftPad(1);

			TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

			Map<String,String> properties = new Map<String,String> {
		        'To'   => phone_to,
		        'From' => '+AAAA,
		        'Body' => text
			};

			TwilioMessage message = client.getAccount().getMessages().create(properties);			
	}
}

 

All Answers

BALAJI CHBALAJI CH
Hi YoungHoon,

I think initially you had javascript with ActionSupport or AcionFunction. You can change OnClick event to execute action in CommandButton. Please find below modified code:
VF Page:
<apex:page showHeader="true" sidebar="true" controller="Test">
	<apex:form id="fr1">
		<apex:inputTextarea id="text" value="{!text}" cols="4" style="width:100%; height:80px"/><br/>
		From : &nbsp;<apex:outputText value="+565656"/><br/>
		To : &nbsp;<apex:inputText id="to" value="{!to}" style="width:80%"/>
		<apex:commandButton value="Send Message" action="{!sendSMS}" immediate="false"/>
	</apex:form>
</apex:page>

Controller:
public with sharing class Test {
	public TwilioTest() {}
	public String text {get; set;}
	public String to {get; set;}

	public void sendSMS(){
		String ACCOUNT_SID = 'AAA';
		String AUTH_TOKEN = 'VVV';
			String phone_to = '+82'+to.leftPad(1);

			TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

			Map<String,String> properties = new Map<String,String> {
		        'To'   => phone_to,
		        'From' => '+AAAA,
		        'Body' => text
			};

			TwilioMessage message = client.getAccount().getMessages().create(properties);			
	}
}

 
This was selected as the best answer
Neil KimNeil Kim
@BALAJI
Thanks. What you said is exactly right.
Really helpful!.
BALAJI CHBALAJI CH
My Pleasure.

Best Regards,
BALAJI