SalesForce to FedEx


A lot of my scripts are designed to make shipping easier. I usually use FedEx for my shipping and have discussed in the past filling out the packing forms in Safari in an automated way. I don’t know about you, but this saves me a lot of time. I’ve been using SalesForce a lot of late and one thing I do frequently is ship samples to leads or to existing contacts. Since I don’t (yet) have a safe syncing between my leads and Address Book I wanted a way to use a Service to print a FedEx packing slip for the displayed contact or lead.

This script makes a few assumptions. First it assumes you are logged into FedEx. (If you have an open window in Safari somewhere that’s sufficient) It then assumes the front window is your SalesForce window. It does check to ensure you are in a detail. If you’re not then it doesn’t do anything.

This script uses my paste_to_fedex.py script. I do this since I have a lot of scripts that paste into FedEx and it’s nice to break out that particular functionality. I’ve included paste_to_fedex.py at my permanent location. Just make sure it is in the same location as the below.

What’s nice about the below script is that it demonstrates how to quickly strip desired information from a website without complex Python. You simply use Safari’s do_JavaScript function to get the HTML element with a certain ID. If you have the develop menu enabled on Safari then you have a very powerful HTML analysis tool. You get an outline of all the data in your HTML document. Usually what I do is simply search for the text I’m looking for. Safari will then display in its outline view the element with that text. From there you can swiftly find the element id. If you use SalesForce this is easy to demonstrate. Just go to the lead detail page, go to the “Show Web Inspector” menu. Then do a search elements in the web inspector for lea8_ileinner. You’ll see selected the text

<div id="lea8_ileinner">(111) 111-1111</div>

only with the phone number of the contact you are viewing. You could have searched for your contact name or some other text in order to find that id.

The below simply uses JavaScript in Safari to find an element by that id and then get it’s value (which is the text we are attempting to extract). We then put it in the dictionary that our paste_to_fedex module demands.

SalesForce_to_fed.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/python
 
## SalesForce_to_fed.py
##-------------------------------------------------------------------------
## This takes the lead or contact displayed in the frontmost Safari window
## and creates a FedEx packing slip for that address.
##
## The actual creation of the FedEx packing slip is done by the 
## paste_to_fedex module which must be in the same directory as this 
## script or in the Python path.
##
## Note this only works with American addresses and not Canadian or 
## European.
 
 
import string, sys, time, shutil
from appscript import *
import os
import paste_to_fedex
 
# Discover which view in SalesForce we are at.   Right now we just recognize
# (and return) lead, contact and login.
 
def which_view():        
 
    # get the url for the front most tab/window in Safari
 
    url = app(u'Safari').documents[1].URL.get()
 
    if "login.salesforce.com" in url:
        return "login"
 
    # 00Q indicates a lead directory whereas an extra A indicates a detail
    if "salesforce.com/00QA" in url:
        return "leads"
 
    # 001A indicates a contact  detail
    if "salesforce.com/003A" in url:
        return "contacts"
 
    return url
 
# This reads lead info out of SalesForce into the order_data dict
 
def get_leads():
    S = app('Safari')
    FEdoc = S.documents[0].get() 
 
    order_data = {}
 
    name = S.do_JavaScript("document.getElementById('lea2_ileinner').innerText",  in_=FEdoc)
 
    # make a list with each name element
    names = name.split()
 
    if len(names) > 1:
        order_data['first'] = " ".join(names[:-1])  # Everything but last name
        order_data['last'] = names[-1] # last name
    else:
        order_data['first'] = ""
        order_data['last'] = names[0]
 
 
    phone = S.do_JavaScript("document.getElementById('lea8_ileinner').innerText",  in_=FEdoc)
    order_data['telephone'] = phone
 
    account = S.do_JavaScript("document.getElementById('lea3_ileinner').innerText",  in_=FEdoc)
    order_data['company'] = account
 
    email = S.do_JavaScript("document.getElementById('lea11_ileinner').innerText",  in_=FEdoc)
    order_data['email'] = email
 
    address = S.do_JavaScript("document.getElementById('lea16_ileinner').innerText",  in_=FEdoc)
    al =  address.split("\n")       # separate by line
    order_data['address'] = al[0]   # first line of address in SF
    l = al[1].split(",")   
    order_data['city'] = l[0]  
    order_data['state'] = l[1][1:3] # characters 1-2 of text after ","
    order_data['zip'] = l[1][4:]    # character 4.. of text after ","
 
    return order_data
 
 
# This reads contact info out of SalesForce into the order_data dict
 
def get_contacts():
    S = app('Safari')
    FEdoc = S.documents[0].get() 
 
    order_data = {}
 
    name = S.do_JavaScript("document.getElementById('con2_ileinner').innerText",  in_=FEdoc)
 
    # make a list with each name element
    names = name.split()
 
    if len(names) > 1:
        order_data['first'] = " ".join(names[:-1])  # Everything but last name
        order_data['last'] = names[-1] # last name
    else:
        order_data['first'] = ""
        order_data['last'] = names[0]
 
    phone = S.do_JavaScript("document.getElementById('con10_ileinner').innerText",  in_=FEdoc)
    order_data['telephone'] = phone
 
    account = S.do_JavaScript("document.getElementById('con4_ileinner').innerText",  in_=FEdoc)
    order_data['company'] = account
 
    email = S.do_JavaScript("document.getElementById('con5_ileinner').innerText",  in_=FEdoc)
    order_data['email'] = email
 
    address = S.do_JavaScript("document.getElementById('con19_ileinner').innerText",  in_=FEdoc)
    al =  address.split("\n")       # separate by line
    order_data['address'] = al[0]   # first line of address in SF
    l = al[1].split(",")   
    order_data['city'] = l[0]  
    order_data['state'] = l[1][1:3] # characters 1-2 of text after ","
    order_data['zip'] = l[1][4:]    # character 4.. of text after ","
 
    return order_data
 
def getdata():
    v = which_view()
 
    if v == 'contacts':
        return get_contacts() 
 
 
    if v == 'leads':
        return get_leads()
 
    print "Not Proper Page"
    return None
 
def main():
    order_data = getdata()
 
    if order_data == None:
        return
 
    paste_to_fedex.fill_safari( order_data )
 
if __name__ == '__main__':
 
    if len(sys.argv) == 1:
        main()
 
 
    # change to 0 for success, 1 for (partial) failure
    sys.exit(0)

Related posts:

  1. Displaying a SalesForce Contact’s Emails in Mail
  2. Auto-fill FedEx Forms in Safari
  3. Address Book to Sales Force
  4. Thoughts on SalesForce
  5. Salesforce and the Mac
  6. Copy Email Address Only
  7. Reading Credit Cards with a Swiper
  8. Leaving Salesforce
  1. No comments yet.
(will not be published)