• Home
  • About
    • Leadership
    • Partners
    • Blogroll
  • Force-Squared Blog
    • Tips and Tricks
    • Configuration
    • Development
  • Support
    • Knowledge Base
    • Submit a Case
  • Is It Dreamforce Yet?

X-Squared On Demand

Salesforce solutions delivered

  • Home
  • About
    • Leadership
    • Partners
    • Blogroll
  • Force-Squared Blog
    • Tips and Tricks
    • Configuration
    • Development
  • Support
    • Knowledge Base
    • Submit a Case
  • Is It Dreamforce Yet?
  • Tips and Tricks
  • Configuration
  • Development
You are here: Home / Companies / Salesforce / Trigger to help Salesforce for Twitter

Trigger to help Salesforce for Twitter

October 6, 2009 by David Schach Leave a Comment

Salesforce for Twitter is one of the best AppExchange packages I've seen. It fulfills the promise salesforce.com made to bring the Service Cloud to all orgs of all sizes. And it works well.

Though a supplemental/unofficial guide to customizing SFDC for Twitter will be released soon on this site, I wanted to share a trigger I just wrote to add new Leads to a campaign:

Firstly, thank you to Scott Hemmeter at Arrowpointe, who wrote the original code that I customized.

Secondly, you could easily duplicate this trigger and set it to run on the Contact object as well.

Please don't set the trigger to "after update," as in testing, it ran into problems when converting a Lead and merging with a Contact already on the "Twitter" campaign.

trigger AddToTwitterCampaign on Lead (after insert) {

    // List containing each Lead being processed
    list<Lead> theLeads = new list<Lead>(); 
      
    //We only execute if we have a campaign named "Twitter"

    if([SELECT Count() FROM Campaign WHERE name = 'Twitter'] == 1){
        Campaign TC = [SELECT id, name FROM Campaign WHERE name = 'Twitter' LIMIT 1];
        
        for(Lead l:trigger.new) { 
            if (l.leadsource.indexOf('Twitter',0 ) >= 0 ||  l.leadsource.indexOf('Tweet',0 ) >= 0 ){  
                theLeads.add(l); // add lead to the main lead list
                }
            }
      
      // List containing Campaign Member records to be inserted
      list <CampaignMember> theCampaignMembers = new list<CampaignMember>(); 
    
      for (Lead ld:theLeads) {
          CampaignMember cml = new CampaignMember();
          cml.leadid = ld.id;
          cml.campaignid = TC.id;
          theCampaignMembers.add(cml);
        }

     //Insert the list of Campaign Members
      if(!theCampaignMembers.isEmpty()){
        insert theCampaignMembers;
        }
    }
}

The trigger requires that you have a Campaign called "Twitter," but feel free to change that to anything else you'd like.

Don't worry if you have other triggers that add Leads to Campaigns - this can work alongside them, so you can add Leads to as many Campaigns as you'd like.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to email a link to a friend (Opens in new window) Email

Related

Filed Under: Salesforce Tagged With: Apex, Force.com Platform, New Features

← Filtered Lookups, Validation Rules, and Order of Execution Dreamforce Starting Today →

Share Your ThoughtsCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Is it Dreamforce Yet?

Find out!

Find us on the AppExchange

Mass Update Contacts 2.0

Get It Now

Recent Posts

  • Prevent Duplicate Emails on Leads
  • Duplicate Record Item Enrichment and Auto-Deletion Code
  • Lightning Component With Running User Information
  • ChatterBINGO is Now Open-Source
  • Display Only My Role’s Records on a Report

Post Categories

Popular Tags

#df09 #df10 Akismet Apex AppBuilder AppExchange Appirio Astadia Blogs Chatter Configuration Content DreamFactory Dreamforce Eclipse IDE Enterprise Force.com Builder Force.com Platform Google Infowelders Integration Just for fun Lightning New Developments New Features Partner Program PersonAccount Projects Publisher Salesforce Salesforce1 Salesforce for Google Apps sfdcverse Sites Visualforce Web-to X-Squared On Demand

Find Us Online

  • Twitter
  • Facebook
  • LinkedIn
  • RSS

Subscribe

RSS Feed Comments

Subscribe via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 164 other subscribers

Copyright © 2008–2025 X-Squared On Demand · Genesis Framework by StudioPress · WordPress · Log in