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
scottskiblackscottskiblack 

ASP.NET with VB.NET create case help

I am trying to create a new case with a form on a web page (ASP.NET with VB.NET).  The code behind to process the form and insert is as follows.  Fails at Try creating the case.

        Dim binding As sforce.SforceService
        binding = Nothing
        Dim SFLogin As New Main
        SFLogin.login()

        'First we will create an account object array, all calls are batch
        'ours is a single element batch
        Dim SFDCcases(1) As sforce.sObject
        Dim SFcase As sforce.Case = New sforce.Case

        'instantiate the new account object
        Dim qr As sforce.QueryResult = Nothing

        'Set several properties
        SFcase.First_Name__c = FirstName.Text
        SFcase.Last_Name__c = LastName.Text
        SFcase.Street_Address__c = Address.Text
        SFcase.City__c = City.Text
        SFcase.State_Province__c = State.Text
        SFcase.Postal_Code__c = Zip.Text
   
        SFDCcases(0) = SFcase

        Dim saveResults() As sforce.SaveResult = Nothing
        Try
            saveResults = binding.create(SFDCcases)  ' ERROR Object reference not set
                                                                                    ' to an instance of an object.)
        Catch ex As Exception
            ERRORTXT.Text = ex.Message
            recordType.Text = ex.Source
            status.Text = ex.StackTrace
        End Try



SuperfellSuperfell
I don't see how the binding local variable is anything but null, 10 seconds with the debugger should be able to confirm that.
scottskiblackscottskiblack
Thanks,

I took out setting the binding to nothing after declaring it and yes it is still nothing when debugging.
What am I missing?

I'm new to VB.NET from PHP.

Thanks, again.
scottskiblackscottskiblack
Code is now below.  I don't understand why the binding is null.  This is just like the VB.NET start example.

   Protected Sub buttonPost_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonPost.Click

        Dim binding As sforce.SforceService
        Dim lr As sforce.LoginResult

        If lr Is Nothing Then
            Dim SFLogin As New Main
            SFLogin.login()
        End If

        'First we will create an case object array, all calls are batch
        'ours is a single element batch
        Dim SFDCcases(1) As sforce.sObject
        'instantiate the new case object
        Dim SFcase As sforce.Case = New sforce.Case

        'Set several properties
        SFcase.First_Name__c = FirstName.Text
        SFcase.Last_Name__c = LastName.Text

        SFDCcases(0) = SFcase

        Dim saveResults() As sforce.SaveResult = Nothing
        Try
            saveResults = binding.create(SFDCcases)
        Catch ex As Exception
            ERRORTXT.Text = ex.Message
            recordType.Text = ex.Source
            status.Text = ex.StackTrace
        End Try

        For i As Integer = 0 To saveResults.GetUpperBound(0)
            'we will create a separate save result object for clarity
            Dim saveResult As sforce.SaveResult = saveResults(i)
            'check to see if the first create was a success
            If saveResult.success Then
                'the id that we passed should be the one we get back.
                WriteLine("Case entered with id: " + saveResult.id)
            Else
                'an error occurred on this record
                Dim [error] As sforce.Error = saveResult.errors(0)
                WriteLine([error].message)
            End If
        Next
    End Sub
SuperfellSuperfell
because binding is a local variable that you never give a value.
scottskiblackscottskiblack
I don't understand because the QuickStart VB example is below and it works?  What should the binding be set to then? Any suggestions or know how to set it?

Module quickstart

    Private binding As apex.SforceService
    Private lr As apex.LoginResult
    Private records() As apex.sObject
.
.
.
.
.

     'First we will create an account object array, all calls are batch
        'ours is a single element batch
        Dim accounts(1) As apex.sObject

        'instantiate the new account object
        Dim account As apex.Account = New apex.Account

        'set the id, the id must always be set for an update
        account.Id = accountID

        'set the new name of the account
        account.Name = newAccountName

        'add the account to the array of objects
        accounts(0) = account

        'we are now ready to update the record
        'create an array for the results
        Dim saveResults() As apex.SaveResult = Nothing
        Try
            saveResults = binding.update(accounts)
        Catch e As Exception
            Console.WriteLine(e.Message + vbCrLf + e.StackTrace)
            Return
        End Try

        For i As Integer = 0 To saveResults.GetUpperBound(0)
            'we will create a separate save result object for clarity
            Dim saveResult As apex.SaveResult = saveResults(i)
            'check to see if the first update was a success
            If saveResult.success Then
                'the id that we passed should be the one we get back.
                Console.WriteLine("Update account with id: " + saveResult.id)
            Else
                'an error occurred on this record
                Dim [error] As apex.Error = saveResult.errors(0)
                Console.WriteLine([error].message)
            End If
        Next
scottskiblackscottskiblack
Ok, I now have binding set. But I am not sure how to proceed with this new error.
But I get the following error now:
       UNKNOWN_EXCEPTION: Destination URL not reset. The URL returned from login must be set in the SforceService


    Protected Sub buttonPost_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonPost.Click

        Dim binding As sforce.SforceService
        binding = New sforce.SforceService()

        Dim SFLogin As New Main
        SFLogin.login()


        'First we will create an case object array, all calls are batch
        'ours is a single element batch
        Dim SFDCcases(1) As sforce.sObject
        'instantiate the new case object
        Dim SFcase As sforce.Case = New sforce.Case

        'Set several properties
        SFcase.First_Name__c = FirstName.Text
        SFcase.Last_Name__c = LastName.Text
        SFcase.Employee_Number__c = EmpNumber.Text
        SFcase.Street_Address__c = Address.Text
        SFcase.City__c = City.Text
        SFcase.State_Province__c = State.Text
        SFcase.Postal_Code__c = Zip.Text
        SFcase.Country__c = Country.Text
        SFcase.Phone__c = Phone.Text
        SFcase.Mobile_Phone__c = Mobile.Text
        SFcase.Fax__c = Fax.Text
        SFcase.Email__c = Email.Text
        SFcase.Company__c = Company.Text
        SFcase.Department__c = Department.Text
        SFcase.Division__c = Division.SelectedValue
        SFcase.Salesforce_training__c = Training.Checked
        SFcase.Title__c = Titletxt.Text
        SFcase.Employee_Number__c = EmpNumber.Text

        SFDCcases(0) = SFcase

        Dim saveResults() As sforce.SaveResult = Nothing
        Try
            saveResults = binding.create(SFDCcases)
        Catch ex As Exception
            ERRORTXT.Text = ex.Message
            recordType.Text = ex.Source
            status.Text = ex.StackTrace
        End Try

        'For i As Integer = 0 To saveResults.GetUpperBound(0)
        '    'we will create a separate save result object for clarity
        '    Dim saveResult As sforce.SaveResult = saveResults(i)
        '    'check to see if the first create was a success
        '    If saveResult.success Then
        '        'the id that we passed should be the one we get back.
        '        WriteLine("Case entered with id: " + saveResult.id)
        '    Else
        '        'an error occurred on this record
        '        Dim [error] As sforce.Error = saveResult.errors(0)
        '        WriteLine([error].message)
        '    End If
        'Next
    End Sub
SuperfellSuperfell
All the work in SFLogin.login that's done to setup the binding, you're throwing away, you need to use the binding that login sets up, so that it has a sessionId and the right serverUrl setup on it.
scottskiblackscottskiblack
Thanks,
Like I said I am new to VB.  I am totally confused on the binding.  I do not see how to use this appropriately, can you give an example or something in more detail of how to use the binding that login sets up, so that it has the sessionID and the serverUrl?

An example would be really helpful.


scottskiblackscottskiblack
I'm trying this route with no avail.

        Dim m As New Main
        m = Session("main")

        Dim validSession As Boolean = m.setupBinding(Request("sessionId"), Request("serverURL"))
        If (validSession) Then
            m.validated = m.validateUser(Request("userId"), m.positionId)
            If (m.validated) Then
                Session.Add("main", m)

            End If
        End If
ZitizonXZitizonX
 
Did you get it working mate?
scottskiblackscottskiblack
not yet.  starting back on it today.
scottskiblackscottskiblack
Are there any examples or documentation for VB.NET to understand the bindings for Salesforce in regards to the sessionId and serverUrl?

I have got the VB.NET quickstart and used this code to try a form to insert a case as well as downloaded the asp examples too.

Does anyone have any useful tips to understand this subject?
SuperfellSuperfell
Your problems seem to be largely related to understanding the object lifetime and processing model in ASP.NET, I'd recommend you start with a good ASP.NET book.
SuperfellSuperfell
Oh, and i think there's some ASP.NET samples on the Wiki, see http://wiki.apexdevnet.com/index.php/API#.NET
scottskiblackscottskiblack

I have a web page with a form to capture case information.  I am trying to create a new case from this form using the enterprise WSDL in ASP.NET with VB.NET.  When click submit the following code is executed, my main class and the function to create is followed.  Login, works, everything seems to work fine when I debug, but no case is created in SFDC.  Any suggestions?

Protected Sub buttonPost_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonPost.Click

Dim m As New Main

'First we will create an case object array, all calls are batch

'ours is a single element batch

Dim SFDCcases(1) As sforce.sObject

'instantiate the new case object

Dim SFcase As sforce.Case = New sforce.Case

'Set several properties

SFcase.First_Name__c = FirstName.Text

SFcase.Last_Name__c = LastName.Text

SFcase.Subject = "Request"

SFcase.Reason = "Other"

SFDCcases(0) = SFcase

Try

If m.CreateCase(SFDCcases(0)) Then

status.Text = "IT worked"

End If

Catch ex As Exception

ERRORTXT.Text = ex.Message

status.Text = ex.StackTrace

End Try

End Sub

 

 

 

Public Function CreateCase(ByVal SFcase As sforce.sObject) As Boolean

'This funciton will create a case if available and return true

login()

Dim SFDCcases(1) As sforce.sObject

SFDCcases(0) = SFcase

Dim saveResults() As sforce.SaveResult = Nothing

Try

saveResults = binding.create(SFDCcases)

Catch ex As Exception

Throw New Exception(ex.Message & vbCrLf & ex.StackTrace)

Return False

End Try

Return True

End Function

SuperfellSuperfell
Look at the success & error properties of the saveResult object.
scottskiblackscottskiblack

Thanks for your patience! The error is there, missing a required field.

 

eformxeformx
VB.Net ASP.Net Create Contact/Case/Lead Solution:  Make sure your password and consumer secret are combined in the following.  For example if your password was 1234 and your consumer secret is abcde then your password is 1234abcde.  The following is based on a download of the enterprise wsdl.  You make a web reference to it in Visual Studio (I am using VS 2017 Community).  This code will connect to SalesForce, validate the username and password+consumer secret and return a session id to the call.  It will then create a contact. 
 
Public Function loginSample(_userId As String, _password As String) As Boolean
        Dim success As Boolean = False
        Dim username As String = _userId
        Dim password As String = _password

        ' Create a service object.  Note that binding object name could be anything, you will see this object name but anything could be used...binding could be sforce_login 
        Dim binding = New SforceService
        Dim lr As LoginResult

        Try

            'login via username and password + consumer secret
            lr = binding.login(username, password)

            ' Save old authentication end point URL
            Dim authEndPoint As String = binding.Url

            ' Set returned service endpoint URL
            binding.Url = lr.serverUrl

            binding.SessionHeaderValue = New SessionHeader
            binding.SessionHeaderValue.sessionId = lr.sessionId

            ' Print user and session info
            Dim userInfo As GetUserInfoResult = lr.userInfo
            Response.Write("UserID: " + userInfo.userId + "<br/>")
            Response.Write("User Full Name: " + userInfo.userFullName + "<br/>")
            Response.Write("User Email: " + userInfo.userEmail + "<br/>")
            Response.Write("<br/>")
            Response.Write("SessionID: " + lr.sessionId + "<br/>")
            Response.Write("Auth End Point: " + authEndPoint + "<br/>")
            Response.Write("Service End Point: " + lr.serverUrl + "<br/>")
            Response.Write("<br/>")

            'First we will create a contact object array...sforce_contact(0).  Need 2 contacts then use sforce_contact(1) and so on.
            Dim sforce_contact(0) As sforce.sObject
            'instantiate the new case object
            Dim SFcontact As sforce.Contact = New sforce.Contact

            'Set several properties
            SFcontact.FirstName = "John"
            SFcontact.LastName = "Doe"
            sforce_contact(0) = SFcontact

            Dim saveResults() As sforce.SaveResult = Nothing

            Try
                'this saves the contact via the credentials, callback url in our binding object
                saveResults = binding.create(sforce_contact)

                'write the contact id from salesforce to our asp.net page
                For i As Integer = 0 To saveResults.GetUpperBound(0)
                    'we will create a separate save result object for clarity
                    Dim saveResult As sforce.SaveResult = saveResults(i)
                    'check to see if the first create was a success
                    If saveResult.success Then
                        'the id that we passed should be the one we get back.
                        Response.Write("<br/>")
                        Response.Write("Contact entered with id: " + saveResult.id + "<br/>")
                    Else
                        'an error occurred on this record
                        Dim [error] As sforce.Error = saveResult.errors(0)
                        Response.Write([error].message)
                    End If
                Next

            Catch ex As Exception
                Dim s As String = ex.Message
            End Try

            ' Return true to indicate that we are logged in, pointed  
            ' at the right URL and have our security token in place.     
            success = True
        Catch e As Exception
            Dim s As String = e.Message
        End Try

        Return success
    End Function
Hope this helps someone.