• Susan Johnson
  • NEWBIE
  • 5 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
hello, I have a VF page "Renew" button similar to standard clone button. It allows the user to perform what I call "quick edits" for a few key fields. When the opportunity  is opened by using Renawal button , the opportunity fields are displayed  in edit mode. If the users changes or adds to these fields, then clicks save, it does save the record as I want it to. However, instead of refreshing to display the VF page, the newly created (renewed) opportunity in View mode,  it refreshes to display the page layout of the Renewed (first) oportunity in Edit Mode. 

How can I see the new Renewed Opportunity in View Mode? Here  is my code. Please help. I appriciate any feedback.


public class OpportunityRenewalController
{

    private ApexPages.StandardController controller {get; set;}
    private Opportunity opp {get;set;}
    public ID newRecordId {get;set;}

    public OpportunityRenewalController(ApexPages.StandardController controller)
        {
            this.controller = controller;
            opp = (Opportunity)controller.getRecord();
        }
    
    public PageReference RenewalWithItems()
    {
        Savepoint sp = Database.setSavepoint();
        Opportunity newOpp;
         
        try
        {
            opp = [SELECT Id, AccountId, Account.Name, Name, Contract_Type__c, StageName, MarketRegion__c, Service_Offering__c, Market__c, Forecast_Status__c,Sub_Practice__c,CurrencyIsoCode, Billing_Type__c, Service_Line__c, CampaignId, Campaign.Name, Amount, Notice_Period__c, Delivery_Location__c, LeadSource, NextStep, Description, Insurance_Coverage__c, Unusual_Insurance_Requirements__c, Warranty_Clause__c, Unusual_Damages__c, Risk_Do_Dont__c, Unusual_Risk__c, External_Counsel__c, Payment_Terms__c, Gross_Margin__c, Authorized_Approver__c, Risk_Assessment_Comment__c, Contract_Start_Date__c, Contract_End_Date__c, Contract_Duration__c, CIBER_Opportunity_ID__c, Nearshore__c, Offshore__c, Onshore__c,Delivery_Location_Type_Poland_GSC__c,Solution_Set__c FROM Opportunity
                WHERE Id = :opp.Id];
            if (opp.StageName != 'Pre-Qualified')
            {
                 
                opp.StageName = 'Qualified';
                opp.Forecast_Status__c = 'Probable';                
                opp.Booking_Type__c = 'Existing Client - Renewals/Extension';                
                if ((opp.Contract_Start_Date__c != Null) &&  (opp.Contract_End_Date__c != Null))
                                {
                    opp.Contract_Start_Date__c = opp.Contract_End_Date__c.addDays(1);
                    integer iDuration = integer.valueOf(opp.Contract_Duration__c);
                    opp.Contract_End_Date__c = opp.Contract_Start_Date__c.addMonths(iDuration)-1;
                    opp.CloseDate = opp.Contract_Start_Date__c;
                    opp.Number_of_Positions__c =opp.Number_of_Positions__c=5;
                    opp.Probability = 75;                   
                                       
                }
                       
           
                else
                {
                    opp.CloseDate = Date.today().addMonths(1);
                    opp.Contract_Start_Date__c = Null;
                    opp.Contract_End_Date__c = Null;
                    integer iDuration = 0; 
                    opp.Probability = 75;                 
                }
                    
                     
                newOpp = opp.Clone(false);
                insert newOpp;
                     
                newRecordId = newOpp.id;
                     
                List<OpportunityLineItem> items = new List<OpportunityLineItem>();
                for (OpportunityLineItem li : [Select l.Id, l.Quantity, l.Description, l.ServiceDate,l.UnitPrice, l.CurrencyIsoCode, l.PricebookEntryId, l.SortOrder, l.Cost_Price__c From OpportunityLineItem l where OpportunityId = :opp.id])
                {
                OpportunityLineItem newLI = li.clone(false);
                newLI.OpportunityId = newOpp.id;
                items.add(newLI);
                }
                insert items;
                     
                Note myNote = new Note (ParentId = newRecordId, Title = 'This is the renewal of CIBER Opportunity ID ' + opp.CIBER_Opportunity_ID__c);
                insert myNote;
            }
            else
            {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Renewal not possible for opportunities with phase line Pre-Qualified'));
                return null;
            }
        }                            
            catch (Exception e)
            {
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
            }
  
           //return new ApexPages.StandardController(newOpp).edit();
           //PageReference oppPage = new ApexPages.standardcontroller(newOpp).view();
        PageReference oppPage = new ApexPages.StandardController(newOpp).edit();
        //PageReference oppPage = new PageReference('/' + newOpp.id+'/e');
        //oppPage.setRedirect(true);


            //oppPage = new ApexPages.standardcontroller(newOpp).view();
            oppPage.setRedirect(true);
            return oppPage;
    }
}
  • May 27, 2015
  • Like
  • 0