I have a part of a visualforce page that redirects to a URL that works. I have another condition in my controller that is supposed to show an error condition if a certain condition is false. It only shows if I remove the part rendered="{!IF(accList.size!=null, true, false)}" oncomplete="complete1();
Because of the rendered part in my command button, I don't see the error message on my page.
Is it possible to call a page redirect directly from my controller or stop the rerender of my command button?
Visualforce
`<script>
function complete1() {
RedirectAccount();
}
</script>
<apex:commandButton value="Update all Contacts" action="{!updateContacts}" rendered="{!IF(accList.size!=null, true, false)}" oncomplete="complete1();" >
<apex:actionfunction name="RedirectAccount" action="{!RedirectAccount}"/>
</apex:commandButton>
<apex:messages />`
Controller
`public void cloneRec()
{
List<Contact> lstContact = [SELECT Accountid FROM Contact WHERE Splits__c=true];
if(lstContact.size()>0){
ID AccSel=lstContact[0].Accountid;
system.debug('AccSel '+AccSel);
Account acc = [SELECT Id,Name FROM Account WHERE Id =:AccSel];
accClone = acc.clone(false,true,false,false);
List<Account> CheckAcc =[SELECT id, (SELECT Splits__c FROM Contacts) FROM Account WHERE id IN (SELECT AccountId FROM Contact WHERE Splits__c=true)];
if(CheckAcc.size()>1){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'No splitting'));
system.debug('CheckAcc.size() '+ CheckAcc.size());
}Else{
insert accClone;
IsMade = '1';
accClone.Name = 'TESTCLONE' + accClone.Name;
update accClone;
idOfRecNew = accClone.id;
List<Contact> cons = new List<Contact>();
List<Contact> con = [SELECT Id,firstname, LastName, AccountId,Splits__c FROM Contact WHERE AccountId = : acc.Id AND Splits__c=true];
for(Contact c : con)
{
Contact conClone = c.clone(false,true,false,false);
conClone.AccountId = accClone.Id;
conClone.Splits__c = false;
cons.add(conClone);
}
insert cons;
List<Contact> conOld =[SELECT id,Accountid FROM Contact WHERE Splits__c=true AND Accountid =:AccSel];
system.debug('conOld '+conOld);
delete conOld;
}
}
}
public PageReference RedirectAccount(){
List<Account> CheckAcc =[SELECT id, (SELECT Splits__c FROM Contacts) FROM Account WHERE id IN (SELECT AccountId
FROM Contact WHERE Splits__c=true)];
if(IsMade=='1'){
system.debug('redirection has fired ');
PageReference retURL = new PageReference('https://cs89.salesforce.com/'+idOfRecNew);
retURL.setRedirect(true);
return retURL;}
return null;
}`