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
JoelJoel 

VBscript

Can VBscript be used to pull data from a user's salesforce.com account? Or do you have to use Java or C#? If so, is there a place where I can find some sample VBscript code for logging into salesforce.com with a username and password, and then pulling in contact information, for example.

Thanks much!
qmanqman
You can use the Office Toolkit which is really a COM object. Check your VBScript doc for instructions on using COM objects from VBScript.

Bill
JoelJoel
Bill

..thanks. I'll give it a try.

Joel
ScotScot
Here are a few snippets from a VBScript implementation ...
 
To login:
This is called from the mainline routine.
UserName2 and PassWord2 are constants (for use in testing), but the normal usage would be to call this
from a web link, using the session id's passed to the scontrol.
Function MeSession()
    MeSession = False
    on error resume next
    Set binding = CreateObject("SForceOfficeToolkit.SForceSession")
    dim x
    x = err.number
    if x = 429 then 
       call displayerror("Office Edition")
       exit function
    elseif x > 0 then
       call displayerror("Unknown error creating salesforce binding")
       exit function
    end if
    on error goto 0
    if Len(binding.SessionId) > 0 Then
          MeSession = True
          exit function
    End If
  if left("{!API_Enterprise_Server_URL_40}",5)="{!API" then
    MeSession = binding.Login(UserName2, PassWord2, False)
    batch = true
  else
    binding.SessionId = "{!API_Session_ID}"
    binding.SetServerUrl "{!API_Enterprise_Server_URL_40}"
    batch = false
    MeSession = true
  end if
End Function

The following gets an opportunity and it's related account. The input value (opportunity id) is provided by a merge field in the call of the scontrol, or as a constant for testing.

Notes:
 ... use of "For each" loops to get the values out of the result.
 ... ids for the retrieve must be put into an array before calling. 
 ... use of "*" in the retrieve for the list of fields -- something not supported in the API itself,
         but supported in the toolkit interface.
The result of this code are values in "opp" and "acct" for use later
 ... as is shown here by the use of opp.item("AccountId") ...

Sub GetOp(id)
    ids(0) = id
    Set qrs = binding.Retrieve("*", "Opportunity", ids, False)
    if qrs.size <> 1 then call oops("Opportunity was not retrieved")
    For Each so In qrs 
       set opp = so
    Next
    
    Acctid = opp.item("AccountId").value
    ids(0) = Acctid
    Set qrs = binding.Retrieve("*", "Account", ids, False)
    if qrs.size <> 1 then call oops("Account was not retrieved")
    For Each so In qrs ' only one
       Set acct = so
    Next

 Here is an example of updating two fields in an opportunity:

Code:

Sub UpdateOpp()   
   dim opps(0) 
   set opps(0) = opp
   for each so in opps
      so.Item("Check_Date__c").value = date()
      so.Item("Check_Status__c").value = tempstat
   next

   dim results
   results = binding.Update(opps,false)
if binding.Error = 0 then
     window.status = "Opportunity updated to " & tempstat
  else     
     window.status = "Oops, opportunity update failed"
     msgbox ("Oops, status update failed:" & vbcrlf & binding.errormessage & " (" & binding.error & ")")
  end if
end sub

 And finally, here is an example of inserting a new task:

Code:

Sub BuildTask()
dim newtasks(0)
   set newtasks(0) = binding.CreateObject("Task")

    Dim i 
    Dim Xmsg 
    If UBound(Xerror) > 0 Then 
        Xmsg = "** You have some problems to fix:" & vbNewLine
    ElseIf UBound(Xwarn) > 0 Then
        Xmsg = "Good Opportunity." & vbNewLine
    Else
        Xmsg = "GREAT Opportunity !!!"
    End If 
           
    For i = 1 To UBound(Xerror)
         Xmsg = Xmsg & vbNewLine & "   " & Xerror(i)
         Next
           
    If UBound(Xwarn) > 0 Then
           Xmsg = Xmsg & vbNewLine & vbNewLine & "You might want to consider:" & vbNewLine
        For i = 1 To UBound(Xwarn)
           Xmsg = Xmsg & vbNewLine & "   " & Xwarn(i)
           Next
        End If
dim newtask   ' individual task
for each newtask in newtasks
  newtask.Item("ActivityDate").value = dateadd("d",14,date())
  newtask.Item("Description").value = xmsg
  newtask.Item("OwnerId").value = opp.Item("OwnerId").value
  newtask.Item("Priority").value = "Normal"
  newtask.Item("SPIN_Type__c").value = "Continuation"
  newtask.Item("Status").value = "Not Started"
  newtask.Item("Subject").value = "Fix Problems"
  newtask.Item("WhatId").value = opp.Item("Id").value
  newtask.Item("Activity_Status__c").value = "Error Checking"
 next
dim r         ' individual result
dim results   ' array of results
dim errmsg    ' error message string, built from each message
dim errors    ' array of error objects
dim e         ' individual error object
results = binding.Create(newtasks,false)
if binding.Error = 0 then
     msgbox("Ok, task created")
  else     
     msgbox("Oops, task generation failed:" & vbcrlf & binding.errormessage & " (" & binding.error & ")")
  end if
end sub

 

Updated to simplify several examples

Message Edited by Scot on 11-22-2005 10:17 AM