• Welcome
  • About
    • Leadership
    • Blogroll
  • Force-Squared Blog
  • Support
    • Knowledge Base
    • Log a Case
  • Contact
  • Welcome
  • About
    • Leadership
    • Blogroll
  • Force-Squared Blog
  • Support
    • Knowledge Base
    • Log a Case
  • Contact
  • Tips and Tricks
  • Configuration
  • Development
You are here: Home / Companies / salesforce.com / 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.

Related

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

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

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

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Is it Dreamforce Yet?

Find out!

Find us on the AppExchange

Mass Update Contacts Pro

Get It Now



Mass Update Contact Address

Get It Now


Recent Posts

  • Lightning Component With Running User Information
  • ChatterBINGO is Now Open-Source
  • Display Only My Role’s Records on a Report
  • Chatter Publisher Actions (Part 2): Object Custom Action
  • Chatter Publisher Actions (Part 1): Create a Record

Post Categories

Salesforce Blogs

  • Embracing the Cloud
  • Force Monkey
  • Vertical Coder
  • Decoding Salesforce
  • Arrowpointe
  • Technology, Coding and Bears… OH MY!
  • TehNerd
  • MK Partners
  • Salesforce Source
  • The Silver Lining

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.com Salesforce1 Salesforce for Google Apps sfdcverse Sites Visualforce Web-to X-Squared On Demand

Like Us on Facebook

Like Us on Facebook

Share This Page

Find Us Online

  • Twitter
  • Facebook
  • LinkedIn
  • Google Plus
  • X-Squared G+ Page
  • 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 5,205 other subscribers

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