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
Carter85Carter85 

Need Help resolving a "Cyclical server-side forwards detected" error

I'm trying to take a VF page I've created and which the user is currently viewing, convert it to a pdf on an actionbutton click and save it to an Object record, but everything I try returns the error I referenced in the subject of this thread.
My current method is this:
public pageReference savePdf(){
    		System.Pagereference currentPage = ApexPages.currentPage();
    		Attachment attach = new Attachment();
    		Blob body = currentPage.getContentAsPDF();
        	attach.Body = body;
        	attach.Name = 'RemitRegister.pdf';
        	attach.ParentId = 'a0319000000ppIf';
        	insert attach;
    		PageReference newPg = new PageReference('/apex/CM_MonthlyRemittance');
			newPg.setRedirect(true);
    		return newPg;
    		}
From what I've seen in similar instances when people have come across this it's generally caused when you try and return the same page which is currently being displayed.  However, even with any number of means of trying to redirect or return a different page this still occurs for me and is getting quite frustrating as this is the only nagging issue which is preventing the larger project from being completed.  Any help would be appreciated.
 
Carter85Carter85
Conversely, I also have another method which executes onload of the page in question and, if I encapsulate my attach method in this, I don't get the error and the attachments all go where they're supposed to be.  However, the attached files are blank of all pertinent data, so if anyone can figure out how to get either of these methods working as intended that would help me greatly.  The second method is:
public pageReference searchPend2(){
    	List<ID> batchList = new List<ID>();
        
        tContractList = new List<ContractWrapper>();
        twcContractList = new List<ContractWrapper>();
		gapContractList = new List<ContractWrapper>();
		mppContractList = new List<ContractWrapper>();
		wsContractList = new List<ContractWrapper>();
		upppcContractList = new List<ContractWrapper>();
		upppContractList = new List<ContractWrapper>();
		uppContractList = new List<ContractWrapper>();
		sealContractList = new List<ContractWrapper>();
		pdrContractList = new List<ContractWrapper>();
		mpppcContractList = new List<ContractWrapper>();
		mpppContractList = new List<ContractWrapper>();
		keyContractList = new List<ContractWrapper>();
		keyplContractList = new List<ContractWrapper>();
		etchContractList = new List<ContractWrapper>();
        	
        tdueAmount = 0.00;
        tcdueAmount = 0.00;
        gapdueAmount = 0.00;
        mppdueAmount = 0.00;
        wdueAmount = 0.00;
        upppcdueAmount = 0.00;
        upppdueAmount = 0.00;
        uppdueAmount = 0.00;
        sealdueAmount = 0.00;
        pdrdueAmount = 0.00;
        mpppcdueAmount = 0.00;
        mpppdueAmount = 0.00;
        krepldueAmount = 0.00;
        kplusdueAmount = 0.00;
        etchdueAmount = 0.00;
        	
        	for(MG_Contract_Holder__c c : [SELECT ID,
        										  Batch__r.ID,
        										  Vin_Number__c,
        										  Contract_Status__c,
        										  Product_Group__c,
        										  Name,
        										  Policy_Type__c,
        										  Contract_Number__c,
        										  Benefits_Options__c,
        										  Dealer_Cost__c,
        										  Batch__r.Batch_Remit_Balance__c,
        										  Term__c, Class__c,
        										  Effective_Date__c
        										  FROM MG_Contract_Holder__c
        										  WHERE ((Contract_Status__c = 'Submitted - Pending Payment')
        										  AND (Dealer_Name__c =:account)
        										  AND (Remit_Amount__c = null OR Remit_Amount__c < 0))
        										  AND Remit_Sent_Date__c = TODAY
        										  ORDER BY Name]){
        		
        		if(c.Product_Group__c == 'T-W'){
					tdueAmount += c.Dealer_Cost__c;	
					tContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					}
				if(c.Product_Group__c == 'T-W - Cosmetic'){
					tcdueAmount	+= c.Dealer_Cost__c;
					twcContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					}
				if(c.Product_Group__c == 'GAP'){
					gapdueAmount += c.Dealer_Cost__c;
					gapContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					}
				if(c.Product_Group__c == 'MPP'){
					mppdueAmount += c.Dealer_Cost__c;
					mppContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					}
				if(c.Product_Group__c == 'Windshield'){
					wdueAmount += c.Dealer_Cost__c;
					wsContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					}
				if(c.Product_Group__c == 'UPP Plus - Cosmetic'){
					upppcdueAmount += c.Dealer_Cost__c;
					upppcContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					}
				if(c.Product_Group__c == 'UPP Plus'){
					upppdueAmount += c.Dealer_Cost__c;
					upppContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					}
				if(c.Product_Group__c == 'UPP'){
					uppdueAmount += c.Dealer_Cost__c;
					uppContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					}
				if(c.Product_Group__c == 'Sealants'){
					sealdueAmount += c.Dealer_Cost__c;
					sealContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					}
				if(c.Product_Group__c == 'PDR'){
					pdrdueAmount += c.Dealer_Cost__c;
					pdrContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					}
				if(c.Product_Group__c == 'MPP Plus - Cosmetic'){
					mpppcdueAmount += c.Dealer_Cost__c;
					mpppcContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					batchList.add(c.Batch__r.ID);
					}
				if(c.Product_Group__c == 'MPP Plus'){
					mpppdueAmount += c.Dealer_Cost__c;
					mpppContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					}
				if(c.Product_Group__c == 'Key Replacement'){
					krepldueAmount += c.Dealer_Cost__c;
					keyContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					}
				if(c.Product_Group__c == 'Key Plus'){
					kplusdueAmount += c.Dealer_Cost__c;
					keyplContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					}
				if(c.Product_Group__c == 'Etch'){
					etchdueAmount += c.Dealer_Cost__c;
					etchContractList.add(new contractWrapper(c));
					batchList.add(c.Batch__r.ID);
					}	
					cost = [SELECT SUM(Dealer_Cost__c)costTotals 
							FROM MG_Contract_Holder__c
							WHERE ((Contract_Status__c = 'Submitted - Pending Payment'))
							AND (Dealer_Name__c =:account)
							AND Remit_Sent_Date__c = TODAY
							];
            		String str = '' + cost[0].get('costTotals');
                	total = double.valueOf(str);
				}
			   	        	   	        	   	        	   	        	        	   	        	   	        	   	        	   				        	   	        	   	
        	if(tContractList.size() > 0){ 
        		TIRE = true;
        		}
        	if(twcContractList.size() > 0){
        		TIRECOS = true;
        		}
        	if(gapContractList.size() > 0){
        		GAP = true;
        		} 
        	if(mppContractList.size() > 0){
        		MPP = true;
        		} 
        	if(wsContractList.size() > 0){
        		WINDSHIELD = true;
        		}
        	if(upppcContractList.size() > 0){
        		UPPPCOS = true;
        		} 
        	if(upppContractList.size() > 0){
        		UPPP = true;
        		} 
        	if(uppContractList.size() > 0){
        		UPP = true;
        		} 
        	if(sealContractList.size() > 0){
        		SEAL = true;
        		} 
        	if(pdrContractList.size() > 0){
        		PDR = true;
        		} 
        	if(mpppcContractList.size() > 0){
        		MPPPCOS = true;
        		}
        	if(mpppContractList.size() > 0){
        		MPPP = true;
        		}
        	if(keyContractList.size() > 0){
        		KEY = true;
        		}
        	if(keyplContractList.size() > 0){
        		KEYPLS = true;
        		}
        	if(etchContractList.size() > 0){
        		ETCH = true;
        		}
    			
    			Set<ID> myset = new Set<ID>();
				List<ID> result = new List<ID>();
				myset.addAll(batchList);
				result.addAll(myset);
    			
    			System.Pagereference currentPage = ApexPages.currentPage();
    			Attachment attach = new Attachment();
    			for(integer i = 0; i < result.size(); i++){
    				
    				Blob body = currentPage.getContentAsPDF();
        			attach.Body = body;
        			attach.Name = 'RemitRegister.pdf';
        			attach.ParentId = 'a0319000000ppIf';
        			insert attach;
    				
    				Task tk = new Task();
            		tk.Priority = 'Normal';
            		tk.Status = 'Not Started';
           			tk.WhatID = result[i];
           			tk.OwnerId = remittance;
            		tk.Subject = 'Note: Remittance being sent in via mail, look for Check matching Batch Amount from Dealer';
            		insert tk;
        			}
    			
    			return null;
    			}
Though I fear the problem here lies in all the lists I need to maintain and pull data from to populate the various tables in the intended PDF, because they're not being preserved when the page is finished completing the action.
As I said, any suggestion/solution would be great so I can hopefully finish this.