• amit wagaskar
  • NEWBIE
  • 30 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 5
    Replies
my comtroller:
public class myWeb2LeadExtensionIndia 
{
     private final Lead weblead;
    private ID Leadid;
   
    public myWeb2LeadExtensionIndia(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 {
           
           if(myfile.name==null)
           {
                PageReference pg = apexpages.Currentpage();
                apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.info,'Please Upload An Attachment ');
                apexpages.addmessage(msg);
                return null;
           }
            weblead.LeadSource='Job Application 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;
     }
    

}
//my controller

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;
     }
        
//apex trigger
trigger Validate_attachment on Lead (before insert,before update) {
    
    set<id> ldList=new set<id>();
    
    list<lead>validatelead=[select id,name,leadsource,email from lead where id in:trigger.new];
    for(lead newlead:trigger.new)
    {
    
    List<sObject> objList = new List<sObject>();
    List<lead> leadList=[select id,FirstName,LastName,LeadSource,Email,Email_Sent__c from lead where id IN:trigger.new];
   if((newlead.LeadSource=='Job Application India' || newlead.LeadSource=='Job Application Non-India')&& newlead.attachments.isempty()) 
       
   {
       system.debug('******');
      newlead.adderror('please upload the attchament');
      ldList.add(newlead.id);
       
   }
}
    //list <lead> validatelead = new list<lead>();
    validatelead=[select id,name,leadsource,email,(select id,name from attachments) from lead where id in:ldList];
    
    for(lead l:validatelead)
    {
         if(l.attachments.size()==null)
         {
             l.adderror('please upload the attchament');
             
         }     
    }
    
}
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>
    global class Job_Application_India_Mail_To_Hr implements Database.Batchable<sobject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC)    
    {
    return Database.getQueryLocator([select id,LeadSource from lead where leadsource ='Job Application India' AND Email_sent__c=false]);
    }
    
    
    OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address ='careers@Cenduit.com'];
    
    global void execute(Database.BatchableContext BC,List<lead>GetLeads)
    
    {
    EmailTemplate et=[Select id from EmailTemplate where name=:'Mail To Hr India']; 
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
    List <id> LeadId = new List<id>();
    List <id> Email_Sent_update = new List<id>();
    List <Lead> Lead_flag_Update = new List<Lead>();
    for(lead GetLeadId:GetLeads)
    { 
    LeadId.add(GetLeadId.id); 
    } 
    
    List<Attachment> getattachment=[select id,parentid,name,body from Attachment where parentid in :LeadId];
    
    
    //send mail to Hr for Non India
    try{
    for(Attachment SendMailOnLead:getattachment)
    {
    Email_Sent_update.add(SendMailOnLead.ParentId); 
    Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
    String[] sendingTo = new String[]{'wagaskar.amit@gmail.com'};
    
    singleMail.setTargetObjectId(SendMailOnLead.ParentId);
    
    singleMail.setTemplateId(et.Id);
    singleMail.setToAddresses(sendingTo);
    singleMail.setTreatTargetObjectAsRecipient(false); 
    if (owea.size()>0)
    {
    singleMail.setOrgWideEmailAddressId(owea.get(0).Id);
    }
    
    
    
    Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
    efa.setFilename(SendMailOnLead.Name);
    efa.setbody(SendMailOnLead.Body);
    fileAttachments.add(efa);
    singleMail.setFileAttachments(fileAttachments);
    fileAttachments.clear();
    emails.add(singleMail);
    
    }
    Messaging.sendEmail(emails);
    fileAttachments.clear();
    List <Lead> Update_flag = [select id,Email_Sent__c from lead where id in : Email_Sent_update ];
    for(Lead Email_sent_flag_update : Update_flag)
    {
    system.debug('********');
    Email_sent_flag_update.Email_Sent__c=True;
    Lead_flag_Update.add(Email_sent_flag_update);
    }
    update Lead_flag_Update;
    }
    catch(Exception ex)
    {
    System.debug(ex.getMessage());
    }
    }
    global void finish(Database.BatchableContext BC)
    
    {
    system.debug('batch completed successfully');
    
    }
    }

//here i am not able to update field as well as not able to send an email
HI All,
 I am new to salesforce and wants to know how to write a test class for below apex controller class
//controller class
public class myWeb2LeadExtension {

    private final Lead weblead;
    private ID Leadid;
   
    public myWeb2LeadExtension(ApexPages.StandardController
                                stdController) {
       this.weblead = (Lead)stdController.getRecord();
       this.Leadid    = stdController.getId();
                                     
    }
    
    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;
     }
    
    
    
}
thanks in advance,
Amit wagaskar
Hi all,
I'm trying to send an email which contains some merge field as well as attachment.but the thing is that when i'm sending an email by using apex trigger it will only sending the email with merge field and not the attachment.also when im doing the same by giving static value for the Setname and setbody method at that time i'm able to send email with attachment.wants to know what i'm missing here.

here is the code which i'm using to achieve functionality  mention above. 
//my trigger
trigger LeadCreate on Lead (after insert) {
    
    if(trigger.isafter)
    {
        if(trigger.isinsert)
        {
            sendMailOnLead.sendmailtoHR(trigger.new);
        }
        
    }

}
//helper class for the trigger
public class SendMailOnLead {
    
    
    public static list<lead> sendmailtoHR(list<lead>leads)
    {
        EmailTemplate et=[Select id from EmailTemplate where name=:'LeadDetails'];    
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        
        for(Lead l:leads)
        {
             Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
             String[] sendingTo = new String[]{'wagaskar.amit@gmail.com'};
             
             singleMail.setTargetObjectId(l.Id);
             singleMail.setTemplateId(et.Id);
             singleMail.setToAddresses(sendingTo);
            
            list<attachment>getattachment=new list<Attachment>();
             getattachment=[select id,name,body from Attachment where parentid=:l.id];
            for (Attachment a: getattachment)
            {
              Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
              efa.setFilename(a.Name);
              efa.setbody(a.Body);
              fileAttachments.add(efa);
            }
            //by using below code i'm able to send an email with attachment
            /*
             Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
              efa.setFilename( 'myAttachmentName.txt');
              efa.setbody(Blob.valueOf('Unit Test Attachment Body'));
              fileAttachments.add(efa);

            */
            singleMail.setFileAttachments(fileAttachments);
            system.debug('Attachment Added successfully');
            emails.add(singleMail);
            
            
        }
        Messaging.sendEmail(emails);
        system.debug('class run successfully');
        return leads;
    }

}
thanks,
Amit
 
"message":"Unable to create/update fields: ParentId__c. Please check the security settings of this field and verify that it is read/write for your profile or permission set.","errorCode":"INVALID_FIELD_FOR_INSERT_UPDATE","fields":["ParentId__c"]}]
 
//apex trigger
trigger Validate_attachment on Lead (before insert,before update) {
    
    set<id> ldList=new set<id>();
    
    list<lead>validatelead=[select id,name,leadsource,email from lead where id in:trigger.new];
    for(lead newlead:trigger.new)
    {
    
    List<sObject> objList = new List<sObject>();
    List<lead> leadList=[select id,FirstName,LastName,LeadSource,Email,Email_Sent__c from lead where id IN:trigger.new];
   if((newlead.LeadSource=='Job Application India' || newlead.LeadSource=='Job Application Non-India')&& newlead.attachments.isempty()) 
       
   {
       system.debug('******');
      newlead.adderror('please upload the attchament');
      ldList.add(newlead.id);
       
   }
}
    //list <lead> validatelead = new list<lead>();
    validatelead=[select id,name,leadsource,email,(select id,name from attachments) from lead where id in:ldList];
    
    for(lead l:validatelead)
    {
         if(l.attachments.size()==null)
         {
             l.adderror('please upload the attchament');
             
         }     
    }
    
}
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>
Hi all,
I'm trying to send an email which contains some merge field as well as attachment.but the thing is that when i'm sending an email by using apex trigger it will only sending the email with merge field and not the attachment.also when im doing the same by giving static value for the Setname and setbody method at that time i'm able to send email with attachment.wants to know what i'm missing here.

here is the code which i'm using to achieve functionality  mention above. 
//my trigger
trigger LeadCreate on Lead (after insert) {
    
    if(trigger.isafter)
    {
        if(trigger.isinsert)
        {
            sendMailOnLead.sendmailtoHR(trigger.new);
        }
        
    }

}
//helper class for the trigger
public class SendMailOnLead {
    
    
    public static list<lead> sendmailtoHR(list<lead>leads)
    {
        EmailTemplate et=[Select id from EmailTemplate where name=:'LeadDetails'];    
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        
        for(Lead l:leads)
        {
             Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
             String[] sendingTo = new String[]{'wagaskar.amit@gmail.com'};
             
             singleMail.setTargetObjectId(l.Id);
             singleMail.setTemplateId(et.Id);
             singleMail.setToAddresses(sendingTo);
            
            list<attachment>getattachment=new list<Attachment>();
             getattachment=[select id,name,body from Attachment where parentid=:l.id];
            for (Attachment a: getattachment)
            {
              Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
              efa.setFilename(a.Name);
              efa.setbody(a.Body);
              fileAttachments.add(efa);
            }
            //by using below code i'm able to send an email with attachment
            /*
             Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
              efa.setFilename( 'myAttachmentName.txt');
              efa.setbody(Blob.valueOf('Unit Test Attachment Body'));
              fileAttachments.add(efa);

            */
            singleMail.setFileAttachments(fileAttachments);
            system.debug('Attachment Added successfully');
            emails.add(singleMail);
            
            
        }
        Messaging.sendEmail(emails);
        system.debug('class run successfully');
        return leads;
    }

}
thanks,
Amit