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
amit wagaskaramit wagaskar 

want to throw an error on vf page

please help me with this code so that i can able to display an error msg on vf page if user is  submitting for without the attachment

controller:
public class myWeb2LeadExtension {

    private final Lead weblead;
    private ID Leadid;
   
    public myWeb2LeadExtension(ApexPages.StandardController
                                stdController) {
       this.weblead = (Lead)stdController.getRecord();
       this.Leadid  = stdController.getId();
                                     
    }
    
    @testvisible transient private Attachment myfile;
    public Attachment getmyfile() {
    myfile = new Attachment();
    return myfile;
}

   
     public PageReference saveLead() {
       try {
        weblead.LeadSource='Job Application Non-India';
        weblead.Company='added from Job Application Page';
        weblead.Status='open';
        insert(weblead);
        Attachment a = new Attachment(parentid=weblead.id, Name = myfile.name , Body = myfile.Body );
        insert a;     
       }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
           return null;
       }
         
       PageReference p = Page.ThankYou;
       p.setRedirect(true);
       return p;
     }
        


VF page`:
 <tr><th><apex:outputLabel value="First Name"></apex:outputLabel></th></tr>
            <td><apex:inputField value="{!Lead.FirstName}" style="width:400px; height:30px;border-radius: 5px;" required="true"></apex:inputField></td>
            
            <tr><th><apex:outputLabel value="Last Name"></apex:outputLabel></th></tr>
            <td><apex:inputField value="{!Lead.LastName}" style="width:400px; height:30px;border-radius: 5px;" required="true"></apex:inputField></td>
            
            <tr><th><apex:outputLabel value="Email"></apex:outputLabel></th></tr>
            <td><apex:inputField value="{!Lead.Email}" style="width:400px; height:30px;border-radius: 5px;" required="true"></apex:inputField></td>
            
            <tr><th><apex:outputLabel value="Position Applying For"></apex:outputLabel></th></tr>
            <td><apex:inputField value="{!Lead.position__c}" style="width:400px; height:30px;border-radius: 5px;" required="true"></apex:inputField></td>
            
            <tr><th><apex:outputLabel value="Location"></apex:outputLabel></th></tr>
            <td><apex:inputField value="{!Lead.Location__c}" style="width:400px; height:30px;border-radius: 5px;" required="true"></apex:inputField></td>
            
            <tr><th><apex:outputLabel value="Summarize your education and experience."></apex:outputLabel></th></tr>
            <td><apex:inputField value="{!Lead.Expirience__c}" style="width:400px; height:30px;border-radius: 5px;" required="true"></apex:inputField></td>
            
            <tr><th><apex:outputLabel value="What type of opportunity are you looking for?"></apex:outputLabel></th></tr>
            <td><apex:inputField value="{!Lead.Career_Interest__c}" style="width:400px; height:30px;border-radius: 5px;" required="true"></apex:inputField></td>
            
            <tr><th><apex:outputLabel value="What is your current salary?"></apex:outputLabel></th></tr>
            <td><apex:inputField value="{!Lead.Current_Salary__c}" style="width:400px; height:30px;border-radius: 5px;" required="true"></apex:inputField></td>
            
            <tr><th><apex:outputLabel value="What is your desired salary?"></apex:outputLabel></th></tr>
            <td><apex:inputField value="{!Lead.Desire_Salary__c}" style="width:400px; height:30px; border-radius: 5px;" required="true"></apex:inputField></td>
             
                    
            <tr><th><apex:outputLabel value="Resume" ></apex:outputLabel></th>  
            <td>
                <div class="file-input" style="background:#fff; margin-left:-355px;" name="username" >
                   <apex:inputfile value="{!myfile.body}" filename="{!myfile.Name}" required="true" id="username"  onclick="return(validate());"/>   
                </div>
                
              </td>  
              </tr> 
              
             <tr><th  class="padd"></th></tr> 
            <tr><th><apex:outputLabel value="Email OPT-IN"  ></apex:outputLabel>   
            <td><apex:inputField value="{!Lead.Email_OPT_IN__c}" style="margin-left:-320px;"></apex:inputField></td></th></tr>
              
            </div>   
                
                <apex:pageBlockButtons location="bottom" style="background:#fff">
                    <div class="save-button" style="float: left;width: 100%;margin-top: 45px;margin-left: 50px; margin-bottom: 20px;">                      
                    
                        <apex:commandButton styleclass="slds-button slds-button_brand" value="Save" action="{!saveLead}"/>               
                        
                    </div>
Best Answer chosen by amit wagaskar
Selvaganesh IlangoSelvaganesh Ilango
Hi Amit,

You can make an SOQL query to see if there are any attachments in the object "Attachment" and this code has to be written on the saveLead method.
List<Attachment> attachFile = [Select Id, ParentId from Attachment where ParentId =: weblead.id];
if(attachFile.size() == 0){
   ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Attachment Mandatory'));
}


Thanks
Selva

All Answers

Selvaganesh IlangoSelvaganesh Ilango
Hi Amit,

You can make an SOQL query to see if there are any attachments in the object "Attachment" and this code has to be written on the saveLead method.
List<Attachment> attachFile = [Select Id, ParentId from Attachment where ParentId =: weblead.id];
if(attachFile.size() == 0){
   ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Attachment Mandatory'));
}


Thanks
Selva

This was selected as the best answer
amit wagaskaramit wagaskar
Hi Selvaganesh,
can you please specify where  im suppose to put that code in controller clas.
 
amit wagaskaramit wagaskar
Hi selva,
i tried putting code suggested by you in controller,but its still not working

I modified my code like below:
public class testattachmentapex {

    private final Lead weblead;
    private ID Leadid;
   
    public testattachmentapex(ApexPages.StandardController
                                stdController) {
       this.weblead = (Lead)stdController.getRecord();
       this.Leadid  = stdController.getId();
                                     
    }
    
    @testvisible transient private Attachment myfile;
    public Attachment getmyfile() {
    myfile = new Attachment();
    return myfile;
}

   
     public PageReference saveLead() {
       try {
        weblead.LeadSource='Job Application Non-India';
        weblead.Company='added from Job Application Page';
        weblead.Status='open';
        insert(weblead);
        Attachment a = new Attachment(parentid=weblead.id, Name = myfile.name , Body = myfile.Body );
        insert a ; 
           
           
        List<Attachment> attachFile = [Select Id, ParentId from Attachment where ParentId =: weblead.id];
        if(attachFile.size() == 0){
               
            }
       }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
           return null;
       }
         
       PageReference p = Page.ThankYou;
       p.setRedirect(true);
       return p;
     }
    
    
    
}