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
NehaKSNehaKS 

Field Validation Failure

Hi all,

 

Heres my scenario

I want to edit the opportunity line items for a particular opportunity using vf page. The opportunity line items are displayed in a pageblock block table.

I have 3 button on the page "Save", "Save & Close" and "Cancel"

"Save" button is saving the data in database.

"Save & Close" button is saving the data in database and closing the window.

"Cancel " button will undo the changes if not saved yet.

 

Only save button is functional-- the data is saved in database and if error in entering the date it show apex message.

But the 'save & close' button-- data is not saved in database nor it gives error if date is not entered properly

 

Here goes my code

Apex Class:-

public class ProductScheduler1
{
      public Opportunity opp{get;set;}   
      public ProductScheduler1(ApexPages.StandardController stdController) 
      {
        opp = [SELECT Amount, Id, Name,CloseDate,StageName,Account.Name,Probability, (SELECT Quantity,  PricebookEntry.Name, Description, UnitPrice,ServiceDate FROM OpportunityLineItems order by PricebookEntry.Name) FROM Opportunity where id =:ApexPages.currentPage().getParameters().get('id')];
      }
      public PageReference save() 
      {
        update opp.opportunitylineitems;
        return null;
      }
      public PageReference cancel() 
      {
        return null;    
      }
}

 

Visualforce Page:-

 

<apex:page standardController="Opportunity" showHeader="false" sidebar="false" extensions="ProductScheduler1" tabStyle="Opportunity">
    <apex:form >
    <apex:pageBlock >
        <apex:pageMessages />
        <apex:pageBlockButtons >
            <apex:commandbutton action="{!save}" id="savebutton1" value="Save"/>
            <apex:commandbutton action="{!save}" id="savebutton" oncomplete="javascript&colon;CloseAndRefresh()" value="Save & Close" />
            <script language="JavaScript" type="text/javascript">
            function CloseAndRefresh()
            {
                window.opener.location.href="/{!$CurrentPage.parameters.id}";
                window.top.close();  
            }
            </script>
 
            <apex:commandbutton action="{!cancel}" id="cancelbutton" value="Cancel" oncomplete="return confirmCancel();" immediate="true"/> 
            <script language="JavaScript" type="text/javascript">
            function confirmCancel()
            {
                var isCancel = confirm("Are you sure you wish to cancel?");
                if (isCancel)
                {
                    window.top.close();  
                    return true;
                }
                return false;
            }
            </script>    
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Opportunity Header" collapsible="false">
            <apex:outputField value="{!opp.Name}"/>                    
        </apex:pageBlockSection>
        <apex:pageBlocksection >
            <apex:pageBlock title="Opportunity Line Items" mode="edit" id="pb">
                <apex:pageBlockTable value="{!opp.OpportunityLineItems}" var="oppitems" align="center" id="main">
                    <apex:column value="{!oppitems.PricebookEntry.Name}" /> 
                    <apex:column headerValue="Line Description">
                         <apex:inputField value="{!oppitems.Description}"/>
                    </apex:column>
                    <apex:column headerValue="Date" >
                        <apex:inputField value="{!oppitems.ServiceDate}"/>
                    </apex:column>    
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>

 

Tejpal KumawatTejpal Kumawat

Hi, this is working corretly... Test it one more time...

NehaKSNehaKS

 

When i give value '0' in date field and click 'save and close' button...it doesnt give me any error message.(ie apex message)

And also if all the information entered is correct it doesnt save and close the window.

 

Same is the case in cancel button-- Window is not getting closed

 

 

 

NehaKSNehaKS

Cancel is working fine

 

But save and close--

Doesnt give apex message if wrong date entered ..its directly closing the window

Tejpal KumawatTejpal Kumawat

Hi Neha Use this one :--

 

<apex:page standardController="Opportunity" showHeader="false" sidebar="false" extensions="ProductScheduler1" tabStyle="Opportunity">
<apex:form >
<apex:pageBlock >
<apex:pageMessages id="msg" />
<apex:pageBlockButtons >
<apex:commandbutton action="{!save}" id="savebutton1" value="Save" reRender="msg"/>
<apex:commandbutton action="{!save}" id="savebutton" oncomplete="return CloseAndRefresh();" value="Save & Close" reRender="msg"/>
<script language="JavaScript" type="text/javascript">
function CloseAndRefresh(){
window.opener.location.href="/{!$CurrentPage.parameters.id}";
window.top.close();
}
</script>

<apex:commandbutton action="{!cancel}" id="cancelbutton" value="Cancel" onclick="return confirmCancel();" immediate="true"/>
<script language="JavaScript" type="text/javascript">
function confirmCancel()
{
var isCancel = confirm("Are you sure you wish to cancel?");
if (isCancel)
{
window.top.close();
return true;
}
return false;
}
</script>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Opportunity Header" collapsible="false">
<apex:outputField value="{!opp.Name}"/>
</apex:pageBlockSection>
<apex:pageBlocksection >
<apex:pageBlock title="Opportunity Line Items" mode="edit" id="pb">
<apex:pageBlockTable value="{!opp.OpportunityLineItems}" var="oppitems" align="center" id="main">
<apex:column value="{!oppitems.PricebookEntry.Name}" />
<apex:column headerValue="Line Description">
<apex:inputField value="{!oppitems.Description}"/>
</apex:column>
<apex:column headerValue="Date" >
<apex:inputField value="{!oppitems.ServiceDate}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

-------------------------------------------------

 

 

 

public class ProductScheduler1
{
public Opportunity opp{get;set;}
public ProductScheduler1(ApexPages.StandardController stdController)
{
opp = [SELECT Amount, Id, Name,CloseDate,StageName,Account.Name,Probability, (SELECT Quantity, PricebookEntry.Name, Description, UnitPrice,ServiceDate FROM OpportunityLineItems order by PricebookEntry.Name) FROM Opportunity where id =:ApexPages.currentPage().getParameters().get('id')];
}
public PageReference save()
{
update opp.opportunitylineitems;
return null;
}
public PageReference cancel()
{
return null;
}
}

 

 

 

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

 

Thanks 

NehaKSNehaKS

Thanks a lot Tejpal

NehaKSNehaKS

Hi Tejpal,

 

The save and close logic works only if developer mode is on. If i disable the developer mode it doesnot work the same.

Kindly address this issue.

 

Thanks in advance