0

I want show a alert message when list size is greater than zero. I received like this error.enter image description here

This is my controller:

public class OpportunityVFPageExtension {

    public Opportunity getOpportunity {get;set;}
    public List<Opportunity> oldOpportunity {get;set;}

    public OpportunityVFPageExtension(ApexPages.StandardController sc){
        getOpportunity = (Opportunity)sc.getRecord();
    }

    Public Void Save(){
        oldOpportunity = [SELECT Id,Name FROM Opportunity WHERE Name =: getOpportunity.Name]; 
        insert getOpportunity;
    }
}

This is my VF page:

<apex:page standardController="Opportunity" extensions="OpportunityVFPageExtension">
    <apex:sectionHeader title="New Opportunity"/>
    <script>
    function validateName(){
           if('{oldOpportunity.size}' > 0){
            alert('This is duplicate record. Do you want to submit?');
            }
        }
    </script> 
    <apex:form >
            <!--    <apex:actionFunction name="callSave" action="{!Save}"/>-->
        <apex:pageBlock title="Opportunity Edit" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!Save}" onclick="validateName();" />
                <apex:commandButton value="Cancel" action="{!Cancel}" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Opportunity Information"  columns="1" collapsible="true" >
                <apex:inputField value="{!Opportunity.Name}" />
                <apex:inputField value="{!Opportunity.CloseDate}" />
                <apex:inputField value="{!Opportunity.StageName}" />
                <apex:inputField value="{!Opportunity.Last_Name__c}" />
                <apex:inputField value="{!Opportunity.Email__c}" />
                <apex:inputField value="{!Opportunity.Phone__c}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Thanks in advance.

  • 1
    In the save method you are trying to create an already created record. You can do update operation on it but not insert. You should explain what you want to do in the save method? – abdn Aug 28 '16 at 11:54
  • No i want to check duplicate record.. User wish to recreate the same record means i want to show one popup alert after that allow to save – Jana Shree Aug 28 '16 at 11:57
  • Instead of alert show a confirm message box and if user select yes then in your controller insert the record. – Tushar Sharma Aug 28 '16 at 12:56
  • To find duplicate, remove the insert oldOpportunity line from class and, in the VF page, call the JS function on load like this: window.onload= validateName() – abdn Aug 28 '16 at 13:08
0

To find duplicate and insert new record only if duplicate does not exist modify your save method.

Public pagereference Save(){
  oldOpportunity = [SELECT Id,Name FROM Opportunity WHERE Name =: getOpportunity.Name]; 
  if(oldOpportunity.isEmpty()){
        insert getOpportunity;
  }
  return null
}

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.