0

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;
 }`

2 Answers 2

0

Change your action function to actionSupport like so:

<apex:commandButton value="Update All Contacts" action="{!updateContacts}" rendered="{!IF( AND(contacts != null, contacts.size > 0),true, false )}">
    <apex:actionSupport event="oncomplete" action="{!RedirectAccount}"  rerender="idofsomecomponentonpage" />
</apex:commandButton> 

OR you can simply do this in the controller ...

public PageReference updateContacts(){
    ... your update code...

    return RedirectAccount();
}
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;
}

Both of these would perform the same action and if you have an error like you're assuming in your request ( list greater than 1 ) your error will show in page messages. I would suggest you use rerender in your action support and give the ID of the element that contains your form and messages tag. Also in your URL for redirect- for maintenance, pull the server address from the headers using

ApexPages.currentPage().getHeaders()

so you're not having to update code when your instance is moved.

0

update your vf page as shown below and add rerender to your actionfunction

<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" rerender="pgmsg" action="{!RedirectAccount}"/> 
         </apex:commandButton>
   <apex:messages id="pgmsg"/>
5
  • This doesnt render the ApexPages.addmessage
    – Jenssen
    Sep 24, 2017 at 14:17
  • do you see system.debug('CheckAcc.size() '+ CheckAcc.size()); value in debug log?
    – Himanshu
    Sep 24, 2017 at 14:22
  • Yes I do see a value of 2
    – Jenssen
    Sep 24, 2017 at 14:42
  • I'd use action support in there not action function inside the button
    – Ronnie
    Sep 25, 2017 at 5:12
  • my bad @ronnie, i didn't realize that he is using action function inside command button.
    – Himanshu
    Sep 25, 2017 at 7:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .