1

I have a vb.net console app that is pulling info from a database, and upserting it to a Salesforce object.

Here is the error I'm getting:

INVALID_FIELD: Field name provided, Product2 does not match an External ID for Product2

Here is the fields on the object:

Object

Here is the code I have:

 Try
 log.Info("Product Load Starting...")

        Dim cmdItems As String = "select ItemCode, ItemType, ItemCodeDesc, ExtendedDescriptionKey, SalesUnitofMeasure, StandardUnitofMeasure, ProductLine, Category1, Category2, Category3, Category4, DateCreated, InactiveItem, D002_UseInRO, D002_EscalationBillCode, D002_EscDailyRate, D002_EscWeeklyRate, D002_EscMonthlyRate, D002_EventRate, D002_ProRate1,D002_ProRate2, D002_ProRate3, D002_ProRate4, D002_MoDailyRate, StandardUnitPrice, D002_AllowRentals, DateUpdated from CI_Item where (ItemType = '1' or ItemType = '5') and InactiveItem<> 'Y' and D002_UseInRO = 'Y' and ItemCode not like '/DELV%'"

        Dim adaptItems As New OdbcDataAdapter(cmdItems, cn)
        Dim dsItems As New DataSet

        adaptItems.Fill(dsItems, "Items")


        Dim ItemEquip As New List(Of ETankSF.Product2)

        For Each dr As DataRow In dsItems.Tables("Items").Rows

            Dim Items As New ETankSF.Product2()

            Items.Product_Code_Ext_ID__c = dr("ItemCode").ToString.Trim()
            Items.ExternalId = dr("ItemCode").ToString.Trim()
            Items.ProductCode = dr("ItemCode").ToString.Trim()
            Items.ExternalDataSourceId = dr("ItemCode").ToString.Trim()

            If Not String.IsNullOrEmpty(dr("ItemCodeDesc").ToString) Then

                Items.Description = dr("ItemCodeDesc").ToString.Trim()

                Items.Name = dr("ItemCodeDesc").ToString.Trim()
            Else

                Items.Description = "n/a"

            End If

            If Not String.IsNullOrEmpty(dr("SalesUnitofMeasure").ToString) Then
                Items.QuantityUnitOfMeasure = dr("SalesUnitofMeasure").ToString
            Else
                Items.QuantityUnitOfMeasure = ""
            End If

            If Not String.IsNullOrEmpty(dr("ProductLine").ToString) Then

                If dr("ProductLine").ToString = "PFIT" Or dr("ProductLine").ToString = "PUMP" Or dr("ProductLine").ToString = "FLTR" Or dr("ProductLine").ToString = "HOSE" Then

                    Items.Division__c = "E-Pump"

                Else

                    Items.Division__c = "E-Tank"

                End If

            Else
                Items.Division__c = "E-Tank"
            End If

            Items.Category1__c = dr("Category1").ToString
            Items.Category2__c = dr("Category2").ToString
            Items.Category3__c = dr("Category3").ToString
            Items.Category4__c = dr("Category4").ToString


            If Not String.IsNullOrEmpty(dr("D002_EventRate").ToString) Then
                Items.Event_Rate__c = Convert.ToDouble(dr("D002_EventRate"))
            Else
                Items.Event_Rate__c = 0
            End If

            If Not String.IsNullOrEmpty(dr("D002_ProRate1").ToString) Then
                Items.Pro_Rate_1__c = Convert.ToDouble(dr("D002_ProRate1"))
            Else
                Items.Pro_Rate_1__c = 0
            End If

            If Not String.IsNullOrEmpty(dr("D002_ProRate2").ToString) Then
                Items.Pro_Rate_2__c = Convert.ToDouble(dr("D002_ProRate2"))
            Else
                Items.Pro_Rate_2__c = 0
            End If
            If Not String.IsNullOrEmpty(dr("D002_ProRate3").ToString) Then
                Items.Pro_Rate_3__c = Convert.ToDouble(dr("D002_ProRate3"))
            Else
                Items.Pro_Rate_3__c = 0
            End If
            If Not String.IsNullOrEmpty(dr("D002_ProRate4").ToString) Then
                Items.Pro_Rate_4__c = Convert.ToDouble(dr("D002_ProRate4"))
            Else
                Items.Pro_Rate_4__c = 0
            End If


            If Not String.IsNullOrEmpty(dr("D002_MoDailyRate").ToString) Then
                Items.MoDaily_Rate__c = Convert.ToDouble(dr("D002_MoDailyRate"))
                Items.MoDaily_Rate__cSpecified = True
            Else
                Items.MoDaily_Rate__c = 0
                Items.MoDaily_Rate__cSpecified = False
            End If


            Dim newProd(dsItems.Tables("Items").Rows.Count) As sObject

            newProd(dsItems.Tables("Items").Rows.IndexOf(dr)) = Items

            Dim sr() As UpsertResult = service.upsert("Product2", newProd)

            For i As Integer = 0 To sr.GetUpperBound(0)

                If sr(i).success Then

                    log.Info("SF Product Upload Success")

                Else

                    For Each err As ETankSF.Error In sr(i).errors

                        log.Error("Error Status: Product - " & err.statusCode.ToString)
                        log.Error("Error Message: Product - " & err.message.ToString)

                    Next

                End If
            Next
        Next
        log.Info("[Products] Added, Next...")
    Catch ex As Exception
        Console.WriteLine(ex.Message.ToString)
        log.Error("FAILED: " & ex.Message.ToString)

    End Try

Why do I get the above mentioned error? How can I fix it?

  • Hi Daniel, welcome to SFSE. Please take a moment to scroll through the tour and read How to Ask. Please note that the format here is specific questions and answers. Saying "help is appreciated" isn't really on topic, please try to instead ask something specific. I have edited your post to do so here. – Adrian Larson Aug 17 '18 at 0:30
  • Also, not sure what the syntax is in this language, but in Apex you have to specify which field you wish to upsert on. Note that unlike Prod_Code_Ext_Id__c, your ExternalId field is confusingly not defined as an External Id field. – Adrian Larson Aug 17 '18 at 0:30
  • @AdrianLarson Visual Basic .NET, apparently. – sfdcfox Aug 17 '18 at 1:32
0

Your problem is this line right here:

Dim sr() As UpsertResult = service.upsert("Product2", newProd)

"Product2" should be the name of the External Id field that you're using, not the name of the object you're upserting. I presume you meant to do:

Dim sr() As UpsertResult = service.upsert("Product_Code_Ext_ID__c", newProd)

Make sure the field name is correctly case sensitive. You may have other issues in your code; I have addressed only the primary error in your question.

  • This was totally it thank you! As I said I am attempting to refactor a former dev's code, where I know not all of it was working, but I can get it from here. THANKS! – Daniel Bauer Aug 17 '18 at 4:35
  • @DanielBauer I'm glad I was able to assist you. I know the API can be a bit obtuse sometimes, so I think this will also help future visitors avoid this problem. – sfdcfox Aug 17 '18 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.