0

The following lines of code are updating the record, closing the quote and redirecting back to the record after update is performed. I need to display record success messages on closure. Moreover, if a particular the quote has a closed status already then, it should not re-enter in visual force page and display a custom message.

I have tried to add this behavior in code, but it is not reflecting. Could someone please help?

global class CloseQuote{
    private ApexPages.StandardController controller {get; set;}
    global CameleonCPQ__Quote__c QuoteRecordQueried {get; set;}

    public CloseQuote(ApexPages.StandardController controller) {
        this.controller = controller;
        QuoteRecordQueried = [select Id,
                              Name,
                              CameleonCPQ__Status__c,
                              Quote_Closure_Reason__c,
                              Quote_Closure_Comments__c                                     
                              from CameleonCPQ__Quote__c
                              where id =: controller.getId()];  
        this.QuoteRecordQueried.CameleonCPQ__Status__c = 'Closed';         
    } 

    public PageReference closeQuote() {
        String quote_closure_reason = QuoteRecordQueried.Quote_Closure_Reason__c;
        String quote_closure_comments = QuoteRecordQueried.Quote_Closure_Comments__c;

        Savepoint sp = Database.setSavepoint();
        try{
            if (QuoteRecordQueried.CameleonCPQ__Status__c == 'Closed' && QuoteRecordQueried.Quote_Closure_Reason__c !=''){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Quote closed successfully'));
                update QuoteRecordQueried;
            } 
            else {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'You may not close the quote at this time')); 
            }
        } 
        catch (Exception e){
            Database.rollback(sp);
            ApexPages.addMessages(e);
            //PageReference tempPage = ApexPages.currentPage();
            //tempPage.setRedirect(true);
            //return tempPage; 
            return null;
        }
        return new PageReference('/'+QuoteRecordQueried.id);
    }
}

Visualforce page

    <apex:page standardController="CameleonCPQ__Quote__c" 
    extensions="CloseQuote" showHeader="false" applyHtmlTag="false"
    lightningStylesheets="true">
    <apex:sectionHeader title="Close Quote" subtitle=" 
    {!QuoteRecordQueried.Name}"/>
    <apex:form>
        <apex:pageBlock mode="inlineEdit" title="">
            <apex:outputPanel id="MESSAGES">
            <apex:pageMessages ></apex:pageMessages>
            </apex:outputPanel> 
           <apex:pageblockButtons>
              <apex:commandButton action="{!closeQuote}" value="Close Quote"/>
              <apex:commandButton action="{!Cancel}" value="Cancel"/>
            </apex:pageblockButtons>
            <apex:pageBlockSection columns="2" title="Quote Closure Information" collapsible="false">         
                <apex:inputField value="{!QuoteRecordQueried.Quote_Closure_Reason__c}" required="true"/>
                <apex:inputField value="{!QuoteRecordQueried.Quote_Closure_Comments__c}"/>           
    </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
    </apex:page>
0

VChandra , can you Try This when your saving record , On Visual force page :

<apex:outputPanel id="MESSAGES">
<apex:pageMessages ></apex:pageMessages>
</apex:outputPanel>

in controlller use this after updating/saving your record.

PageReference pg;

pg=new PageReference('/' + redirectToRecordID);

pg.getParameters().put('message','Saved Successfully');

ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Updated 
Successfully'));

 pg.setRedirect(true);

return pg;

and if your record is already closed.. most IMPORTANT you have to reRender the Apex:pageMessages tag, when click on the button that your performing the updating record.

this the case, but your showing the messgae and updating the record, in this case the page message will go off from the screen(reRender ). so update the record first and throw the ApexPages.addmessage and rerender only <Apexpagemessage>tag.

7
  • Hi - thanks for your suggestion. I tried to update the code per your suggestion but I am still not getting any message on update. Could you please add your suggestions and paste the code again? I think I might me making a mistake in the placement of code lines.
    – VChandra
    Mar 19 '19 at 10:41
  • your code placement is wrong.
    – Umesh Beti
    Mar 19 '19 at 11:00
  • why are you returning the page refernce, even if the quote is not updating... insert that the line after update record, and add my code for returning to the particular record id. <<----- this should be in if condition. and in else condition you just need to throw the error that this quote is alredy closed and reRender the page. that's it.. yes you need to remove the try catch block for some time.
    – Umesh Beti
    Mar 19 '19 at 11:04
  • If I don't return the page reference, I am receiving the following error: Missing return statement required return type: System.PageReference
    – VChandra
    Mar 19 '19 at 11:45
  • make it method as void and use my code Vchandra
    – Umesh Beti
    Mar 19 '19 at 11:53
0

Try like below -

<apex:pageMessages id="showmsg"></apex:pageMessages>
<apex:commandButton value="Close Quote" action="{!closeQuote}" rerender="showmsg"/>

In apex class -

ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Error,'Error Message'));
1
  • Hi - thanks for your suggestion. I tried to update the code per your suggestion but I am still not getting any message on update. Could you please add your suggestions and paste the code again? I think I might me making a mistake in the placement of code lines. I am also adding the vf page involved. Thanks for your help.
    – VChandra
    Mar 19 '19 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.