• Aaron Hill
  • NEWBIE
  • 65 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 13
    Questions
  • 19
    Replies
Hi Everyone,

So a little background info. We created a relationship between leads and accounts through a lookup field on leads called "Related Account". I wanted leads to be assigned to accounts based on various criteria in a particular order. I initially wrote the trigger (with a significant amount of help from this community) to match a lead to an account based on company name. And then, using the same syntax from that prototype added three more criteria, so if there wasn't a match based on company name it would match secondly on LinkedIn Company ID, Thirdly on matching the lead email domain with the account website (I parsed it up to the beginning of the domain), and Fourthly on matching the lead email website with the account website. 

It works fine but the problem is- it's super slow. Like the load time is increased by 4 or 5 more seconds on average. Also bulk uploads can take up to 30-40 minutes. This is not ideal. Lately I've been disabling the trigger just before a bulk upload and re-enabling it afterwards. My question- is there a way that I can refactor my code so that it runs faster? I use a lot of lists, so I'm thinking that might have something to do with it.

Here's my code:
 
trigger updateRelatedAccount on Lead (before update, before insert) { //you NEED before insert too
    set<string> accNames = new set<string>();  // Set for company name
    set<Decimal> accLIDs = new set<Decimal>(); // Set for Company ID
    set<String> accEmails = new set<String>(); // Set for account emails
    set<String> accWebs = new set<String>(); //Set for account websites
        for(Lead ld : Trigger.New){
            accNames.add(ld.Company);
            accLIDs.add(ld.Company_ID__c);
            accEmails.add(ld.Lead_Email_Domain_Parsed__c);
            accWebs.add(ld.Lead_Website_Parsed__c);
        } 
    
    

        list<Account> accs = new list<Account>([Select Id,Name,LinkedIn_Id__c,Account_Website_Parsed__c from account where name in :accNames or LinkedIn_Id__c in :accLIDs or Account_Website_Parsed__c in :accEmails or Account_Website_Parsed__c in :accWebs]); //using WHERE IN
        map<String,id> mapaccs = new map<String,id>();
        map<Decimal, id> mapids = new map<Decimal, id>();
        map<String, id> mapemails = new map<String, id>();
        map<String, id> mapwebs = new map<String, id> ();

        for(Account ac : accs){
            mapaccs.put(ac.Name,ac.Id);
            mapids.put(ac.LinkedIn_Id__c,ac.Id);
            mapemails.put(ac.Account_Website_Parsed__c, ac.Id);
            mapwebs.put(ac.Account_Website_Parsed__c, ac.Id);
        }

        for(Lead lds : Trigger.New){ 
            //you new lead has company id, check if lead has same id
            if (lds.Company_ID__c !=null && mapids.get(lds.Company_ID__c)!=null) { 
                lds.Related_Account__c = mapids.get(lds.Company_ID__c); //if so, copy id
            } else if (lds.Company != null && mapaccs.get(lds.company) != null) { //if not, search by name
                lds.related_account__c = mapaccs.get(lds.company);
            } else if (lds.Lead_Email_Domain_Parsed__c != null && mapemails.get(lds.Lead_Email_Domain_Parsed__c) != null) {
                lds.Related_Account__c = mapemails.get(lds.Lead_Email_Domain_Parsed__c);
            } else if (lds.Lead_Website_Parsed__c != null && mapWebs.get(lds.Lead_Website_Parsed__c) != null) {
                lds.Related_Account__c = mapWebs.get(lds.Lead_Website_Parsed__c);
            } else {
                lds.Related_Account__c = null;
            }
        }
  }

I'd appreciate any feedback! Thanks!

Best,
Aaron
I have a trigger to update a related account field on my leads when there is a name match or a match of a third party id. This trigger should fire whenever a lead is created or updated and works perfectly for individual records but does not work when I use the data import wizard to update multiple leads at once. 

My research into this issue tells me that apex triggers by default should fire for bulk records and indeed previous triggers I've written have worked for bulk records so I'm confused about why there's an exeption here. I'll paste my code below. Thank you.
 
trigger updateRelatedAccount on Lead (before update, before insert) { 
    set<string> accNames = new set<string>();
    set<Decimal> accLIDs = new set<Decimal>();
        for(Lead ld : Trigger.New){
        	accNames.add(ld.Company);
            accLIDs.add(ld.Company_ID__c);
        } 
    
    

        list<Account> accs = new list<Account>([Select Id,Name,LinkedIn_Id__c from account where name in :accNames or LinkedIn_Id__c in :accLIDs]); //using WHERE IN
        map<String,id> mapaccs = new map<String,id>();
    	map<Decimal, id> mapids = new map<Decimal, id>();

        for(Account ac : accs){
        	mapaccs.put(ac.Name,ac.Id);
            mapids.put(ac.LinkedIn_Id__c,ac.Id);
        }

        for(Lead lds : Trigger.New){ 
            //you new lead has company id, check if lead has same id
            if (lds.Company_ID__c !=null and mapids.get(lds.Company_ID__c)!=null) { 
                lds.Related_Account__c = mapids.get(lds.Company_ID__c); //if so, copy id
            } else { //if not, search by name
                lds.related_account__c = mapaccs.get(lds.company);
            }
        }
  }

 
We have our leads linked to our accounts through a child/parent relationship. I currently have a trigger set up so that when a lead record is edited it checks the database for accounts that have a matching name and then links them through the "Related Account" lookup field. We have another field called company id though, which is a unique identifier for each company (we use the linkedin company id). This id is on both accounts and leads. I want to change the rule to match FIRST based on this ID and SECOND based on the name if there is no ID Match. I'll try to represent the goal with pseudocode below. And then I'll paste what I've got so far.

WHEN (A lead record is created or updated) {
      IF (Lead company ID is equal to Account company id) {
               //Update lead related account field to matching account.
      } ELSE IF (Lead Name is equal to Account Name) {
               //Update lead related account field to matching account.
      }
}


The pseudocode structure looks different from my actual code.
I tried to use "key: value" maps. This doesn't work. It works for a name match but not for an id match at all. 
I'd appreciate any pointers, thanks!
 
trigger updateRelatedAccount on Lead (before update) {
    set<string> accNames = new set<string>();
    set<Decimal> accLIDs = new set<Decimal>();
        for(Lead ld : Trigger.New){
        	accNames.add(ld.Company);
            accLIDs.add(ld.Company_ID__c);
        }
    
    

        list<Account> accs = new list<Account>([Select Id,Name,LinkedIn_Id__c from account where name=:accNames or LinkedIn_Id__c=:accLIDs]);
        map<String,id> mapaccs = new map<String,id>();
    	map<Decimal, id> mapids = new map<Decimal, id>();

        for(Account ac : accs){
        	mapaccs.put(ac.Name,ac.Id);
            mapids.put(ac.LinkedIn_Id__c,ac.Id);
        }

        for(Lead lds : Trigger.New){
            if (lds.Company_ID__c < 0) {
                lds.Related_Account__c = mapids.get(lds.Company_ID__c);
            } else {
                lds.related_account__c = mapaccs.get(lds.company);
            }
            
            
        	
        }
  }

 
I have a trigger already mostly written that relates a lead to a parent account object based on 2 criteria. The first is it's linkedin company id. So some of our leads and accounts have as a record their database id on linkedin. If a lead LID matches an account LID I want the lead to update it's related account to be that account with the same id. The SECOND criteria is name, so if the names match- the lead will update with an account with a matching name. I only want a name match search to happen if the id match doesn't happen. I'll post below what I have so far.
 
trigger updateRelatedAccount on Lead (before update) {
    set<string> accNames = new set<string>();
    set<Decimal> accLIDs = new set<Decimal>();
        for(Lead ld : Trigger.New){
        	accNames.add(ld.Company);
            accLIDs.add(ld.Company_ID__c);
        }
    
    

        list<Account> accs = new list<Account>([Select Id,Name,LinkedIn_Id__c from account where name=:accNames or LinkedIn_Id__c=:accLIDs]);
        map<String,id> mapaccs = new map<String,id>();
    	map<Decimal, id> mapids = new map<Decimal, id>();

        for(Account ac : accs){
        	mapaccs.put(ac.Name,ac.Id);
            mapids.put(ac.LinkedIn_Id__c,ac.Id);
        }

        for(Lead lds : Trigger.New){
            if (lds.Company_ID__c < 0) {
                lds.Related_Account__c = mapids.get(lds.Company_ID__c);
            } else {
                lds.related_account__c = mapaccs.get(lds.company);
            }
            
            
        	
        }
  }

I was patting myself on the back because I really REALLY thought this would work. It seems like it would. The mapids object should contain the LID as the key and then the related account id as the Value pair. 

If I do a name match it works perfectly. But if I try to do a LID match it fails. Can anyone help me out? Thanks!
So I was searching the community for something similar to this but I was hitting a wall because admittedly I don't know the nature of the relationship. I've set it up so that our standard account object has a field called Related Company. This is a vlookup field that can select a custom object called "Company" (there's another trigger in place that matches Accounts with Companies based on a name match). We've built a database of "Companies" that has 3 fields: LinkedIn, Industry, and Size. We want to pull this data from the related company and in to the matching account, to update the fields with the correct info from the Company object. 

Below is the code I have so far:
 
trigger updateAccountInfo on Account (before update) {
    
    
    for(Account acc : Trigger.new) {
        if(acc.Related_Company__c != null) {
            String comp = acc.Related_Company__c;
            accountInfo = [Select Industry__c, LinkedIn__c, Size__c 
                           from Related_Company__r 
                           where acc.Related_Company__c = :comp.Id];
            
            
        }
    }
}



I use the relationship __r connector but I still get the following message:

"sObject type 'Related_Company__r' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names."

So here's my question that I can't seem to figure out, Since this trigger is acting on account, which looks up to Company, does that mean that the SOQL query should utilize a Child to Parent relationship? And if so, isn't that what I'm doing? I apologize if this is all really basic I'm somewhat of a newbie. 

TL;DR How can I correct my syntax so that I can access a related object and update the fields within the Account Standard Object?

​Thanks!
I wrote a trigger (with the help of this community) so that a checkbox in the lead view gets updated when a task is created with the subject: 'First Time Meeting (Phone or In-Person)'. The trigger works well with one exception. I'll paste the code below and then describe the issue below that.
 
trigger hadFirstMeeting on Task (after insert) {
   List<Lead> updatelead = new List<Lead>();

    for (task t : Trigger.new) {
        if (t.Type.contains('First Time Meeting (Phone or In-Person)') && t.whoId!=null && t.whoId.getsobjectType() == Lead.sobjectType) {
            updatelead.add(new Lead(Id = t.whoId, Had_First_Time_Meeting__c  = true));
        }
    }
    if(!updatelead.isEmpty()) {
        update updatelead;
    }
}

Like I said above, this works well until we try to BCC an email to salesforce to document an activity. I get the following error from Gmail:

(Undelivered): 554 The apex class Messaging.EmailToSalesforceHandler failed due to: System.UnexpectedException: hadFirstMeeting: execution of AfterInsert  caused by: System.NullPointerException: Attempt to de-reference a null object  Trigger.hadFirstMeeting: line 5, column 1

There's a null exception in the trigger, but admittadly I have no idea if it's the problem. I tried messing with it and removing it altogether but nothing has worked. Can someone lend a hand? Thanks.

 
We have a lead/account relationship. I created a lead field called "Related Account", which is a lookup field. The lead is then added to a related list in the account view. I wrote the following so that when a lead was updated, the related account trigger would automatically find the matching account and populate the field with that information. It works great on an indiviual basis but not at all when I try to bulk update more than a handful of leads. The full error is:

"CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY:updateRelatedAccount: System.LimitException: Apex CPU time limit exceeded:--".

My question: can this code be refactored so that it works for bulk updates? And if so, how?

Thanks for your time.

 
trigger updateRelatedAccount on Lead (before update) {
    List<Account> accList = [SELECT Id, Name FROM Account];
    
    for (Lead lead : System.Trigger.new) {
        for (Account acc : accList) {
            if (lead.Company != '' && lead.Company == acc.Name) {
                lead.Related_Account__c = acc.Id;
            }
        }
    }
}

 
I've been trying to create a trigger for new tasks that are related to a lead. I've had a hard time explaining it in the past so before I post what I have so far I'll pseudo-code it.

//Trigger for when a new task is created
         //When this task is created on a lead
  
         //For (this new task) {

         //If the new task subject is equal to 'First Time Meeting (Phone or In-Person)'. {
                  //Change the custom field 'Had_First_Time_Meeting__c' to 'True'
         }
}
         //update the related lead
}


The task subject field is a picklist value. Here is the code that I created with the help of one of the other developers in the community.
 
trigger hadFirstMeeting on Task (before insert) {
    List<Lead> updatelead = new List<Lead>();
    for (task t : Trigger.new) {
        
        if (t.Type.contains('First Time Meeting (Phone or In-Person)')&& t.whatId!=null && t.whatId.getsobjectType() == Lead.sobjectType) {
            updatelead.add(new Lead(Id = t.whatId, Had_First_Time_Meeting__c  = true));
        }
    }
    
    update updatelead;
}

This task saves just fine and there are no errors when I create a new task with this subject. But it doesn't update the 'Had_First_Time_Meeting__c' field on the related lead to true. I have quadruple checked the merge fields to make sure they are accurate. I have no idea what to do. When I remove line 6 (The body of the if statement) and remove the second two requeirements from line 5 (&& t.whatId!=null && t.whatId.getsobjectType() == Lead.sobjectType). I can get it to update the task field . But other than that I'm a little lost.

I'd really appreciate any help.
Hey everyone,

I'm using a recipie from the Force.com cookbook to prevent duplicate records from saving. Here's the link: http://developer.force.com/cookbook/recipe/preventing-duplicate-records-from-saving
 
trigger leadDuplicatePreventer on Lead
                               (before insert, before update) {

    Map<String, Lead> leadMap = new Map<String, Lead>();
    for (Lead lead : System.Trigger.new) {
		
        // Make sure we don't treat an email address that  
        // isn't changing during an update as a duplicate.  
    
        if ((lead.Email != null) &&
                (System.Trigger.isInsert ||
                (lead.Email != 
                    System.Trigger.oldMap.get(lead.Id).Email))) {
		
            // Make sure another new lead isn't also a duplicate  
    
            if (leadMap.containsKey(lead.Email)) {
                lead.Email.addError('Another new lead has the '
                                    + 'same email address.');
            } else {
                leadMap.put(lead.Email, lead);
            }
       }
    }
	
    // Using a single database query, find all the leads in  
    
    // the database that have the same email address as any  
    
    // of the leads being inserted or updated.  
    
    for (Lead lead : [SELECT Email FROM Lead
                      WHERE Email IN :leadMap.KeySet()]) {
        Lead newLead = leadMap.get(lead.Email);
        newLead.Email.addError('A lead with this email '
                               + 'address already exists.');
    }
}

My question, how do I modify this code in another trigger so that it prevents lead/contact duplicates  from being synched? Again this trigger should still be for leads being inserted but I want it to match against existing contact email rather than existing lead email. Thanks!
The title says it all. We have an activity type on leads called "First Time Meeting (Phone or In-Person)". So when someone in the org logs a call on a lead with this status we want to have a checkbox called "Had First Time Meeting" Update to "true". This is what I have so far:
 
trigger hadFirstMeeting on Task (before update) {
    for (task t : Trigger.new) {
        if (t.Type === "First Time Meeting (Phone or In-Person)") {
        
        }
    }
}

This produces an error: Error: Compile Error: line 3:23 no viable alternative at character '"' at line 3 column 23

I'm a JS programmer, fairly new to Apex. I originally planned for it to look like this: 
 
trigger hadFirstMeeting on Task on Lead (before update) {
    for (task t, Lead L : Trigger.new) {
        if (t.Type === "First Time Meeting (Phone or In-Person)") {
            L.Had_First_Time_Meeting__c = true;
        }
    }
}

But I have no idea if this is the correct syntax. I'm having a hard time finding resources for apex that talk about activities. I'm not sure how a lead should relate to a task within the code. And first I need to solve the error anyway. Can anyone offer some help on this? Thanks!

 
Hi, I created a VF page, which is meant to be a pricing calculator. It's divided into a grid using html and css and structured with apex code to allow for merge fields and inline editing. I embedded the page as a section of our opportunity object, where the calculator is supposed to live. When I first go to the page everything looks great, it loads fine and the inline editing works. Then when I click the save button that also sits inside the section the page reloads but the app doesn't. In the section where it lives there's another copy of the same page. AND inside the calculator section of that page is a preview of my source code. I'll paste my code below. It's a lot but I think we can ignore the divs and just look at the very top and very bottom. I think the answer lies there. 

Also below my code, I'll add a screen shot of what it looks like when the page reloads after I save something with inline edit.

I'm a JS Dev but I'm just now learning Apex and Visualforce, so forgive me if this is obvious. But I did about an hour of googling before I got to this point so any help would be greatly apprecited. Cheers.
 
<apex:page standardController="Opportunity" tabStyle="Opportunity">
   
    <apex:stylesheet value="https://fonts.googleapis.com/css?family=Raleway:400,700"/>
    <style>

        h1 {
            color: blue;
            }

        .field_container {
            width: 30%;
            height: 30px;
            display: inline-block;
            margin: 0;
            border: none;
        }
        .field_container_2 {
            width: 22.5%;
            height: 30px;
            display: inline-block;
            margin: 0;
            border: none;
        }
        h3 {
            color: #000;
            margin: 10px 0 0 10px;
            font-size: 1.5em;
            font-family: Raleway;
            }
       .row_container {
            padding-bottom: 20px;
            text-align: center;
            }
      .container {
            width: 80%;
       
            }
            
            .row {
            padding-top: 15px;
            display: block;
            }
           .pbHeader {
           color: #000000 !important;
           font-family: Raleway;
           }
           field_container.inlineEditUndo {
           display: none;
           }
           select {
           max-width: 150px;
           }
           body {
               background-color: #fff !important;
           }
           .opportunityTab .secondaryPalette, .individualPalette .opportunityBlock .secondaryPalette .opportunityTab .listViewport .subNav .linkBar, .opportunityTab .mComponent .cHeader, .opportunityTab .genericTable, .opportunityTab .bSubBlock, .opportunityTab .bPageBlock {
               border: none;
               }
    </style>

  <apex:form >
  <apex:pageBlock mode="inlineEdit">
  <div class="container">
  <!-- Animation Row -->
    <div class="row_container" id="elearning_animation_row">
      <h3>eLearning and Animation</h3>
      <div class="row" id="row_1">
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Minutes_ELA1__c}"/><apex:facet name="header">Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Level_ELA1__c}"/><apex:facet name="header">Level</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock >{!Opportunity.Price_ELA1__c}<apex:facet name="header">Price</apex:facet></apex:pageBlock></div>
      </div>
      <div class="row" id="row_2">      
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Minutes_ELA2__c}"/><apex:facet name="header">Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Level_ELA2__c}"/><apex:facet name="header">Level</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock >{!Opportunity.Price_ELA2__c}<apex:facet name="header">Price</apex:facet></apex:pageBlock></div>
      </div>
      <div class="row" id="row_3">
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Minutes_ELA3__c}"/><apex:facet name="header">Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Level_ELA3__c}"/><apex:facet name="header">Level</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock >{!Opportunity.Price_ELA3__c}<apex:facet name="header">Price</apex:facet></apex:pageBlock></div>
      </div>
    </div>
    <!-- ID Row -->
    <div class="row_container" id="instructional_design_row">
      <h3>Instructional Design</h3>
      <div class="row" id="row_4">
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Minutes_ID1__c}"/><apex:facet name="header">Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Level_ID1__c}"/><apex:facet name="header">Level</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock >{!Opportunity.Price_ID1__c}<apex:facet name="header">Price</apex:facet></apex:pageBlock></div>
      </div>
      <div class="row" id="row_5">      
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Minutes_ID2__c}"/><apex:facet name="header">Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Level_ID2__c}"/><apex:facet name="header">Level</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock >{!Opportunity.Price_ID2__c}<apex:facet name="header">Price</apex:facet></apex:pageBlock></div>
      </div>
    </div>
    
 <!--  Other Row -->
    <div class="row_container" id="other_row">
      <h3>Other</h3>
      <div class="row" id="row_7">      
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Project_Management__c}"/><apex:facet name="header">Level of Project Management</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock >{!Opportunity.Subtotal_PM__c}<apex:facet name="header">Project Management Price</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Voiceover_Minutes__c}"/><apex:facet name="header">Voiceover Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock >{!Opportunity.Price_VO__c}<apex:facet name="header">Voiceover Price</apex:facet></apex:pageBlock></div>
      </div>
      <div class="row" id="row_8">      
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Assessments_ELM_Creates__c}"/><apex:facet name="header">ELM Assessments</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock >{!Opportunity.Price_Assessments__c}<apex:facet name="header">Assessments Price - Total</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Stock_Photography_Minutes__c}"/><apex:facet name="header">Stock Photography Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock >{!Opportunity.Price_Stock_Photography__c}<apex:facet name="header">Stock Photography Price</apex:facet></apex:pageBlock></div>
      </div>
      <div class="row" id="row_9">      
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Assessments_Client_Provides__c}"/><apex:facet name="header">Client Assessments</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock >{!Opportunity.Price_Assessments__c}<apex:facet name="header">Assessments Price - Total</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Script__c}"/><apex:facet name="header">Script Writing Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock >{!Opportunity.Price_Scripting__c}<apex:facet name="header">Scripting Price</apex:facet></apex:pageBlock></div>
      </div>
      <div class="row" id="row_10">      
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Line_Item_Expense__c}"/><apex:facet name="header">Line Item Expense</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Line_Item_Expense_Notes__c}"/><apex:facet name="header">Line Item Expense Notes</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"></div>
         <div class="field_container_2" ><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Localization_Quote__c}"/><apex:facet name="header">Localization Quote</apex:facet></apex:pageBlock></div>
      </div>
    </div>
  <!--   Total Row -->
    <div class="row_container" id="total_row">
      <h3>Total</h3>
      <div class="row" id="row_10">
        <div class="field_container_2" ><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Total_Price__c}"/><apex:facet name="header"><strong>Total Price</strong></apex:facet></apex:pageBlock></div>
        <div class="field_container_2"></div>
        <div class="field_container_2"></div>
        <div class="field_container_2"></div>
      </div>
         <apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="editButton" />
    </div>
      <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" id="saveButton" />
                <apex:commandButton value="Cancel" action="{!cancel}" id="cancelButton" />
                </apex:pageBlockButtons>
</div>
  
    </apex:pageBlock>
   </apex:form>

</apex:page>

Image of Broken Page
I have a custom object called "Invoice" and a button called "Send Invoice" in this object. When it's clicked I use a URL hack to send bring up an email template. Here is the URL hack:
 
location.replace('/email/author/emailauthor.jsp?retURL=/{!Invoice__c.Id}&p3_lkid={!Invoice__c.Id}&rtype=003&template_id=00Xi0000000ioeb&p5=&p24=invoice@elearningmind.com');



Below that I want to put logic so that a couple fields within the view of the object are updated. One field is a picklist value: "Payment Status". The other field is a simple date field that I need to be updated with the day that the cutton is clicked, so I think I need to use TODAY().

My question: can I use merge fields for this or do I have to use an AJAX toolkit? Here is the merge field I was trying to use:
 
{!Invoice__c.Payment_Status__c}  = "Internal Invoice Request Sent";

But when I put it in there, the field does not update at all. If I have to use an AJAX toolkit could you please explain how I should structure it? I was looking at a lot of different examples but I couldn't seem to translate any of them to my situation. Either the page just reloaded without updating or I was given an error. 

Any help would be greatly apprecited. 

Best,
Aaron
I have a large text field on Opportunities that is populated with text formatted by html when the Opportunity is created. I use an apex trigger to do this. The field is a simple questionnare asking details about the opportunity the. The trigger only inserted html code into the field, nothing too fancy.

I created a visualforce email template with the field inside of it hoping I could send a copy of the questionnare to my team. I put the merge field inside of html tags. But when I tested the template my merge field rendered as plain text, with no line breaks, and all of the html tags. 

Here is the visualforce template:
 
<messaging:emailTemplate subject="RTF Email template" recipientType="User"
relatedToType="Opportunity"
 >

<messaging:htmlEmailBody >
<html>
    <h2><em>Hello! I'm a rich text email! And the general project details for this oppy are:</em></h2><br></br><br></br>
    {!relatedTo.General_Project_Details__c} c
    
    <br></br><br></br>
    <p style="{font-size: 32px; color:purple}">Look how rich I am!!!</p>
    
    <div style="border:1px solid green"><p style="font-size: 46px; color: red">I'm the richest text ever!</p></div>
</html>




</messaging:htmlEmailBody>
</messaging:emailTemplate>


And the resulting text looked like this:

User-added image


Normally I would use jQuery to re-encode the html. I saw something about using this command: 
!HTMLENCODE(myobjdata). 

But I've heard I can't use or shouldn't use jQuery in a visualforce email template.

I need that code to render! Can anyone help? Thanks!
We have our leads linked to our accounts through a child/parent relationship. I currently have a trigger set up so that when a lead record is edited it checks the database for accounts that have a matching name and then links them through the "Related Account" lookup field. We have another field called company id though, which is a unique identifier for each company (we use the linkedin company id). This id is on both accounts and leads. I want to change the rule to match FIRST based on this ID and SECOND based on the name if there is no ID Match. I'll try to represent the goal with pseudocode below. And then I'll paste what I've got so far.

WHEN (A lead record is created or updated) {
      IF (Lead company ID is equal to Account company id) {
               //Update lead related account field to matching account.
      } ELSE IF (Lead Name is equal to Account Name) {
               //Update lead related account field to matching account.
      }
}


The pseudocode structure looks different from my actual code.
I tried to use "key: value" maps. This doesn't work. It works for a name match but not for an id match at all. 
I'd appreciate any pointers, thanks!
 
trigger updateRelatedAccount on Lead (before update) {
    set<string> accNames = new set<string>();
    set<Decimal> accLIDs = new set<Decimal>();
        for(Lead ld : Trigger.New){
        	accNames.add(ld.Company);
            accLIDs.add(ld.Company_ID__c);
        }
    
    

        list<Account> accs = new list<Account>([Select Id,Name,LinkedIn_Id__c from account where name=:accNames or LinkedIn_Id__c=:accLIDs]);
        map<String,id> mapaccs = new map<String,id>();
    	map<Decimal, id> mapids = new map<Decimal, id>();

        for(Account ac : accs){
        	mapaccs.put(ac.Name,ac.Id);
            mapids.put(ac.LinkedIn_Id__c,ac.Id);
        }

        for(Lead lds : Trigger.New){
            if (lds.Company_ID__c < 0) {
                lds.Related_Account__c = mapids.get(lds.Company_ID__c);
            } else {
                lds.related_account__c = mapaccs.get(lds.company);
            }
            
            
        	
        }
  }

 
So I was searching the community for something similar to this but I was hitting a wall because admittedly I don't know the nature of the relationship. I've set it up so that our standard account object has a field called Related Company. This is a vlookup field that can select a custom object called "Company" (there's another trigger in place that matches Accounts with Companies based on a name match). We've built a database of "Companies" that has 3 fields: LinkedIn, Industry, and Size. We want to pull this data from the related company and in to the matching account, to update the fields with the correct info from the Company object. 

Below is the code I have so far:
 
trigger updateAccountInfo on Account (before update) {
    
    
    for(Account acc : Trigger.new) {
        if(acc.Related_Company__c != null) {
            String comp = acc.Related_Company__c;
            accountInfo = [Select Industry__c, LinkedIn__c, Size__c 
                           from Related_Company__r 
                           where acc.Related_Company__c = :comp.Id];
            
            
        }
    }
}



I use the relationship __r connector but I still get the following message:

"sObject type 'Related_Company__r' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names."

So here's my question that I can't seem to figure out, Since this trigger is acting on account, which looks up to Company, does that mean that the SOQL query should utilize a Child to Parent relationship? And if so, isn't that what I'm doing? I apologize if this is all really basic I'm somewhat of a newbie. 

TL;DR How can I correct my syntax so that I can access a related object and update the fields within the Account Standard Object?

​Thanks!
I wrote a trigger (with the help of this community) so that a checkbox in the lead view gets updated when a task is created with the subject: 'First Time Meeting (Phone or In-Person)'. The trigger works well with one exception. I'll paste the code below and then describe the issue below that.
 
trigger hadFirstMeeting on Task (after insert) {
   List<Lead> updatelead = new List<Lead>();

    for (task t : Trigger.new) {
        if (t.Type.contains('First Time Meeting (Phone or In-Person)') && t.whoId!=null && t.whoId.getsobjectType() == Lead.sobjectType) {
            updatelead.add(new Lead(Id = t.whoId, Had_First_Time_Meeting__c  = true));
        }
    }
    if(!updatelead.isEmpty()) {
        update updatelead;
    }
}

Like I said above, this works well until we try to BCC an email to salesforce to document an activity. I get the following error from Gmail:

(Undelivered): 554 The apex class Messaging.EmailToSalesforceHandler failed due to: System.UnexpectedException: hadFirstMeeting: execution of AfterInsert  caused by: System.NullPointerException: Attempt to de-reference a null object  Trigger.hadFirstMeeting: line 5, column 1

There's a null exception in the trigger, but admittadly I have no idea if it's the problem. I tried messing with it and removing it altogether but nothing has worked. Can someone lend a hand? Thanks.

 
We have a lead/account relationship. I created a lead field called "Related Account", which is a lookup field. The lead is then added to a related list in the account view. I wrote the following so that when a lead was updated, the related account trigger would automatically find the matching account and populate the field with that information. It works great on an indiviual basis but not at all when I try to bulk update more than a handful of leads. The full error is:

"CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY:updateRelatedAccount: System.LimitException: Apex CPU time limit exceeded:--".

My question: can this code be refactored so that it works for bulk updates? And if so, how?

Thanks for your time.

 
trigger updateRelatedAccount on Lead (before update) {
    List<Account> accList = [SELECT Id, Name FROM Account];
    
    for (Lead lead : System.Trigger.new) {
        for (Account acc : accList) {
            if (lead.Company != '' && lead.Company == acc.Name) {
                lead.Related_Account__c = acc.Id;
            }
        }
    }
}

 
I've been trying to create a trigger for new tasks that are related to a lead. I've had a hard time explaining it in the past so before I post what I have so far I'll pseudo-code it.

//Trigger for when a new task is created
         //When this task is created on a lead
  
         //For (this new task) {

         //If the new task subject is equal to 'First Time Meeting (Phone or In-Person)'. {
                  //Change the custom field 'Had_First_Time_Meeting__c' to 'True'
         }
}
         //update the related lead
}


The task subject field is a picklist value. Here is the code that I created with the help of one of the other developers in the community.
 
trigger hadFirstMeeting on Task (before insert) {
    List<Lead> updatelead = new List<Lead>();
    for (task t : Trigger.new) {
        
        if (t.Type.contains('First Time Meeting (Phone or In-Person)')&& t.whatId!=null && t.whatId.getsobjectType() == Lead.sobjectType) {
            updatelead.add(new Lead(Id = t.whatId, Had_First_Time_Meeting__c  = true));
        }
    }
    
    update updatelead;
}

This task saves just fine and there are no errors when I create a new task with this subject. But it doesn't update the 'Had_First_Time_Meeting__c' field on the related lead to true. I have quadruple checked the merge fields to make sure they are accurate. I have no idea what to do. When I remove line 6 (The body of the if statement) and remove the second two requeirements from line 5 (&& t.whatId!=null && t.whatId.getsobjectType() == Lead.sobjectType). I can get it to update the task field . But other than that I'm a little lost.

I'd really appreciate any help.
Hey everyone,

I'm using a recipie from the Force.com cookbook to prevent duplicate records from saving. Here's the link: http://developer.force.com/cookbook/recipe/preventing-duplicate-records-from-saving
 
trigger leadDuplicatePreventer on Lead
                               (before insert, before update) {

    Map<String, Lead> leadMap = new Map<String, Lead>();
    for (Lead lead : System.Trigger.new) {
		
        // Make sure we don't treat an email address that  
        // isn't changing during an update as a duplicate.  
    
        if ((lead.Email != null) &&
                (System.Trigger.isInsert ||
                (lead.Email != 
                    System.Trigger.oldMap.get(lead.Id).Email))) {
		
            // Make sure another new lead isn't also a duplicate  
    
            if (leadMap.containsKey(lead.Email)) {
                lead.Email.addError('Another new lead has the '
                                    + 'same email address.');
            } else {
                leadMap.put(lead.Email, lead);
            }
       }
    }
	
    // Using a single database query, find all the leads in  
    
    // the database that have the same email address as any  
    
    // of the leads being inserted or updated.  
    
    for (Lead lead : [SELECT Email FROM Lead
                      WHERE Email IN :leadMap.KeySet()]) {
        Lead newLead = leadMap.get(lead.Email);
        newLead.Email.addError('A lead with this email '
                               + 'address already exists.');
    }
}

My question, how do I modify this code in another trigger so that it prevents lead/contact duplicates  from being synched? Again this trigger should still be for leads being inserted but I want it to match against existing contact email rather than existing lead email. Thanks!
The title says it all. We have an activity type on leads called "First Time Meeting (Phone or In-Person)". So when someone in the org logs a call on a lead with this status we want to have a checkbox called "Had First Time Meeting" Update to "true". This is what I have so far:
 
trigger hadFirstMeeting on Task (before update) {
    for (task t : Trigger.new) {
        if (t.Type === "First Time Meeting (Phone or In-Person)") {
        
        }
    }
}

This produces an error: Error: Compile Error: line 3:23 no viable alternative at character '"' at line 3 column 23

I'm a JS programmer, fairly new to Apex. I originally planned for it to look like this: 
 
trigger hadFirstMeeting on Task on Lead (before update) {
    for (task t, Lead L : Trigger.new) {
        if (t.Type === "First Time Meeting (Phone or In-Person)") {
            L.Had_First_Time_Meeting__c = true;
        }
    }
}

But I have no idea if this is the correct syntax. I'm having a hard time finding resources for apex that talk about activities. I'm not sure how a lead should relate to a task within the code. And first I need to solve the error anyway. Can anyone offer some help on this? Thanks!

 
Hi, I created a VF page, which is meant to be a pricing calculator. It's divided into a grid using html and css and structured with apex code to allow for merge fields and inline editing. I embedded the page as a section of our opportunity object, where the calculator is supposed to live. When I first go to the page everything looks great, it loads fine and the inline editing works. Then when I click the save button that also sits inside the section the page reloads but the app doesn't. In the section where it lives there's another copy of the same page. AND inside the calculator section of that page is a preview of my source code. I'll paste my code below. It's a lot but I think we can ignore the divs and just look at the very top and very bottom. I think the answer lies there. 

Also below my code, I'll add a screen shot of what it looks like when the page reloads after I save something with inline edit.

I'm a JS Dev but I'm just now learning Apex and Visualforce, so forgive me if this is obvious. But I did about an hour of googling before I got to this point so any help would be greatly apprecited. Cheers.
 
<apex:page standardController="Opportunity" tabStyle="Opportunity">
   
    <apex:stylesheet value="https://fonts.googleapis.com/css?family=Raleway:400,700"/>
    <style>

        h1 {
            color: blue;
            }

        .field_container {
            width: 30%;
            height: 30px;
            display: inline-block;
            margin: 0;
            border: none;
        }
        .field_container_2 {
            width: 22.5%;
            height: 30px;
            display: inline-block;
            margin: 0;
            border: none;
        }
        h3 {
            color: #000;
            margin: 10px 0 0 10px;
            font-size: 1.5em;
            font-family: Raleway;
            }
       .row_container {
            padding-bottom: 20px;
            text-align: center;
            }
      .container {
            width: 80%;
       
            }
            
            .row {
            padding-top: 15px;
            display: block;
            }
           .pbHeader {
           color: #000000 !important;
           font-family: Raleway;
           }
           field_container.inlineEditUndo {
           display: none;
           }
           select {
           max-width: 150px;
           }
           body {
               background-color: #fff !important;
           }
           .opportunityTab .secondaryPalette, .individualPalette .opportunityBlock .secondaryPalette .opportunityTab .listViewport .subNav .linkBar, .opportunityTab .mComponent .cHeader, .opportunityTab .genericTable, .opportunityTab .bSubBlock, .opportunityTab .bPageBlock {
               border: none;
               }
    </style>

  <apex:form >
  <apex:pageBlock mode="inlineEdit">
  <div class="container">
  <!-- Animation Row -->
    <div class="row_container" id="elearning_animation_row">
      <h3>eLearning and Animation</h3>
      <div class="row" id="row_1">
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Minutes_ELA1__c}"/><apex:facet name="header">Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Level_ELA1__c}"/><apex:facet name="header">Level</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock >{!Opportunity.Price_ELA1__c}<apex:facet name="header">Price</apex:facet></apex:pageBlock></div>
      </div>
      <div class="row" id="row_2">      
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Minutes_ELA2__c}"/><apex:facet name="header">Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Level_ELA2__c}"/><apex:facet name="header">Level</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock >{!Opportunity.Price_ELA2__c}<apex:facet name="header">Price</apex:facet></apex:pageBlock></div>
      </div>
      <div class="row" id="row_3">
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Minutes_ELA3__c}"/><apex:facet name="header">Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Level_ELA3__c}"/><apex:facet name="header">Level</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock >{!Opportunity.Price_ELA3__c}<apex:facet name="header">Price</apex:facet></apex:pageBlock></div>
      </div>
    </div>
    <!-- ID Row -->
    <div class="row_container" id="instructional_design_row">
      <h3>Instructional Design</h3>
      <div class="row" id="row_4">
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Minutes_ID1__c}"/><apex:facet name="header">Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Level_ID1__c}"/><apex:facet name="header">Level</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock >{!Opportunity.Price_ID1__c}<apex:facet name="header">Price</apex:facet></apex:pageBlock></div>
      </div>
      <div class="row" id="row_5">      
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Minutes_ID2__c}"/><apex:facet name="header">Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Level_ID2__c}"/><apex:facet name="header">Level</apex:facet></apex:pageBlock></div>
        <div class="field_container"><apex:pageBlock >{!Opportunity.Price_ID2__c}<apex:facet name="header">Price</apex:facet></apex:pageBlock></div>
      </div>
    </div>
    
 <!--  Other Row -->
    <div class="row_container" id="other_row">
      <h3>Other</h3>
      <div class="row" id="row_7">      
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Project_Management__c}"/><apex:facet name="header">Level of Project Management</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock >{!Opportunity.Subtotal_PM__c}<apex:facet name="header">Project Management Price</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Voiceover_Minutes__c}"/><apex:facet name="header">Voiceover Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock >{!Opportunity.Price_VO__c}<apex:facet name="header">Voiceover Price</apex:facet></apex:pageBlock></div>
      </div>
      <div class="row" id="row_8">      
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Assessments_ELM_Creates__c}"/><apex:facet name="header">ELM Assessments</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock >{!Opportunity.Price_Assessments__c}<apex:facet name="header">Assessments Price - Total</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Stock_Photography_Minutes__c}"/><apex:facet name="header">Stock Photography Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock >{!Opportunity.Price_Stock_Photography__c}<apex:facet name="header">Stock Photography Price</apex:facet></apex:pageBlock></div>
      </div>
      <div class="row" id="row_9">      
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Assessments_Client_Provides__c}"/><apex:facet name="header">Client Assessments</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock >{!Opportunity.Price_Assessments__c}<apex:facet name="header">Assessments Price - Total</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Script__c}"/><apex:facet name="header">Script Writing Minutes</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock >{!Opportunity.Price_Scripting__c}<apex:facet name="header">Scripting Price</apex:facet></apex:pageBlock></div>
      </div>
      <div class="row" id="row_10">      
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Line_Item_Expense__c}"/><apex:facet name="header">Line Item Expense</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Line_Item_Expense_Notes__c}"/><apex:facet name="header">Line Item Expense Notes</apex:facet></apex:pageBlock></div>
        <div class="field_container_2"></div>
         <div class="field_container_2" ><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Localization_Quote__c}"/><apex:facet name="header">Localization Quote</apex:facet></apex:pageBlock></div>
      </div>
    </div>
  <!--   Total Row -->
    <div class="row_container" id="total_row">
      <h3>Total</h3>
      <div class="row" id="row_10">
        <div class="field_container_2" ><apex:pageBlock mode="inlineEdit"><apex:outputField value="{!Opportunity.Total_Price__c}"/><apex:facet name="header"><strong>Total Price</strong></apex:facet></apex:pageBlock></div>
        <div class="field_container_2"></div>
        <div class="field_container_2"></div>
        <div class="field_container_2"></div>
      </div>
         <apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="editButton" />
    </div>
      <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" id="saveButton" />
                <apex:commandButton value="Cancel" action="{!cancel}" id="cancelButton" />
                </apex:pageBlockButtons>
</div>
  
    </apex:pageBlock>
   </apex:form>

</apex:page>

Image of Broken Page
I have a large text field on Opportunities that is populated with text formatted by html when the Opportunity is created. I use an apex trigger to do this. The field is a simple questionnare asking details about the opportunity the. The trigger only inserted html code into the field, nothing too fancy.

I created a visualforce email template with the field inside of it hoping I could send a copy of the questionnare to my team. I put the merge field inside of html tags. But when I tested the template my merge field rendered as plain text, with no line breaks, and all of the html tags. 

Here is the visualforce template:
 
<messaging:emailTemplate subject="RTF Email template" recipientType="User"
relatedToType="Opportunity"
 >

<messaging:htmlEmailBody >
<html>
    <h2><em>Hello! I'm a rich text email! And the general project details for this oppy are:</em></h2><br></br><br></br>
    {!relatedTo.General_Project_Details__c} c
    
    <br></br><br></br>
    <p style="{font-size: 32px; color:purple}">Look how rich I am!!!</p>
    
    <div style="border:1px solid green"><p style="font-size: 46px; color: red">I'm the richest text ever!</p></div>
</html>




</messaging:htmlEmailBody>
</messaging:emailTemplate>


And the resulting text looked like this:

User-added image


Normally I would use jQuery to re-encode the html. I saw something about using this command: 
!HTMLENCODE(myobjdata). 

But I've heard I can't use or shouldn't use jQuery in a visualforce email template.

I need that code to render! Can anyone help? Thanks!