+ Start a Discussion
biggabigga 

Creating Opportunities with SalesforceScripting and AS

This script has issues:

 

tell application "SalesforceScripting"
	activate
	
	set CustName to "Smithtown"
	set OrderNum to "123456"
	set session to login with saved credentials
	
	set opp to make SObject
	set type of opp to "Opportunity"
	
	(* THESE are REQUIRED Opportunity Fields - the Opportunity is not created if these 4 objects don't have values *)
	
	opp setField named "Name" to "blahblah2" -- text returned of (display dialog "Name This" default answer "") -- as text -- will be filled by a variable
	opp setField named "StageName" to "Prospecting"
	opp setField named "CloseDate" to "2011-07-10"
	
	(* THESE are Custom Opportunity Fields - anything written via applescript, and the "Create SObjects" instruction fails - no opportunity created *)
	
	--	opp setField named "Opp_Name_20__c" to "123456 BLAH BLAH" -- Eventually will filled with these variables: OrderNum & " " & CustName
	--	opp setField named "PDS__PO_Number__c" to "123456"  -- variable OrderNum
	--	opp setField named "PDS__Document_Number__c" to "123456" -- variable OrderNum
	--	opp setField named "PDS__Invoice_Number__c" to "123456" -- variable OrderNum
	
	set res5 to session create sobjects opp
	Id of first item of res5
	set NewOppID to Id of first item of res5 as text
	
end tell

 

Inserting anything into the custom opportunity fields causes the script to fail. I can understand having "required" fields, like name, but I should be able to fill custom fields with my own data.

 

SuperfellSuperfell

What error message do you get, custom fields should work fine.

biggabigga

Sorry, I guess I wasn't thorough in my testing  These custom fields are from Pervasive Datasynch - it's an appexchange app that synchs opportunities to invoices in Quickbooks. 

 

I'd like to insert data into these three fields in a New Opportunity:

 

- PDS__PO_Number__c 

- PDS__Document_Number__c

- PDS__Invoice_Number__c

 

The fourth custom field is my own - it is filled with the Opportunity Name (CustName+Number), but in reverse (Number+CustName)

 

Now after review, I see that the only field that works consistently is PDS__PO_Number__c - when any of the other fields are un-commented, I get:

 

Result:
"missing value"

 after the 

 

	set NewOppID to Id of first item of res5 as text

 line. 

 

However, I am able to write data to the PDS__PO_Number__c field when that's the ONLY custom field to write to.

SuperfellSuperfell

the result object has properties success, message & statusCode, if success is false, then message & statusCode will report the reason why the create call failed.

biggabigga

I don't get those messages in my Applescript Editor. Here's the event log of one that succeeded:

 

tell application "SalesforceScripting"
	activate
	login with saved credentials
		--> UserSession id "us_69"
	make new SObject
		--> SObject id "row_71"
	set type of SObject id "row_71" to "Opportunity"
	setField SObject id "row_71" named "Name" to "Smithtown 123456"
	setField SObject id "row_71" named "StageName" to "Prospecting"
	setField SObject id "row_71" named "CloseDate" to "2011-07-10"
	setField SObject id "row_71" named "PDS__PO_Number__c" to "123456"
	create UserSession id "us_69" sobjects SObject id "row_71"
		--> {SaveResult id "sr_72" of UserSession id "us_69"}
	get Id of SaveResult id "sr_72" of UserSession id "us_69"
		--> "006E0000002eYMVIA2"
	get Id of SaveResult id "sr_72" of UserSession id "us_69"
		--> "006E0000002eYMVIA2"
end tell
Result:
"006E0000002eYMVIA2"

 Here's one that failed:

 

tell application "SalesforceScripting"
	activate
	login with saved credentials
		--> UserSession id "us_61"
	make new SObject
		--> SObject id "row_63"
	set type of SObject id "row_63" to "Opportunity"
	setField SObject id "row_63" named "Name" to "Smithtown 123456"
	setField SObject id "row_63" named "StageName" to "Prospecting"
	setField SObject id "row_63" named "CloseDate" to "2011-07-10"
	setField SObject id "row_63" named "PDS__Document_Number__c" to "123456"
	create UserSession id "us_61" sobjects SObject id "row_63"
		--> {SaveResult id "sr_64" of UserSession id "us_61"}
	get Id of SaveResult id "sr_64" of UserSession id "us_61"
		--> missing value
	get Id of SaveResult id "sr_64" of UserSession id "us_61"
		--> missing value
end tell
Result:
"missing value"

The only difference that I can tell is that the successful one has a new Opportunity ID, and the failed one does not. Unless I'm missing something totally obvious. 

 

 

 

SuperfellSuperfell

You'll have to change your script to look at the success & message properties. Id will be missing on failed creates.

biggabigga

Sorry to be a dunce... change the script how?

SuperfellSuperfell

something like

if success of res5 then

--what ever you currently do with the id

else

message of res5

end fi

biggabigga

That's way over my dumb applescript head, Simon. I've got a post on the AS board looking for help in adding that to it to capture the errors.

 

Stay tuned...

biggabigga

I haven't come up with any "if success equals false then message" syntax, so I'm still stuck with entering data into these custom fields:

 

- Opp_Name_20__c 

- PDS__PO_Number__c 

- PDS__Document_Number__c

- PDS__Invoice_Number__c

 

However, I have discovered this: if the first character starts with a number ( 0 1 2 3 4 5 6 7 8 9) then the script fails. If it's a letter, or an asterisk(*) single quote(') double quote (") backslash (\) the script succeeds!

 

Is this a quirkiness of Salesforce.com, or the SalesforceScripting  app? 

SuperfellSuperfell

There's something weird going on with the success flag, which i'll take a look at tonight, in the intermin, you can just do

message of res2

 to see the failure reason from the API.

biggabigga

Still no good. Sorry I'm such a lame scripter... 

 

Result:
error "Can’t get message of {SaveResult id \"sr_388\" of UserSession id \"us_385\" of application \"SalesforceScripting\"}." number -1728 from «class eMsg» of {«class svRs» id "sr_388" of «class usSN» id "us_385"}

 Does 1728 help any?

SuperfellSuperfell

I uploaded a new build that addresses the problem with status code/message etc. You can just do check for updates from the help menu to get it.

 

And here's a full sample of creating a contact, and checking the returned SaveResult object for either the Id of the created record, or the error message indicating why the create failed.

 

tell application "SalesforceScripting"
	activate
	set session to show login window
	
	set cont to make SObject
	set type of cont to "Contact"
	cont setField named "LastName" to "blowy2"
	set resList to session create sobjects cont
	set res to first item of resList
	if success of res then
		display dialog "created contact, Id is " & Id of res
	else
		display dialog "couldn't create contact " & StatusCode of res & ":" & message of res
	end if
	
end tell

 

biggabigga

Success! Here's the summary of all of this:

 

The script that creates the opportunity now works, thanks to Simon's error reporting functionality. This OrderNum variable is being sent to Salesforce as text:

 

	set OrderNum to "887766"

If written as Text, SalesforceScripting converts the OrderNum variable into a 15 character number: in this case "140734799799680" - the script attempts to enter that long number into these two fields:

 

	opp setField named "PDS__Document_Number__c" to OrderNum
	opp setField named "PDS__Invoice_Number__c" to OrderNum

Each of those fields has an 11-character limit. Here's the error that was reported by the New Version of SalesforceScripting when trying to write to one of those fields:

 

STRING_TOO_LONG:QB Transaction Number: data value too large: 140734799799680 (max length=11)

I was able to fix my script by simply changing the variable OrderNum to an integer:

 

set OrderNum to "887766" as integer

Now the opportunity is created correctly.