• Simone Capelli
  • NEWBIE
  • 5 Points
  • Member since 2014
  • Solution Architect and Senior Developer
  • WebResults S.r.l.


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
I have Custom Field called Start_Date__c in my Custom Object. I have written Validation Rule for that. my requirement is whenever user creating a record the Start_Date__c shouldnt be past date, for this i have given validation rule: START_DATE__c < Today() 
Validation Working fine,

My requirement is, when user Edits the record in future without editing the start date, at that time validation triggering, it shouldnot fire at that time...
Example: user created record on 1st  nov 2014 and user Edited record on 5th november 2014 without editing the start date, validation rule not allowing user to save record with 01st november 2014. its shouldnot happen like this, could you please help me out.

Thanks in Advance...
So my use case is, when a BatchApex runs for every one hour, it inserts records on QNews custom object.
After Trigger fire every time when a record is inserted on QNews object and  sends an email with a PDF attachment.
I’m getting following error when Batch Apex runs, FATAL_ERROR|System.AsyncException: Future method cannot be called from a future or batch method: SendVFAsAttachment.sendVF(String, String, String, String, Id, String, String, String).

My ApexBatch:
global with sharing class ArticleBatch implements Database.Batchable<string>{
public Iterable<string> start(Database.BatchableContext bc)
    return new ArticleBatchIterable();  }

public void execute(Database.BatchableContext bc, List<string> scope){
    string articleTypeName = scope[0];    
    QNewsUpdateBatch batchClassObj = new QNewsUpdateBatch();
    batchClassObj.insertQnewsData(articleTypeName);
}
global void finish(Database.BatchableContext bc){
      //code that sends an email for execution of Batch    
}

My Trigger:
trigger Send_PDF_to_SharePointhelp on QNews__c (after insert, after update) {
    for(QNews__c qn: Trigger.new){
        string filename = qn.Article_Title__c+'_'+qn.Article_Version_Number__c+'.pdf';
        String emailbody='This attachment is the PDF copy of Article:  '+qn.Article_Title__c+' with version number '+qn.Article_Version_Number__c;   SendVFAsAttachment.sendVF('vijaku@vsp.com',qn.Data_Category__c,emailbody,UserInfo.getSessionId(),qn.Article_ID__c,qn.Article_Title__c,filename,qn.Article_Type__c); 
    }  
}

Future Method:
public class SendVFAsAttachment{
    @future(callout=true)
    public static void sendVF(String EmailIdCSV, String Subject,String body,String userSessionId, ID articleid,String ArticleTitle,String attachmentfilename,String ArticleType)
    {
        string newID = String.valueOf(articleid);
        String addr = 'https://vsp--kmbuild.cs10.my.salesforce.com/services/apexrest/sendPDFEmail';
        HttpRequest req = new HttpRequest();
        req.setEndpoint( addr );
        req.setMethod('POST');
        req.setHeader('Authorization', 'OAuth ' + userSessionId);
        req.setHeader('Content-Type','application/json');
        Map<String,String> postBody = new Map<String,String>();
        postBody.put('EmailIdCSV',EmailIdCSV);
        postBody.put('Subject',Subject);
        postBody.put('body',body);
        postBody.put('newID',newID);
        postBody.put('attachmentfilename',attachmentfilename);
        postBody.put('ArticleTitle',ArticleTitle);
        postBody.put('ArticleType',ArticleType);
        String reqBody = JSON.serialize(postBody);
        req.setBody(reqBody);
        Http http = new Http();
        HttpResponse response = http.send(req);
    }
}

Class to send email, exposed to REST API:
@RestResource(urlMapping='/sendPDFEmail/*')
Global class GETPDFContent{
     @HttpPost
    global static void sendEmail(String EmailIdCSV, String Subject, String body,string newID,string attachmentfilename,string ArticleTitle,string ArticleType) {
    List<String> EmailIds = EmailIdCSV.split(',');
        PageReference ref = Page.Multi_Topic_PDF;
        if(ArticleType=='Multi_Topic__kav'){
            ref = Page.Multi_Topic_PDF; 
          }
         if(ArticleType=='Additional_Benefits__kav'){
            ref = Page.AdditionalBenefits_PDF; 
          }
         if(ArticleType=='Core_Benefit__kav'){
            ref = Page.CoreBenefit_PDF; 
          }
         if(ArticleType=='Multi_Topic_Details__kav'){
            ref = Page.MultiTopicDetails_PDF; 
          }
         if(ArticleType=='Open_Enrollment__kav'){
            ref = Page.OpenEnrollment_PDF; 
          }
         if(ArticleType=='Single_Topic__kav'){
            ref = Page.SingleTopic_PDF; 
          }
            ref.getParameters().put('id',newID); 
            ref.setRedirect(true);
        Blob b = ref.getContentAsPDF();
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
        efa1.setFileName(attachmentfilename);
        efa1.setBody(b);
        String addresses;
        email.setSubject( Subject );
        email.setToAddresses(EmailIds);
        email.setPlainTextBody(Body);
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa1});
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
}