You need to sign in to do that
Don't have an account?
Question on command button cancel
Hi
How to blank out all the input field values on clicking the cancel command button.
below is my code. When the cancel button is clicked the data in the field should be removed. So that user can
enter the new values and then click on save.
<apex:page controller="example2"> <apex:form > <apex:pageBlock mode="edit" > <apex:pageBlockSection > </apex:pageBlockSection> <apex:pageBlockSection > <apex:pageBlockTable value="{!Data}" var="dt"> <apex:column > Phone: <apex:inputField value="{!dt.phone}"/><br /> Email: <apex:inputField value="{!dt.email}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlockSection> <apex:pageBlockButtons> <apex:commandButton action="{!save}" value="Save"/> <apex:commandButton action="{!cancel}" value="Cancel"/> </apex:pageBlockButtons> </apex:pageBlock> </apex:form> </apex:page> public class example2{ public User user; public User getData() { user=[Select phone,email from User where id=:UserInfo.getUserId()]; return user; } public PageReference save() { update user; return null; } public PageReference cancel() { return null; } }
Hi,
this is how i would do it :
<apex:page controller="example2" action="{!init}"> <apex:form > <apex:pageBlock mode="edit" id="pg"> <apex:pageBlockSection > </apex:pageBlockSection> <apex:pageBlockSection > <apex:pageBlockTable value="{!Data}" var="dt"> <apex:column > Phone: <apex:inputField value="{!dt.phone}"/><br /> Email: <apex:inputField value="{!dt.email}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlockSection> <apex:commandButton action="{!save}" value="Save"/> <apex:commandButton action="{!cancel}" value="Cancel" reRender="pg"/> </apex:pageBlock> </apex:form> </apex:page> public class example2{ public User user; public List<Travelling__c> tra; public User getData(){ return user; } public PageReference init(){ user=[Select phone,email from User where id=:UserInfo.getUserId()]; return null; } public PageReference save() { tra = new List<Travelling__c>(); for(Travelling__c t:[Select TPhone__c,TEmail__c from Travelling__c where SFDC_ID__c=:UserInfo.getUserId()]){ t.TPhone__c=user.phone; t.TEmail__c=user.email; tra.add(t); } update tra; return null; } public PageReference cancel() { user.phone=''; user.email=''; return null; } }
I haven't tested it, so tell me if it is not working.
Kind Regards,
Catherin.
All Answers
Hi,
i do not really understand what you want to do. In your code you get the current user and you will update it with the value input.
So what do you want to do when you cancel, just blank the value and then the user can input new value and still update the current user ?
Or you want to create a new user with the new value input ?
In any case, i propose to use the action attribute in the <apex:page> in order to initialise your "user" object and to remove the "select " from the getData.
Then in your cancel method you can blank the values.
Hope this helps.
Catherin.
Given an id to the pageblock. id="pageblock" and rerender it on cancel button
Hi -
Thanks for the response and sorry for not explaining properly.
I am actually getting the current user info and asking the user to verify the values (Phone and email). If he thinks the values are not correct then he clicks on cancel,it should then just blank out the value and then user can input new value.
This new value should be updated in other custom object Travelling__c.
In addition to that i am getting this error when i click on save
System.Exception: Cannot modify a collection while it is being iterated
Actually i tried as you said for cancel , but still it is not working.
<apex:page controller="example2"> <apex:form > <apex:pageBlock mode="edit" id="pg"> <apex:pageBlockSection > </apex:pageBlockSection> <apex:pageBlockSection > <apex:pageBlockTable value="{!Data}" var="dt"> <apex:column > Phone: <apex:inputField value="{!dt.phone}"/><br /> Email: <apex:inputField value="{!dt.email}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlockSection> <apex:commandButton action="{!save}" value="Save"/> <apex:commandButton action="{!cancel}" value="Cancel" reRender="pg"/> </apex:pageBlock> </apex:form> </apex:page> public class example2{ public User user; public List<Travelling__c> tra; public User getData() { user=[Select phone,email from User where id=:UserInfo.getUserId()]; return user; } public PageReference save() { tra=[Select TPhone__c,TEmail__c from Travelling__c where SFDC_ID__c=:UserInfo.getUserId()]; for(Travelling__c t:tra) { t.TPhone__c=user.phone; t.TEmail__c=user.email; tra.add(t); } update tra; return null; } public PageReference cancel() { user.phone=null; user.email=null; return null; } }
Please advise. I am a newbie..to both Apex and Visualforce. Please guide me how to resvolve the above two..Thanks in advance
Hi,
this is how i would do it :
<apex:page controller="example2" action="{!init}"> <apex:form > <apex:pageBlock mode="edit" id="pg"> <apex:pageBlockSection > </apex:pageBlockSection> <apex:pageBlockSection > <apex:pageBlockTable value="{!Data}" var="dt"> <apex:column > Phone: <apex:inputField value="{!dt.phone}"/><br /> Email: <apex:inputField value="{!dt.email}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlockSection> <apex:commandButton action="{!save}" value="Save"/> <apex:commandButton action="{!cancel}" value="Cancel" reRender="pg"/> </apex:pageBlock> </apex:form> </apex:page> public class example2{ public User user; public List<Travelling__c> tra; public User getData(){ return user; } public PageReference init(){ user=[Select phone,email from User where id=:UserInfo.getUserId()]; return null; } public PageReference save() { tra = new List<Travelling__c>(); for(Travelling__c t:[Select TPhone__c,TEmail__c from Travelling__c where SFDC_ID__c=:UserInfo.getUserId()]){ t.TPhone__c=user.phone; t.TEmail__c=user.email; tra.add(t); } update tra; return null; } public PageReference cancel() { user.phone=''; user.email=''; return null; } }
I haven't tested it, so tell me if it is not working.
Kind Regards,
Catherin.
try this
<apex:page controller="example2">
<script>
function reset()
{
objphone.value ='';
objemail.value = '';
return false;
}
</script>
<apex:form >
<apex:pageBlock mode="edit" id="pg">
<apex:pageBlockSection >
</apex:pageBlockSection>
<apex:pageBlockSection >
<apex:pageBlockTable value="{!Data}" var="dt">
<apex:column >
Phone: <apex:inputField id="phone" value="{!dt.phone}"/><br />
Email: <apex:inputField id="email" value="{!dt.email}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="Cancel" onclick="jaVascript:return reset()"reRender="pg"/>
<script>
var objphone = document.getElementById('{!$Component.phone}');
var objemaIl = document.getElementById('{!$Component.emaIl}');
</script>
</apex:pageBlock>
</apex:form>
</apex:page>
public class example2{
public User user;
public List<Travelling__c> tra;
public User getData()
{
user=[Select phone,email from User where id=:UserInfo.getUserId()];
return user;
}
public PageReference save() {
tra=[Select TPhone__c,TEmail__c from Travelling__c where SFDC_ID__c=:UserInfo.getUserId()];
for(Travelling__c t:tra)
{
t.TPhone__c=user.phone;
t.TEmail__c=user.email;
tra.add(t);
}
update tra;
return null;
}
public PageReference cancel() {
user.phone=null;
user.email=null;
return null;
}
Catherin and Srinivas.....Thanks to both of you. It did the trick.!!!!
many Many Thanks again.....for your solutions......
hi guys -
one more question. How to write unit tests for the above controller. I have no idea what that means.
But i started writing little bit...
can you please pass me an idea of how to write unit test for the above controller..Thanks in advance.
Hi,
you need to instanciate your controller :
example2 ex = new example2();
ex.getData();
ex.save();
...
you need the right objects created first.
Kind Regards,
Catherin.
hi catherin ...thanks for the quick response..
Is this the way how i should be writing? if so i am getting error when saving the Apex class
Error: Compile Error: unexpected token: u
And same error for when inserting tv.? Any idea?
public class example2{ public User user; public List<Travelling__c> tra; public User getData(){ return user; } public PageReference init(){ user=[Select phone,email from User where id=:UserInfo.getUserId()]; return null; } public PageReference save() { tra = new List<Travelling__c>(); for(Travelling__c t:[Select TPhone__c,TEmail__c from Travelling__c where SFDC_ID__c=:UserInfo.getUserId()]){ t.TPhone__c=user.phone; t.TEmail__c=user.email; tra.add(t); } update tra; return null; } public PageReference cancel() { user.phone=''; user.email=''; return null; } static testMethod results() { User u=new User(LastName='renuami',....all required fields) insert u; Travelling__c tv=new Travelling__c(phone='1111111111',email='renuami@gmail.com'); //all required fields
insert tv; example2 ex=new example2(); ex.getData(); ex.init(); ex.save(); ex.cancel(); } }
i got it to work....
Now when i run the tests i got code coverage 100%.
I am happy now. But a little scared how it would work during deploying to production ....
static testMethod results() { example2 ex=new example2(); ex.getData(); ex.init(); ex.save(); ex.cancel(); }