function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JacksonPJacksonP 

Can anyone help with Perl interface to SF

I can't seem to get past the basics, spending lots of time on this. Just want to get one query to work with some pointers to the correct documentation and I'm off doing something useful.

Starting with:



use Salesforce;

my $user="bubba\@gmail.com";
my $pass="SusaniscoolRfA44QuF7I97QlJr0KEQDV2js";

my $service = new Salesforce::SforceService;
my $port = $service->get_port_binding('Soap');
$port->login('username' => $user,'password' => $pass)
|| die "Could not login to salesforce.com";

my $result = $port->query("select FirstName, LastName from Contact",100);
print "$result\n";
print "We got " . $result->valueof('//queryResponse/result/size') . " back.\n";



Returns this:



C:\Perl Files\SFDC>test.pl
1
Use of uninitialized value in concatenation (.) or string at C:\Perl Files\SFDC
test.pl line 20.
We got back.



Now this makes me quite curious where the //queryResponse/result/size is coming from. Is there a doc that explains these elements? I have not located yet.

This does not work either:


foreach my $elem ($result->valueof('//queryResponse/result/records'))
{
printf "%s, %s\n", $elem->{Id}[0],$elem->{AccountId};


And I really don't mean to be negative demonstrating all that does not work but apparently Perl is still quite challenged here.

At the end of the day, if one exists. I would like to simply insert some leads and associate a task with them so the sales folks get a nice alert. I can do this with the loader and spreadsheets but it is a pretty manual process. If you can point me to some working scripts, pretty much -anything- that actually works I would be most grateful.

I must say this is the coolest and most flexible service I have encountered, just need a little help from my friends.

Regards,

Jacksonp2008@gmail.com
EricVlachEricVlach
download and install the WWW::Salesforce module - it will make your life a lot easier.

I've been using perl to access the SF API for a while now, let me know if you have specific questions.

Try this:

#!/usr/bin/perl
use WWW::Salesforce
$sf = WWW::Salesforce-> login ('username' => 'your_username', 'password' => 'your_password');
$sf_query = "select FirstName, LastName from Contact";
$r = $sf -> query('query' => $sf_query, 'limit' => 100);
print "We got " .  $r->valueof('//queryResponse/result/size') . " back.\n";
exit 0;


To see the 'behind-the-scenes' of the soap calls, (where the //queryResponse/result/size is 'coming from'), edit the Salesforce.pm module, (usually somewhere in /usr/lib/perl) and remove the semi colon and the pound sign after use SOAP::Lite -- it will show debug info when you run the perl script from the command prompt.

tonyktonyk

Something I ran into that wasn't immediately obvious was the need to use queryMore for queries that return lots of rows, or attachment queries that only return one row at a time.   Here's some code that might be useful (I'm selecting a field from a custom object):

$query = 'select Name from EmailMaster__c order by Name asc' ;

$rs = $sforce->query(query => $query) ;

# Grab metadata about our query results

$rdone = $rs->valueof('//queryResponse/result/done') ;
$reccount = $rs->valueof('//queryResponse/result/size') ;
$locator = $rs->valueof('//queryResponse/result/queryLocator') ;
$api_recordtype = '//queryResponse/result/records' ;

while ($reccount > 0) {

  # process the recs we have
 
  foreach $record ($rs->valueof($api_recordtype) ) {
    push @templates, $record->{Name} ;
  }
 
  $reccount = 0 ;
 
  # it's possible that we still have more records to fetch.  Check and call queryMore if required.
 
  if ($rdone eq 'false') {
 
    # Yes, we have to make additional queries.
   
    $rs = $sforce->queryMore(queryLocator => $locator) ;
   
    # Grab the new metadata using queryMoreResponse
   
    $rdone = $rs->valueof('//queryMoreResponse/result/done') ;
    $reccount = $rs->valueof('//queryMoreResponse/result/size') ;
    $locator = $rs->valueof('//queryMoreResponse/result/queryLocator') ;
    $api_recordtype = '//queryMoreResponse/result/records' ;   
  }
 
}

EricVlachEricVlach
http://search.cpan.org/~capoeirab/WWW-Salesforce-0.090/lib/WWW/Salesforce/Simple.pm module WWW::Salesforce::Simple has the query more already implemented, for a cleaner solution.
tonyktonyk
Thanks!  This is definitely more hassle-free!