Saturday, December 20, 2014

Custom Labels as Global Variables

I haven't found the official Salesforce way for creating global variables or configuration properties yet, but this is what I found at one of my clients that I liked.

Say you want to create a global variable to store a public key with a value of "1234xyz".  Just create a new Custom Label with Name = "Global_PublicKey" and Value="1234xyz"



So in Apex you just reference the value as Label.Global_PublicKey:

  
private boolean isAuthorized(){
        String qPublickKey = ApexPages.currentPage().getParameters().get('publicKey');
        return qPublickKey.contains(Label.Global_PublicKey);
}


In a VisualForce page you refence it {!$Label.Global_PublicKey}:

  
<apex:page showheader="false" sidebar="false">
   <apex:pageblock title="Test">
   Global Variable PublicKey = {!$Label.Global_PublicKey}
   </apex:pageblock>
</apex:page>

Tuesday, December 16, 2014

Parsing Emails From Salesforce and Salesforce For Outlook

When emails are sent or received in SFDC, either through SFDC itself or the Salesforce for Outlook plugin, the information is written to the Activities object.  This code helps you parse out the info from either type of email so you can, for example, save it to another table too.


  
 Trigger CopyActivityToCommunication on Task (after insert) {

   Set<ID> ids = Trigger.newMap.keySet();
   List<Task> taskList = [SELECT Id, WhoId, WhatId, CallType, Type, Description,
   Status,Subject,OwnerId,WhatCount,AccountId,CallObject,WhoCount, RecurrenceActivityId  FROM task WHERE id in :ids];
   List<Communication__c> comsToUpdate = new List<Communication__c>();
   RecordType RecType = [Select Id,SobjectType,Name From RecordType  Where SobjectType = 'Communication__c' and Name= 'Outreach'];
   
   for (Task taskItem : taskList ){
       if (taskItem.Type == 'Email'){
           String subjectBody = taskItem.Description;
           
           /* Emails sent from SFDC will either have 'Additional To:' or nothing.  Emails sent from Outlook always only have a 'To:'*/
           Boolean isFromSFDC = subjectBody.contains('Additional To:');
           List<String> AllEmails = new List<String>();
               if(isFromSFDC){
                   System.Debug('Inside isFromSFDC');
                   String[] bodyLines = subjectBody.split('\n');
                   
                   //Additional To:
                   String[] ToEmails = bodyLines[0].substring(14).split(';'); 
                   for(String email : ToEmails){
                       AllEmails.Add(email.trim());
                   }
                   //CC:
                   String[] ccEmails = bodyLines[1].substring(3).split(';'); 
                   for(String email : ccEmails ){
                       if(email.trim() != null || email.trim() != ''){
                           AllEmails.Add(email.trim());
                       }
                   }
                   //BCC:
                   String[] bccEmails = bodyLines[2].substring(4).split(';'); 
                   for(String email : bccEmails ){
                       if(email.trim() != null || email.trim() != ''){
                           AllEmails.Add(email.trim());
                       }
                   }          
               } else {
                   System.Debug('Inside From Outlook');   
                   String[] bodyLines = subjectBody.split('\n');
                   
                   //Additional To:
                   String[] ToEmails = bodyLines[0].substring(3).split(','); 
                   for(String email : ToEmails){
                       AllEmails.Add(email.trim());
                   }
                   
                   integer maxSize = bodyLines.size();
                   if(maxSize > 4) {maxSize = 5;}
                   System.Debug('maxSize  : ' + maxSize );
                   
                   if(maxSize > 2){
                       System.Debug('bodyLines[0] : ' + bodyLines[0]);
                       System.Debug('bodyLines[1] : ' + bodyLines[1]);
                       System.Debug('bodyLines[2] : ' + bodyLines[2]);
                   }
                   
                   for(integer i = 0; i < maxSize; i++){
                       if(bodyLines[i].contains('Cc:')){
                           System.Debug('Inside Cc:');
                           String[] ccEmails = bodyLines[i].substring(3).split(','); 
                           for(String email : ccEmails ){
                               if(email.trim() != null || email.trim() != ''){
                                   AllEmails.Add(email.trim());
                               }
                           }
                       }
                       if(bodyLines[i].contains('Bcc:')){
                           System.Debug('Inside Bcc:');
                           String[] bccEmails = bodyLines[i].substring(4).split(','); 
                           for(String email : bccEmails ){
                               if(email.trim() != null || email.trim() != ''){
                                   AllEmails.Add(email.trim());
                               }
                           }
                       }
                   } 
               }
               

           List<Contact> individualList = [Select Id, Name, Current_Coach__r.Id, Current_Financial_Services_Staff_Contact__r.Id   From Contact Where email = :AllEmails];
           for(Contact individual : individualList){
               
               Communication__c com = new Communication__c();
               
               /* Add stuff to communication (Custom) object*/
               
               comsToUpdate.add(com);
           }
       }
   }
   
  
  insert(comsToUpdate);
   

}

Attach a File to an Object in Apex

I left out the visual force, but here is the apex code.  In this case I'm associating the attachment to a new case that's created.



   1:  public class MyController{
   2:   
   3:      public Attachment attachment {
   4:        get {
   5:            if (attachment == null){
   6:              attachment = new Attachment();
   7:            }
   8:            return attachment;
   9:          }
  10:        set;
  11:      }
  12:      
  13:      
  14:      public void myFunction() {
  15:        try{
  16:              
  17:          Case c = new Case;
  18:          /* Do all the stuffs to the case object */
  19:          INSERT c;
  20:               
  21:          attachment.OwnerId = UserInfo.getUserId();
  22:          attachment.ParentId = c.id;
  23:          attachment.IsPrivate = false;
  24:          try {
  25:            insert attachment;
  26:          } catch (DMLException e) {
  27:            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
  28:          } finally {
  29:            attachment = new Attachment(); 
  30:          }  
  31:        }
  32:      }
  33:  }

Why?

This is my personal place for my own code snippet salesforce.com and force.com that might be of use to others.  I'm not going to do a lot of explanations, because like I said, I am the principle audience.


What I'm using for code formatting:

http://codeformatter.blogspot.com/?_sm_au_=iVVj1DNfV48r1fkj

http://www.manoli.net/csharpformat/format.aspx

http://www.craftyfella.com/2010/01/syntax-highlighting-with-blogger-engine.html

Get RecordType Id Programmatically

This is when you need to assigned the RecordType to an object in apex.  You don't want to hard code the ID because it's ugly and might break the code during deploy.

It works for standard objects (like Case here):
1:  Case c = new Case();  
2:  RecordType RecType = [Select Id,SobjectType,Name From RecordType Where SobjectType = 'Case' and Name= 'Quality Alerts (QA)'];  
3:  c.RecordTypeId = RecType.Id;  


And custom objects:
1:  Communication__c com = new Communication__c();  
2:  RecordType RecType = [Select Id,SobjectType,Name From RecordType Where SobjectType = 'Communication__c' and Name= 'Outreach'];  
3:  com .RecordTypeId = RecType.Id;