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 

Exception when saving opportunity line item

<apex:page standardController="Opportunity" showHeader="false" sidebar="false" extensions="testsavenclose" tabStyle="Opportunity">

<apex:form id="form">
<apex:pageBlock id="results" >
<apex:pageMessages id="msg" />

<apex:pageBlockButtons >
<apex:commandbutton action="{!save}" id="savebutton1" value="Save"/>


<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" id="pbs1">
<apex:outputField value="{!opp.Name}" id="abc"/>
</apex:pageBlockSection>
<apex:pageBlocksection id="pbs2">
<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:column headerValue="Quantity">
                                <apex:inputField value="{!oppitems.Quantity}" rendered="{!NOT(oppitems.HasSchedule)}"/>
                                <apex:outputField value="{!oppitems.Quantity}" rendered="{!oppitems.HasSchedule}"/>
                            </apex:column> 

</apex:pageBlockTable>
</apex:pageBlock>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

Hi,

 

Please find the below code:

 

 Apex Code:

 

public class testsavenclose
{
public Opportunity opp{get;set;}
public testsavenclose(ApexPages.StandardController stdController)
{
 opp = [SELECT Amount, Id, Name,CloseDate,StageName,Account.Name,Probability, (SELECT Quantity,  PricebookEntry.Name, Description, UnitPrice,ServiceDate,HasSchedule,HasRevenueSchedule 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;
}
}

 Steps to get exception:

 

Edit the Quantity field on opportunity line item page block table. Give value "0.00" in quantity field.Click on save.

It throws following exception.

 

System.DmlException: Update failed. First exception on row 0 with id 00k90000005VlrnAAC; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: Quantity (quantity must be nonzero): [Quantity]

 

how do i handle this exception.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka

Hi Neha,

 

Update your try catch block with this

 

try 
{
    //your code here
}
catch (System.DmlException ex)
{
     if(StatusCode.FIELD_INTEGRITY_EXCEPTION == ex.getDmlType(0))
     {
        Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,''+'Quantity must be nonzero'));  
     }
}

 Make sure that you have added a <apex:pageMessages /> tag in rerendering block by the button.

 

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.

 

All Answers

NehaKSNehaKS

Can i give some error message saying "Quantity must be non zero".

JHayes SDJHayes SD

Exception handling in Apex: http://wiki.developerforce.com/page/An_Introduction_to_Exception_Handling

Also, what happens if you update opp in the controller prior to updating opp.opportunitylineitems?  Ex: 

 

public PageReference save()
{ update opp; update opp.opportunitylineitems; return null; }
Chamil MadusankaChamil Madusanka

Hi Neha,

 

Update your try catch block with this

 

try 
{
    //your code here
}
catch (System.DmlException ex)
{
     if(StatusCode.FIELD_INTEGRITY_EXCEPTION == ex.getDmlType(0))
     {
        Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,''+'Quantity must be nonzero'));  
     }
}

 Make sure that you have added a <apex:pageMessages /> tag in rerendering block by the button.

 

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.

 

This was selected as the best answer