• Radhe Shyam
  • NEWBIE
  • 0 Points
  • Member since 2014
  • Software Engineer
  • Kloudrac Software Pvt. Ltd.


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 3
    Replies
Here is my apex batch:

global class EmailAlertToQuoteProposalApprovalUser implements Database.Batchable<sObject>, database.stateful{
    
    public String EVENT_TYPE_MEETING = 'Meeting';
    private Datetime EndTime=System.now();
    private String query;
    public String limitSize;
    private long recordcount;
    private string debuglog='';
    private integer batchcounter=0;
    private datetime processstarttime=system.now();
    private boolean haserror=false;
    private set<id> processedaccounts=new set<id>();
   
    global EmailAlertToQuoteProposalApprovalUser(datetime activitydatetime){
        if(activitydatetime==null){
            EndTime=System.now().adddays(-1);
        }
    }
   
   global Database.QueryLocator start(Database.BatchableContext BC){
       log('Batch process start time: ' + processstarttime);
       log('Batch process id: ' + bc.getJobID());
       log('Acvitity End Time Parameter: ' + EndTime);
       Datetime dtTimeNow=system.now();
       query='SELECT Id,Pending_With_Email__c,Submitted_Date_time__c FROM Apttus_Proposal__Proposal__c WHERE Submitted_Date_time__c!=null AND Submitted_Date_time__c>=: dtTimeNow' + ' ORDER BY Id';
       if(limitSize!=null)
       {
        query = query + ' LIMIT ' +  limitSize;
       }     
            
       log(query); 
       return Database.getQueryLocator(query);                                    
      
    }
   
    global void execute(Database.BatchableContext BC, List<sObject> scope){
        log('Batch number: ' + batchcounter);
        set<id> QuoteProposalId=new set<id>(); 
        list<Apttus_Proposal__Proposal__c>  lstQuoteProposal=new list<Apttus_Proposal__Proposal__c>();
        for(sobject so: scope){
            id quoteid=(Id)so.get('Id');
            Apttus_Proposal__Proposal__c objQuote= (Apttus_Proposal__Proposal__c)so;
            if(!processedaccounts.contains(quoteid)){
                QuoteProposalId.add(quoteid);
                lstQuoteProposal.add(objQuote);
            }
        }
        if(QuoteProposalId.size()>0){
            log('Number of accounts to be processed: ' + QuoteProposalId.size());
            log('Account Ids: '+ QuoteProposalId);
            List<String> CCAddress=new List<String>();
            CCAddress.add('abaranwal@kloudrac.com');
            string TemplateId=system.Label.quoteEmailTemplate;
            
            processedaccounts.addAll(QuoteProposalId);
            try{
                Messaging.SingleEmailMessage[] mails=new Messaging.SingleEmailMessage[0];
                 
                for(Apttus_Proposal__Proposal__c qt:lstQuoteProposal){
                    list<string> AdditionalRecipients=new list<string>();
                    AdditionalRecipients.add(qt.Pending_With_Email__c);
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    mail.setToAddresses(AdditionalRecipients);
                    mail.setCcAddresses(CCAddress);
                    //mail.setOrgWideEmailAddressId(OrgWideEmailId);
                    mail.setTemplateId(TemplateId);
                    mail.whatid=qt.id;
                    mail.setTargetObjectId('003n0000008FULE'); 
                    mails.add(mail);

                }
                
                log('Sending emails ... count: ' + mails);
                Messaging.sendEmail(mails); 
            
                        
            }catch(Exception e) {
                haserror=true;
                string body=e.getMessage();
                log(body);
    
            }
        }else{
            log('No Quote/Proposal is processed in this batch');
        }
        ++batchcounter;
    }

    global void finish(Database.BatchableContext BC){
        log('Entire batch process has ended');
        SaveDebugLog();
    } 
    
    public static void SendEmail(String Subject,String Body,String[] Recipeints){
        /*String Displayname=Lookup.getTarget('UpdateOpportunity', 'Config','FromAddress', true);
        //OneCRM.sandbox@ge.com
        Id OrgWideEmailId=[Select Id, DisplayName, Address from OrgWideEmailAddress where Address =:Displayname].Id;    
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(Recipeints);
        mail.setSubject(Subject);
        mail.setPlainTextBody(Body);
        mail.setOrgWideEmailAddressId(OrgWideEmailId);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});*/
                
    }
    

    private void log(string msg){
        debuglog+=msg+'\n';
        system.debug(logginglevel.info,msg);
    }
    
    private Id SaveDebugLog(){
        String recStr =  'Transaction Log';
        
        Integration_Log__c log= new Integration_Log__c();
        log.Call_Method__c = 'BatchProcessQuoteProposalEscalationEmail';
        log.Object_Name__c = 'Quote/Proposal';
        log.Call_Time__c = processstarttime;            
        log.Status__c = haserror?'Failure':'Success';
        insert log;        
        
        recStr += '\nInterfaceId:' + log.Object_Name__c;
        recStr += '\nObjectId:' + log.Object_Id__c;
        recStr += '\nCallTime:' + log.Call_Time__c.format('yyyy.MM.dd  HH:mm:ss.SSS z');
        recStr += '\nStatus:' + log.Status__c;
        recStr += '\nLogId:'+ log.Id;
        recStr += '\n';            
        recStr += debuglog;
        
        Blob recBlob= Blob.valueOf(recStr);
        Attachment att= new attachment();
        att.Name = 'Log Details ' +system.now()+'.txt';
        att.ParentId = log.Id; 
        att.Body = Blob.valueof(recStr); 
        insert att;     
        
        return log.id;
    }    
    
    public static void startbatch(datetime activitytime){
        
        EmailAlertToQuoteProposalApprovalUser aula=new EmailAlertToQuoteProposalApprovalUser(activitytime);
        aula.log('activitytime: ' + activitytime);
        aula.EndTime=activitytime;
        if(activitytime==null){
            aula.EndTime=System.now().adddays(-1);
        }
        ID batchprocessid = Database.executeBatch(aula);
        System.debug('Apex Job id: ' + batchprocessid );
    }
}


And here is schedulable:  I want to run batch file after every 5 minutes... Please Help
===============================================

global class scheduledQuoteReminderBatchable implements Schedulable {
   global void execute(SchedulableContext sc) {
       EmailAlertToQuoteProposalApprovalUser aula=new EmailAlertToQuoteProposalApprovalUser(system.now());
       ID batchprocessid = Database.executeBatch(aula);
       
   }
}
Hi guys, 
I am trying to write a workflow rule field update through Time Trigger.
This workflow is not running with "Evaluation Criteria : Evaluate the rule when a record is created, and any time it’s edited to subsequently meet criteria"

While it is running on Created Evaluton criteria.What would be the reason?
Here is workflow i have created

Date/time Difference field is calculated by the formula - Now() - 0.040 .  It assign the time 58 min. before the Current time. i.e if Now() returns 4:00 PM then value of Date/time Difference will be 3:02 PM.
 
i have 2 objects, Opportunity and CustomOpportunity(custom obj.) . I want to write a trigger, if i create a record on Opportunity, then one record should be created on CustomOpportunity and if i edit the record, the changes should be reflect on the record of Customopportunity object and vice versa.

 
Here is my apex batch:

global class EmailAlertToQuoteProposalApprovalUser implements Database.Batchable<sObject>, database.stateful{
    
    public String EVENT_TYPE_MEETING = 'Meeting';
    private Datetime EndTime=System.now();
    private String query;
    public String limitSize;
    private long recordcount;
    private string debuglog='';
    private integer batchcounter=0;
    private datetime processstarttime=system.now();
    private boolean haserror=false;
    private set<id> processedaccounts=new set<id>();
   
    global EmailAlertToQuoteProposalApprovalUser(datetime activitydatetime){
        if(activitydatetime==null){
            EndTime=System.now().adddays(-1);
        }
    }
   
   global Database.QueryLocator start(Database.BatchableContext BC){
       log('Batch process start time: ' + processstarttime);
       log('Batch process id: ' + bc.getJobID());
       log('Acvitity End Time Parameter: ' + EndTime);
       Datetime dtTimeNow=system.now();
       query='SELECT Id,Pending_With_Email__c,Submitted_Date_time__c FROM Apttus_Proposal__Proposal__c WHERE Submitted_Date_time__c!=null AND Submitted_Date_time__c>=: dtTimeNow' + ' ORDER BY Id';
       if(limitSize!=null)
       {
        query = query + ' LIMIT ' +  limitSize;
       }     
            
       log(query); 
       return Database.getQueryLocator(query);                                    
      
    }
   
    global void execute(Database.BatchableContext BC, List<sObject> scope){
        log('Batch number: ' + batchcounter);
        set<id> QuoteProposalId=new set<id>(); 
        list<Apttus_Proposal__Proposal__c>  lstQuoteProposal=new list<Apttus_Proposal__Proposal__c>();
        for(sobject so: scope){
            id quoteid=(Id)so.get('Id');
            Apttus_Proposal__Proposal__c objQuote= (Apttus_Proposal__Proposal__c)so;
            if(!processedaccounts.contains(quoteid)){
                QuoteProposalId.add(quoteid);
                lstQuoteProposal.add(objQuote);
            }
        }
        if(QuoteProposalId.size()>0){
            log('Number of accounts to be processed: ' + QuoteProposalId.size());
            log('Account Ids: '+ QuoteProposalId);
            List<String> CCAddress=new List<String>();
            CCAddress.add('abaranwal@kloudrac.com');
            string TemplateId=system.Label.quoteEmailTemplate;
            
            processedaccounts.addAll(QuoteProposalId);
            try{
                Messaging.SingleEmailMessage[] mails=new Messaging.SingleEmailMessage[0];
                 
                for(Apttus_Proposal__Proposal__c qt:lstQuoteProposal){
                    list<string> AdditionalRecipients=new list<string>();
                    AdditionalRecipients.add(qt.Pending_With_Email__c);
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    mail.setToAddresses(AdditionalRecipients);
                    mail.setCcAddresses(CCAddress);
                    //mail.setOrgWideEmailAddressId(OrgWideEmailId);
                    mail.setTemplateId(TemplateId);
                    mail.whatid=qt.id;
                    mail.setTargetObjectId('003n0000008FULE'); 
                    mails.add(mail);

                }
                
                log('Sending emails ... count: ' + mails);
                Messaging.sendEmail(mails); 
            
                        
            }catch(Exception e) {
                haserror=true;
                string body=e.getMessage();
                log(body);
    
            }
        }else{
            log('No Quote/Proposal is processed in this batch');
        }
        ++batchcounter;
    }

    global void finish(Database.BatchableContext BC){
        log('Entire batch process has ended');
        SaveDebugLog();
    } 
    
    public static void SendEmail(String Subject,String Body,String[] Recipeints){
        /*String Displayname=Lookup.getTarget('UpdateOpportunity', 'Config','FromAddress', true);
        //OneCRM.sandbox@ge.com
        Id OrgWideEmailId=[Select Id, DisplayName, Address from OrgWideEmailAddress where Address =:Displayname].Id;    
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(Recipeints);
        mail.setSubject(Subject);
        mail.setPlainTextBody(Body);
        mail.setOrgWideEmailAddressId(OrgWideEmailId);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});*/
                
    }
    

    private void log(string msg){
        debuglog+=msg+'\n';
        system.debug(logginglevel.info,msg);
    }
    
    private Id SaveDebugLog(){
        String recStr =  'Transaction Log';
        
        Integration_Log__c log= new Integration_Log__c();
        log.Call_Method__c = 'BatchProcessQuoteProposalEscalationEmail';
        log.Object_Name__c = 'Quote/Proposal';
        log.Call_Time__c = processstarttime;            
        log.Status__c = haserror?'Failure':'Success';
        insert log;        
        
        recStr += '\nInterfaceId:' + log.Object_Name__c;
        recStr += '\nObjectId:' + log.Object_Id__c;
        recStr += '\nCallTime:' + log.Call_Time__c.format('yyyy.MM.dd  HH:mm:ss.SSS z');
        recStr += '\nStatus:' + log.Status__c;
        recStr += '\nLogId:'+ log.Id;
        recStr += '\n';            
        recStr += debuglog;
        
        Blob recBlob= Blob.valueOf(recStr);
        Attachment att= new attachment();
        att.Name = 'Log Details ' +system.now()+'.txt';
        att.ParentId = log.Id; 
        att.Body = Blob.valueof(recStr); 
        insert att;     
        
        return log.id;
    }    
    
    public static void startbatch(datetime activitytime){
        
        EmailAlertToQuoteProposalApprovalUser aula=new EmailAlertToQuoteProposalApprovalUser(activitytime);
        aula.log('activitytime: ' + activitytime);
        aula.EndTime=activitytime;
        if(activitytime==null){
            aula.EndTime=System.now().adddays(-1);
        }
        ID batchprocessid = Database.executeBatch(aula);
        System.debug('Apex Job id: ' + batchprocessid );
    }
}


And here is schedulable:  I want to run batch file after every 5 minutes... Please Help
===============================================

global class scheduledQuoteReminderBatchable implements Schedulable {
   global void execute(SchedulableContext sc) {
       EmailAlertToQuoteProposalApprovalUser aula=new EmailAlertToQuoteProposalApprovalUser(system.now());
       ID batchprocessid = Database.executeBatch(aula);
       
   }
}