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

Create Contact via AS Weirdness

Though lots of many nights and trial and error with various syntax, I've munged together a set of scripts that help me integrate with Salesforce via Applescript. Many thanks to Simon and all the folks that have put these mac tools together for us to use and and abuse.


I thought I'd had everything licked, but then came upon a discovery - the script that I'd used to take data from Address Book and create accounts and contacts was inserting a "0" where the last name should go in the Contact record.


Here's the code:


tell application "SalesforceScripting"
	set session to login with saved credentials
	set ContactFirstName to "Joe"
	set ContactLastName to "Blow"
	set cont to make SObject
	set type of cont to "Contact"
	cont setField named "FirstName" to ContactFirstName
	cont setField named "LastName" to ContactLastName
	--	cont setField named "AccountId" to NewAccountID
	--	cont setField named "PDS__Primary_Contact__c" with to
	--	cont setField named "Email" to EmailAddy
	set res2 to session create sobjects cont
	set NewContactID to Id of first item of res2
	open location "" & NewContactID
end tell


Here's the results of that script from the AS Log:


tell application "SalesforceScripting"
	login with saved credentials
		--> UserSession id "us_31068"
	make new SObject
		--> SObject id "row_31070"
	set type of SObject id "row_31070" to "Contact"
	setField SObject id "row_31070" named "FirstName" to "Joe"
	setField SObject id "row_31070" named "LastName" to "Blow"
	create UserSession id "us_31068" sobjects SObject id "row_31070"
		--> {SaveResult id "sr_31071" of UserSession id "us_31068"}
	get Id of SaveResult id "sr_31071" of UserSession id "us_31068"
		--> "003E0000005uds7IAA"
	open location ""
end tell


If I hard code the ContactLastName, like:


cont setField named "LastName" to "Blow"

 the log returns an identical result, and the record is written properly to But if "LastName" is written with a variable, I get a big fat ZERO.


Any clues?





That's odd, I got the same results, although it appears to depend on the length of the string, it only happens if its 4 characters long, which makes me suspect there some weird applescript thing going on (everything in applescript is done with 4 character codes). I'll see if i can work out whats going on.


So digging some more, the appleevent the app receives seems correct, but somewhere in the cocoa scripting layer (this is the apple framework that translates the raw appleevent into a cocoa call into my code) is getting confused about the type. (this first item is the raw apple event, and you can see the correct string value, the last log shows what cocoa scripting passes me, and its already messed it up into a 0. I'll see if i can find any more info on it.




  event data:

    { 1 } 'aevt':  - 3 items {

      key 'FEld' - 

        { 1 } 'utxt':  16 bytes {



      key 'VAlu' - 

        { 1 } 'utxt':  8 bytes {



      key '----' - 

        { 1 } 'obj ':  - 4 items {

          key 'form' - 

            { 1 } 'enum':  4 bytes {

              'ID  '


          key 'want' - 

            { 1 } 'type':  4 bytes {



          key 'seld' - 

            { 1 } 'utxt':  12 bytes {



          key 'from' - 

            { -1 } 'null':  null descriptor





2011-07-10 21:00:09.873 SalesforceScripting[8387:a0f] Command: Salesforce Scripting Suite.setField

Direct Parameter: <NSUniqueIDSpecifier: sobjects with an ID of "row_12">

Receivers: <NSUniqueIDSpecifier: sobjects with an ID of "row_12">

Arguments:     {

        fieldName = LastName;

        value = 0;



Incredible discovery, Simon. I barely know how to write Applescript, that Cocoa code you posted is true gobblety-**bleep** for sure. Congratulations on knowing what all that means :robotwink:


Funny that all of my tests were on 4-character sample records - now I see that the script behaves the same way if the First Name is 4 characters. I'm almost crosseyed from looking at this simple AS code over and over for hours.


I'm assuming that any 4-character value passed to an SObject though the SalesforceScripting application will behave the same way.


Thanks again for your efforts Simon. 


This is intruging... Would typecasting the values to Strings work?


Sorry if my question is silly.


I don't think that'll help, as the appleevent already has the type as a string, this seems to be a bug related to cocoa scriptings support for arguments of multiple types. I think the only real fix will be to change/augment the applescript interface so that there are different methods for each type.


Okay. That make sense. Thanks.