#!/usr/bin/perl -w use strict; use DBI; use WWW::Salesforce::Simple; use Date::Parse; use Time::Piece; # Authenticate with the Salesforce API based on command line parameters. my $sforce = WWW::Salesforce::Simple->new( 'username' => $ARGV[0], 'password' => $ARGV[1] ); # Figure out the last time we updated Salesforce my $result = $sforce->do_query("Select CreatedDate From Lead Where LeadSource = 'Blog'"); my $max_date = '1970-01-01'; foreach my $row (@$result) { $max_date = $row->{CreatedDate} if $row->{CreatedDate} gt $max_date; } # Connect to the database for your blog. # You'll need to know the name of the database, the username, and the password. my $dbh = DBI->connect("dbi:mysql:$ARGV[2]", $ARGV[3], $ARGV[4]); my $sth = $dbh->prepare( "SELECT comment_author, comment_email, comment_url, comment_text, comment_created_on FROM mt_comment WHERE comment_created_on > ? AND comment_junk_status != -1"); $sth->execute(Time::Piece->new(str2time($max_date))->strftime("%F %T")); while (my $comment = $sth->fetchrow_hashref()) { next unless $comment->{'comment_email'} && $comment->{comment_author}; my %lead; my $result = $sforce->do_query("select Id from Lead where Email = '$comment->{'comment_email'}'"); $lead{id} = $result->[0]->{'Id'} if $result->[0]; $lead{"type"} = "Lead"; $lead{"Email"} = $comment->{"comment_email"}; $lead{"LeadSource"} = "blog"; $lead{"Company"} = "NA"; $lead{"Website"} = $comment->{"comment_url"}; my ($first, $last) = split(/ /, $comment->{"comment_author"}, 2); unless ($last) { $last = $first; $first = ""; } $lead{"FirstName"} = $first if $first; $lead{"LastName"} = $last if $last; if ($lead{id}) { $result = $sforce->update(%lead); } else { $result = $sforce->create(%lead); if ($result->result->{"success"} eq "false") { print $result->result->{errors}->{message} . "\n"; } else { my %task = ( type => 'Task', WhoId => $result->result->{id}, Subject => "Contact blogger", Description => $comment->{comment_text}, ); $sforce->create(%task); } } }