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
Sabah FarhanaSabah Farhana 

Passing Id's from one page to another

Hi All ,

I have a custom object Opportunity_Attachment__c which has master-detail with Opportunity(master).

I have a button on the  opportunity attachment related list on the opportunity page,which creates an attachment .

Now i tried to modfy the code where i want to redirect after creating the attachment to another page where i add one more attachment

The redirect is working ,but the insert on the second page is giving an error as Iam not able to pass all the id's in the first page.
Please help me modify the code to pass the id's

when iam at the first page

browser has:
https://c.cs18.visual.force.com/apex/OpportunityAttachment1?retURL=%2F00611000003XbMi&CF00Nb0000003eDo6_lkid=00611000003XbMi&CF00Nb0000003eDo6=B%C4%81+Restaurant+%26+Lounge+-+2015+-+100.00+AED&sfdc.override=1&scontrolCaching=1

At the second page its just:
https://c.cs18.visual.force.com/apex/OpportunityAttachment2

So how do i pass all the parameters from first page to the second so that second page has:

https://c.cs18.visual.force.com/apex/OpportunityAttachment2?retURL=%2F00611000003XbMi&CF00Nb0000003eDo6_lkid=00611000003XbMi&CF00Nb0000003eDo6=B%C4%81+Restaurant+%26+Lounge+-+2015+-+100.00+AED&sfdc.override=1&scontrolCaching=1

Please find the controller codes below.

On the second page iam getting an insert error as id's are not passed.

Please help.
************************

public with sharing class OpportunityAttachmentController1{
  
    public Opportunity_Attachment__c objOppAtt {get; set;}
    public OpportunityAttachmentController1(ApexPages.StandardController controller) {
        attach = new Attachment();
        objOppAtt = (Opportunity_Attachment__c)controller.getRecord();
    }

    public Attachment attach {get;set;}
 
    //When user clicks upload button on Visualforce Page, perform upload/insert
    public ApexPages.Pagereference save(){
        if(attach.body==null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select a file to upload'));
            return null;
        }
        insert objOppAtt;
      
        attach.ParentId = objOppAtt.id;
        insert attach;
      
        objOppAtt.URL__c = URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+attach.id;
        objOppAtt.name = attach.Name;
        update objOppAtt;
        //return new PageReference('/'+objOppAtt.Opportunity__c); 
    PageReference congratsPage = Page.OpportunityAttachment2;


//Opportunityattachment2 controller is OpportunityAttachmentController2,its just exactle the same page to add one more attachment
//how to pass all id's here to OpportunityAttachmentController2

  congratsPage.setRedirect(true);

  return congratsPage;






    }
}

*************************************

public with sharing class OpportunityAttachmentController2{
  
    public Opportunity_Attachment__c objOppAtt {get; set;}
    public OpportunityAttachmentController2(ApexPages.StandardController controller) {
        attach = new Attachment();
        objOppAtt = (Opportunity_Attachment__c)controller.getRecord();
    }

    public Attachment attach {get;set;}
 
    //When user clicks upload button on Visualforce Page, perform upload/insert
    public ApexPages.Pagereference save(){
        if(attach.body==null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select a file to upload'));
            return null;
        }
        insert objOppAtt;
      
        attach.ParentId = objOppAtt.id;
        insert attach;
      
        objOppAtt.URL__c = URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+attach.id;
        objOppAtt.name = attach.Name;
        update objOppAtt;
        return new PageReference('/'+objOppAtt.Opportunity__c); 
  




    }
}


******************************
Best Answer chosen by Sabah Farhana
CheyneCheyne
After you create the page reference, you can loop through all of the current page's parameters and add them to the new page's parameters. 

PageReference congratsPage = Page.OpportunityAttachment2;
for (String paramName : ApexPages.currentPage().getParameters().keySet()) {
    congratsPage.getParameters().put(paramName, ApexPages.currentPage().getParameters().get(paramName));
}
congratsPage.setRedirect(true);
return congratsPage;

All Answers

CheyneCheyne
After you create the page reference, you can loop through all of the current page's parameters and add them to the new page's parameters. 

PageReference congratsPage = Page.OpportunityAttachment2;
for (String paramName : ApexPages.currentPage().getParameters().keySet()) {
    congratsPage.getParameters().put(paramName, ApexPages.currentPage().getParameters().get(paramName));
}
congratsPage.setRedirect(true);
return congratsPage;
This was selected as the best answer
Sabah FarhanaSabah Farhana
Works like a charm