Address Book to Sales Force


Here’s an other little script I wrote for using SalesForce.com. We take the selected contacts in Address Book, open up a new contact entry in SalesForce and then fill in the data. Now I could have made this a bit more advanced, copying in all the emails, addresses, phone numbers and so forth that are possible. But I rarely use those. So I can handle copying and pasting those or just modifying the script if they become more common.

Once again Apple’s optional develop menu in Safari is invaluable. I used to do all this with plugins for FireFox but I now actually prefer analyzing web pages in Safari. All you have to do is find the form name and then the id of the field you want to fill in. You then use JavaScript in Safari to set the value like this:

Safari.do_JavaScript("document.forms['form_name']['field_id'].value = \"" + myvalue + "\"", in_=FEdoc)

I’ve used this technique a lot in various scripts. So long as there aren’t logic bugs on the page (i.e. using an identical id multiple times) this approach works fantastic and is a rapid way to fill in forms.

As is typically the case with scripts like this for me I don’t automate the saving of the page, although I could. I prefer a “sanity check” before doing anything on a web page. But it really wouldn’t be hard to automate everything. You’d just get unexpected results if some unexpected data appeared. I prefer to avoid such happenings.

If you’ve seen our Address Book to FedEx script then a lot of this will be familiar.

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
#!/usr/bin/python
 
## address_to_SF.py
##-------------------------------------------------------------------------
## Puts the selected addresses from Address Book as SalesForce contacts
 
from appscript import *
import sys, time
 
def check_Safari_done():
 
    while True:
        try: 
            s = app(u'/System/Library/CoreServices/System Events.app').application_processes[u'Safari'].windows[1].groups[1].static_texts[1].name.get(resulttype=k.unicode_text)
            break
        except:
            time.sleep(1)
            continue
 
    while True:
        try:
            s = app(u'/System/Library/CoreServices/System Events.app').application_processes[u'Safari'].windows[1].groups[1].static_texts[1].name.get(resulttype=k.unicode_text)
            if s[0:7] == "Loading":
                continue
            if s[0:10] == "Contacting":
                continue
            break
        except:
            time.sleep(1)
            break
 
    s = app(u'/System/Library/CoreServices/System Events.app').application_processes[u'Safari'].windows[1].groups[1].static_texts[1].name.get(resulttype=k.unicode_text)
 
 
def fillSF( ad ):
    Safari = app(u'/Applications/Safari.app')
    Safari.make(new=k.document)
    Safari.documents[0].URL.set(u'https://na7.salesforce.com/003/e?retURL=%2F003%2Fo')
 
    check_Safari_done()
    time.sleep(1)
 
    FEdoc = Safari.documents[0].get() 
 
    Safari.do_JavaScript("document.forms['editPage']['con10'].value = \"" + ad["phone"]+ "\"", in_=FEdoc)
    Safari.do_JavaScript("formatPhone(document.forms['editPage']['con10'])",  in_=FEdoc)
 
 
    Safari.do_JavaScript("document.forms['editPage']['name_firstcon2'].value = \"" + ad["first"]+ "\"", in_=FEdoc)
 
    # last name is an index key in SF so we have to put something in here - we can't leave it blank
 
    if ad["last"] == "":
        ad["last"] = "UNKNOWN"
 
    Safari.do_JavaScript("document.forms['editPage']['name_lastcon2'].value = \"" + ad["last"]+ "\"", in_=FEdoc)
 
 
    Safari.do_JavaScript("document.forms['editPage']['con15'].value = \"" + ad["email"]+ "\"", in_=FEdoc)   
 
    # company is an index key in SF so we have to put something in here - we can't leave it blank
 
    if ad["company"] == "":
        ad["company"] = "UNKNOWN"
 
    Safari.do_JavaScript("document.forms['editPage']['con4'].value = \"" + ad["company"]+ "\"", in_=FEdoc)   
 
    Safari.do_JavaScript("document.forms['editPage']['con19street'].value = \"" + ad["street"]+ "\"", in_=FEdoc)   
    Safari.do_JavaScript("document.forms['editPage']['con19city'].value = \"" + ad["city"]+ "\"", in_=FEdoc) 
    Safari.do_JavaScript("document.forms['editPage']['con19state'].value = \"" + ad["state"]+ "\"", in_=FEdoc) 
    Safari.do_JavaScript("document.forms['editPage']['con19zip'].value = \"" + ad["zip"]+ "\"", in_=FEdoc) 
 
 
def get_address():
    AB = app(u'/Applications/Address Book.app')
    addresses = AB.selection.get()
 
    for a in addresses:
        ad = {}
 
        if a == None:
            continue
 
        if a.company.get() == False:
            ad["company"] = ""
        else:
            ad["company"] = a.name()
 
        print ad["company"]
 
 
        if a.first_name() is not None:
            if a.first_name() == k.missing_value:
                ad["first"] = u""
            else:
                ad["first"] = a.first_name()
        else:
            ad["first"] = u""
 
        if len ( a.emails() ) >0 :
            if a.emails()[0].value.get() is not None:
                if a.emails()[0].value.get() == k.missing_value:
                    ad["email"] = u""
                else:
                    ad["email"] = a.emails()[0].value.get()
            else:
                ad["email"] = u""
 
        if a.last_name() is not None:
            if a.last_name() == k.missing_value:
                ad["last"] = u""
            else:
                ad["last"] =  a.last_name() 
        else:
            ad["name"] = u""            
 
 
        if len(a.phones()) >0:
            ad["phone"] = a.phones()[0].value()
        else:
            ad["phone"] = ""
 
        if len( a.addresses() ) > 0:
            if len((a.addresses())[0].street()) > 0:
                ad["street"] = (a.addresses())[0].street()
                ad["city"] = (a.addresses())[0].city()
                ad["state"] = (a.addresses())[0].state()
                ad["zip"] = (a.addresses())[0].zip()
            else:
                ad["street"] = ""
                ad["city"] = ""
                ad["state"] = ""
                ad["zip"] = ""       
        else:
            ad["street"] = ""
            ad["city"] = ""
            ad["state"] = ""
            ad["zip"] = ""         
 
        fillSF( ad )
 
 
 
if __name__ == '__main__':
 
    get_address()
 
    # change to 0 for success, 1 for (partial) failure
    sys.exit(0)

Related posts:

  1. Address Book to Numbers
  2. Proximity Searches in Address Book
  3. SalesForce to FedEx
  4. Address Book Selection as Text on Clipboard
  5. Auto-fill FedEx Forms in Safari
  6. Displaying a SalesForce Contact’s Emails in Mail
  7. Proximity Address Search Script Revisited
  8. Reading Credit Cards with a Swiper
  1. #1 by john wilson on 2010/03/31 - 12:52 pm

    Really stupid question – do I just copy and paste this into the applescripts editor? I tried, ran it but hit an error:

    Syntax error, expected end of line, etc. but found “from”

  2. #2 by Clark on 2010/03/31 - 7:28 pm

    No. It is written in Python and not Applescript. You need to first install appscript. At the command line type

    sudo easy_install appscript

    Then you need to open up a text editor and create the python script. You can paste the above into your text editor.

    Most of my scripts are in Python because the Python libraries are much more robust than Applescript and because as a language Applescript is pretty bad. There is a Python tutorial on the Internet you might want to check out. You also might want to look at this blog post.

    If there is interest I might put up a nice tutorial for using Python for scripting.

(will not be published)