loading... Salesforce Cat

Wednesday, December 24, 2014

Controlling Approval Proccesses From Apex

Salesforce Approval Processes are one of the greatest declarative features comes with the platform.It saves tons of work you have to do as developer if you had to implement them via code.Although they are very useful in their default form, you may come up against requirements that require more than declarative customizations provide. Programmatic access to Salesforce Approval process via Apex come can handy in such situations. This Post discusses how Approval Process Submit ,Approve and Reject actions can be triggered from Apex code

Submit 



 Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();  
 req1.setComments('Submitting request for approval.');  
 req1.setObjectId(RecordId);  
 //RecordId is the id of the record that is submitted for Approval  
 Approval.ProcessResult result = Approval.process(req1);                                    


Approve


 //RecordId is the id of the record that is submitted for Approval 
List<ProcessInstance> PI = [Select Id,TargetObjectId FROM ProcessInstance WHERE TargetObjectId=:RecordId AND Status='Pending'];  
 if(PI.size() > 0)  
 {  
  List<ProcessInstanceWorkitem> PIWI = [Select Id ,ActorId FROM ProcessInstanceWorkitem WHERE ProcessInstanceId=:PI[0].Id];  
  if(PIWI.size() > 0)  
  {  
   Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();  
   req2.setComments('Approving request.');  
   req2.setAction('Approve');  
   req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});  
   req2.setWorkitemId(PIWI[0].Id);  
   Approval.ProcessResult result2 = Approval.process(req2);  
  }  
 }  


Reject



 //RecordId is the id of the record that is submitted for Approval 
List<ProcessInstance> PI = [Select Id,TargetObjectId FROM ProcessInstance WHERE TargetObjectId=:RecordId AND Status='Pending'];  
 if(PI.size() > 0)  
 {  
      List<ProcessInstanceWorkitem> PIWI = [Select Id ,ActorId FROM ProcessInstanceWorkitem WHERE ProcessInstanceId=:PI[0].Id];  
     if(PIWI.size() > 0)  
     {  
          Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();  
         req2.setComments('Approving request.');  
         req2.setAction('Reject');  
         req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});  
         req2.setWorkitemId(PIWI[0].Id);  
         Approval.ProcessResult result2 = Approval.process(req2);  
     }  
 }  

Thursday, December 4, 2014

Salesforce Sending Mail Attachments

Sending attachments via mail has become a common occurrence in everyday life.So it is not much of a surprise if you get a requirement to send and attachment via system generated mail as I did. So in this article I am going to discuss how to send an Word document generated by Visualforce as a mail attachment.I am going to use the Visualforce  Word document generated  in my previous post Render Visualforce Page As A Word Document  as the attachment for the purpose of this article.

given below is the Apex code for sending Word Form as an mail attachment.



     List<Messaging.SingleEmailMessage> EmailMessages= new List<Messaging.SingleEmailMessage>();  
     List<User> loggedUser = [Select Id,Name,Email from User where Id=:UserInfo.getUserId()];  
      Messaging.SingleEmailMessage EmailMsg = new Messaging.SingleEmailMessage();  
      //define mail body content -begin (here i used html tags to format my mail body because I am setting htmbody  
      String EmailBody = '<html><body> Dear User,<br/><br/>';  
      EmailBody ='Please Fill the attached document and send us';  
      EmailBody += '<br/><br/>Thank you,<br/>'+loggedUser[0].Name+'<br/><br/>';  
      EmailBody += '</body></html>';  
      //define mail body content -end  
      EmailMsg.setHtmlBody(EmailBody); // set the html body contnet  
      EmailMsg.setToAddresses(new String[]{'mailaddreses seperated by commas'});//set recipient mail addresses  
      EmailMsg.setSubject('SalesforceCat Demo');  
      EmailMsg.setReplyTo(loggedUser[0].Email);// set reply to email address becuase I expect this mailed to be replied  
      EmailMsg.setUseSignature(true);  
      PageReference Word = new PageReference('/apex/WordDownloadExample'); // url of ur visualforce page rendered as a word  
      //Word.getParameters().put('Id','xxxx'); if any parameters need to be passed to the page 
      Word.setRedirect(true); //do a virtual redirect to the page so we can get page content using page reference
      Blob b;  
      b = Word.getContent();// get the content of the page and assignment to blob  
     
      
      Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();// create an attachment instance  
      attachment.setBody(b); // set the blob as attachment content  
      attachment.setFileName('attach.doc'); // set the attachment name  
      EmailMsg.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment}); // link the attachment to the mail  
      EmailMessages.add(EmailMsg);  
      if(!Test.isRunningTest())  
      {  
      Messaging.sendEmail(EmailMessages);   
      }   

 Mail with word form as an attachment


Mail with Word attachment
mail sent