+ Start a Discussion
biggabigga 

Create New Account/Contact/Opportunity via Applescript and SalesforceScripting App

I was thrilled to find this:

 

tell application "SalesforceScripting"
	set mySession to login username "myemail@mydomain.com" password "passwordtokenhere"
	
	--  Create a new Account in Salesforce
	
	set acc to make SObject
	set type of acc to "Account"
	acc setField named "Name" to "3 New Test Account"
	acc setField named "PDS__QB_Billing_Address__c" to "3 New Test Account
John Doe
123 Main Street
Arlington, TX 75007"
	acc setField named "Phone" to "(888) 555-6666"
	set res to mySession create sobjects acc
	Id of first item of res
	
end tell

 


And It works like a champ. I've spent the last couple of hours scouring the net and the script library trying to find out how to use something similar, or to add to it, so that I can create an Opportunity and Contact by adding some instructions to this.

 

I'm stuck. I guess I don't know enough about Apex and the SF system yet to read what needs to be in the script.

 

Any insight as to how to add Accounts/Contacts/Opportunities at the same time would be greatly appreciated.

Best Answer chosen by bigga
SuperfellSuperfell
You'll need to examine the rest of the properties of the save result (res5 in your script). You should be able to use the success property to determine if the oppty was created or not, and message to tell you the error in the event it wasn't. so add something like
set errMsg to Message of first item of res5
set NewOppId to Id of first item of res5
display display NewOppID + " " + errMsg
​

 

All Answers

biggabigga

More on the Add Account script - this part:

 

acc setField named "Phone" to "(888) 555-6666"

 works as it should when the phone number is in the format of "(888) 555-6666" - I get phone numbers if various formats, so I have another part of script that reformats the number to be just  a string of numbers like "8885556666"

 

acc setField named "Phone" to formattedNumber

 But after adding the account via the script, I get a weird calculated result in that field instead of the phone number

 

 

biggabigga

Progress! Using the applescript snippets on PocketSoap.com, I got this to reliably create a New Account and Related Contact via applescript:

 

-- Make and Account and Primary Contact

tell application "SalesforceScripting"
	set session to login with saved credentials
	
	-- this is for the New Account
	
	set acc to make SObject
	set type of acc to "Account"
	acc setField named "Name" to "My New Account 17"
	acc setField named "PDS__QB_Billing_Address__c" to "Acct Name
	Contact Name
	Billing Street
	City, ST 88888"
	acc setField named "Phone" to "(222) 444-6666"
	
	set res to session create sobjects acc
	set AccountID to Id of first item of res
	
	-- this is for the New Related Primary Contact
	
	set cont to make SObject
	set type of cont to "Contact"
	cont setField named "LastName" to "Smith" as text
	cont setField named "FirstName" to "Jonesy" as text
	cont setField named "AccountId" to AccountID
	cont setField named "PDS__Primary_Contact__c" with to
	cont setField named "Email" to "fakeemail@email.com"
	
	--	set crole to second SObjectDescribe in session whose name contains AccountID
	--	set RoleField to name of fourth FieldDescribe in crole -- to "Decision Maker"
	--	cont setField named RoleField to "Decision Maker"
	--	cont setfield fourth FieldDescribe "Role" of second SObjectDescribe in session whose name contains "Account" of Accountid to "Decision Maker"
	
	set res2 to session create sobjects cont
	set ContactID to Id of first item of res2
end tell

 The commented lines near the end are my feeble attempt at adding a Contact Role to this fresh new contact. I'm using the SFFieldDescribes and FieldDescribe snippets to determine the names/positions of these fields. Nothing I try works. 

 

tell application "SalesforceScripting"
	set session to login with saved credentials
	--	set acc to first SObjectDescribe in session whose name is equal to "Account"
	--	set cont to first SObjectDescribe in session whose name is equal to "Contact"
	--	set opp to first SObjectDescribe in session whose name is equal to "Opportunity"
	set crole to second SObjectDescribe in session whose name contains "Account"
	name of crole -- as list
	--	name of every FieldDescribe in acc -- whose name is equal to "OwnerId"
	--	name of every FieldDescribe in cont -- whose name is equal to "OwnerId"
	--	name of every FieldDescribe in opp -- whose name is equal to "OwnerId"
	name of fourth FieldDescribe in crole -- whose name is equal to "OwnerId"
	--label of f as list
end tell

The script above returns:

 

	get name of SObjectDescribe id "od_9522" of UserSession id "us_9519"
		--> "AccountContactRole"
	get name of FieldDescribe 4 of SObjectDescribe id "od_9522" of UserSession id "us_9519"
		--> "Role"

 But I can't figure out the correct syntax to add a Contact Role to the new Contact in the Account/Contact creation script 

 

 

 

 

 

 

SuperfellSuperfell

You would add a contact role in the same fashion that you created the related contact. If you're struggling to discover field names etc, you might want to look at SoqlX which will let you explore your salesforce schema.

biggabigga

Thanks for the reply Simon. Standing back and taking another look at the records/fields with the SoqlXplorer made it clearer. This bit of code appended to the above assigns the Contact Role to the just created contact and makes it the Primary contact:

 

	-- this makes the New Primary Contact the "Decision Maker"
	
	set crole to make SObject
	set type of crole to "AccountContactRole"
	crole setField named "AccountId" to NewAccountID 
	crole setField named "ContactId" to NewContactID 
	crole setField named "Role" to "Decision Maker"
	crole setField named "IsPrimary" with to
	
	set crole to session create sobjects crole

 Still working on

 

 - getting SF to read 7778887777 as a phone number and not a calculation

 - Adding an opportunity using an account lookup 

 

For the lookup part, I know that MailDrop can do this - how can I look up an account by typing a partial name into a dialog box and then choosing an account to assign a new opportunity to?

biggabigga

I'm completely stuck on this: I've tried every trick I know, and none of them work. Here's where I am:

 

set AcctLookup to "pollack"

tell application "SalesforceScripting"
	
	-- this part opens up a salesforce session
	
	set session to login with saved credentials
	
	set AcctObj to first SObjectDescribe in session whose name is equal to "Account"
	set AcctField to name of every FieldDescribe in AcctObj whose name is equal to "Name"
	set ThisRecord to query session soql "select name from account order by createdDate desc" 
	set AccountName to (get every SObject of ThisRecord) whose (value of AcctField) contains AcctLookup 
		
end tell

I've been staring at Soql Xplorer all day - I know the answer is staring me in the face, but I can't see it. 

SuperfellSuperfell

You want to do the lookup/filter in the query, something like

 

set res to query session soql "select name,id from account where name like '" + startsWith + "%'"
set L to (get every SObject of res)
repeat with row in L
	Id of row -- the Salesforce.com record Id
	type of row -- the SObject type, e.g. Account
	row value of "Name"	-- get the Name field value
end repeat

 

biggabigga

Progress! The "+" characters need to be "&" for that code to work.

 

set startsWith to "oak"

tell application "SalesforceScripting"
	set session to login with saved credentials
	
	set res to query session soql "select id, name from account where name like '" & startsWith & "%'"
	set L to (get every SObject of res)
	repeat with row in L
		Id of row -- the Salesforce.com record Id
		type of row -- the SObject type, e.g. Account
		set AcctList to (row value of "Name") as list -- get the Name field value
	end repeat
	set ThisAcct to (choose from list AcctList with prompt "Which Account?" OK button name "This one!" cancel button name "Cancel" without multiple selections allowed) as string
end tell

 As written, the code returns the first record it finds. I'm stuck in the middle of the repeat, I think. 

 

For example, I have multiple accounts containing 'oak' - "Red Oak", "Mrs. Oakes", "Oakleaf", etc. I need to show a list to choose from. 


biggabigga

Persistence pays... this part will look up an account by searching the first few letters of the account name:

 

set startsWith to text returned of (display dialog "Search Accounts" default answer "Enter Partial Account Name")

tell application "SalesforceScripting"
	set session to login with saved credentials
	
	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
		Id of row -- the Salesforce.com record Id
		type of row -- the SObject type, e.g. Account
		row value of "Name" -- get the Name field value
		set end of MyAcctList to row value of "Name"
	end repeat
	MyAcctList
	tell application "Finder"
		activate
		set ThisAcct to (choose from list MyAcctList with prompt "Which Account?" OK button name "This one!" cancel button name "Cancel" without multiple selections allowed) as list
	end tell
	ThisAcct as text
end tell

 

I'd like to be able to search the entire account name, not just the first part of the name. This line here restricts the search to "startswith"

 

set res to query session soql "select id, name from account where name like '" & startsWith & "%'" 

 I've tried "name from account where name contains" and a few other iterations, however I can't get past this part

 

biggabigga
I've had this working reliably for a few years now:

 - I can parse the info from my Apple Contacts and create a new account and contact in SF with Applescript and SalesForceScripting.
 - I can create a new opportunity for any account using SalesForceScripting.

That is, until today. The Account and Contact creating is working OK, but the script is simply failing to create an opportunity. I've been staring at this for hours until my eyes are crossed. The weird thing is that sometimes it works OK - and creates an opportunity. Just not every time. And I can't for the life of me figure out how it's failing.

Here's the code:
 
tell application "SalesforceScripting"
	activate
	
	set session to login with saved credentials
	
	set opp to make SObject
	set type of opp to "Opportunity"
	opp setField named "Name" to "OppName" -- required
	opp setField named "AccountId" to "001E000001KpsbvIAB" as text -- required
	opp setField named "StageName" to "Prospecting" -- required
	opp setField named "CloseDate" to CloseDate -- as date -- required
	opp setField named "Opp_Name_20__c" to "Opp Name" as text
	opp setField named "BF_PONumber__c" to "111111" as integer
	opp setField named "Pricebook2Id" to "01sE0000000HScEIAW"
	opp setField named "Description" to "Description Here"
	
	set res5 to session create sobjects opp
	Id of first item of res5
	set NewOppID to Id of first item of res5 as string
	
	display dialog NewOppID  -- See if it worked. It's a blank dialog if no opportunity created
	
end tell

The NewOppID should be a long string (similar to 006E000000bPi7o)  that I can append to a salesforce.com url to show the opportunity-- like this

my.salesforce.com/006E000000bPi7o

​It just isn't working today, and I can't figure out why - no updates to the OS, or applescript software in months. Currently on Yosemite 10.10.5

 
SuperfellSuperfell
You'll need to examine the rest of the properties of the save result (res5 in your script). You should be able to use the success property to determine if the oppty was created or not, and message to tell you the error in the event it wasn't. so add something like
set errMsg to Message of first item of res5
set NewOppId to Id of first item of res5
display display NewOppID + " " + errMsg
​

 
This was selected as the best answer
biggabigga
Thanks TONS for the reply. The first attempt was working, but subsequent attempts were failing because I was attempting to create a duplicate record. One of my custom fields has to be unique. The error tracking line you had me put in helped me figure this out.

Now I feel dumb making you do code on a Saturday. Have a pint on me when you get to the pub. Thanks so much for your help.