+ Start a Discussion
biggabigga 

SF Scripting " ' " Apostrophe Error

I've munged together an applescript to create an opportunity in Salesforce, assigning a contact and contact role.

-- beginning of applescript


tell application "SalesforceScripting"
activate
set session to login with saved credentials

-- request a partial account name, then present a list of accounts for the opportunity to relate to

set startsWith to text returned of (display dialog "Search Accounts" default answer "Enter Partial Account Name")
set res to query session soql "select id, name from account where name like '%" & startsWith & "%'"

set L to (get every SObject of res)
set MyAcctList to {}
repeat with row in L
set end of MyAcctList to row value of "Name"
end repeat
try
if (count of items of MyAcctList) is greater than or equal to 1 then
set AccountName to (choose from listMyAcctListwith prompt "Which Account?" OK button name "This one!" cancel button name "Cancel" without multiple selections allowed) as list
else if (count of items of MyAcctList) is equal to 0 then
beep
display dialog "No Such Account Exists" buttons {"OK"} default button 1
return
end if
on error errMsg number errnum
display dialogerrnum
end try

--get the associated Account ID from the Account Name

set res2 to query session soql "select id, name from account where name = '" & AccountName & "'"
set L to (get every SObject of res2)
repeat with row in L
set AcctId to rowvalueof "Id" -- the Salesforce.com Acct Id
end repeat

--get the associated Contact Name from the Account Id you just selected

set res3 to query session soql "select id, name from contact where AccountId = '" & AcctId & "'"
set Contacts to (get every SObject of res3)
set ContList to {}
repeat with row in Contacts
set end of ContList to row value of "Name"
end repeat

-- present a list of related contacts for the account, for the particular opportunity

try
if (count of items of ContList) is greater than or equal to 1 then
set ContactName to (choose from listContListwith prompt "Which Contact?" OK button name "This one!"cancel button name "Cancel" without multiple selections allowed) as text
else if (count of items of ContList) is equal to 0 then
beep
display dialog "No Contacts Found" buttons {"OK"} default button 1
return
end if
on error errMsg number errnum
display dialogerrnum
end try

--get the associated Contact ID from the Contact Name

set res4 to query session soql "select id, name from contact where name = '" & ContactName & "'"
set L to (get every SObject of res4)
repeat with row in L
set ContactId to rowvalueof "Id" -- the Salesforce.com Acct Id
end repeat

--then build the Opportunity in Salesforce.com

delay 3

set opp to makeSObject
set type of opp to "Opportunity"
opp setField named "Name" to CustName & " " & OrderNum -- required
opp setField named "AccountId" to AcctId as text -- required
opp setField named "StageName" to "Prospecting" -- required
oppsetFieldnamed "CloseDate" toCloseDate-- as date -- required

set res5 to sessioncreatesobjectsopp
Id of first item of res5
set NewOppID to Id of first item of res5 as text

display dialogNewOppID

--and Assign a Contact & ContactRole

set opprole to makeSObject
set type of opprole to "OpportunityContactRole"
opprole setField named "OpportunityId" to NewOppID
opprole setField named "ContactId" to ContactId
opprole setField named "Role" to "Decision Maker"
set res6 to sessioncreatesobjectsopprole

end tell

-- end of applescript

However if the contact's name as an apostrophe (O'Leary) then the script fails. I also still have the issue when creating a contact with a 4-character name (Marc) the entry in SF becomes "0"