mike4aday / SwiftlySalesforce

The Swift-est way to build native mobile apps that connect to Salesforce.
MIT License
136 stars 43 forks source link

keyNotFound "id" error on simple query #137

Closed perbrondum closed 2 years ago

perbrondum commented 3 years ago

// Fails in 9.06 BETA with:

keyNotFound(CodingKeys(stringValue: "Id", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "records", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"Id\", intValue: nil) (\"Id\").", underlyingError: nil))

when using , works when using .

Not blocking for me, but seems related to the fix somehow.

private func getSFDCData() {
    DB.salesforce.identity().sink(receiveCompletion: { (completion) in
        switch completion {
        case .finished:
            break
        case let .failure(error) :
            print("Failed to login. Error: \(error)")
        }
    }) { identity in
        let userId = identity.userID

        let userSoql = "SELECT CompanyName FROM User where id = '\(userId)'"
        DB.salesforce.query(soql: userSoql).sink(receiveCompletion: { (completion) in
            switch completion {
            case let .failure(error):
                print("Failed to query(1) SFDC in MeetingVC. Error: \(error)")
            case .finished:
                break
            }
        }, receiveValue: { (userData : QueryResult<User>) in
            //
        }).store(in: &subscriptions)
    }.store(in: &subscriptions)
}
perbrondum commented 3 years ago

line 5: Should read "fails when using USER struct, works when using SALESFORCERECORD"

mike4aday commented 3 years ago

@perbrondum I believe the issue is that the Id field is not in the list of queried fields in your SOQL query; only CompanyName is queried. Does it work if you add Id to the queried field list?

perbrondum commented 3 years ago

Sorry, just saw this. While id is not needed in the select list, I will check tomorrow if the bug still exists with it in.

perbrondum commented 3 years ago

Indeed, adding id to the select list solves the problem.

perbrondum commented 3 years ago

Looked at this again, the id field does not have to be in the select list for this to work, but the Id field in the USER struct does have to be optional. public var Id: String? In my case I had the Id field as non-optional and that reproduces the error.

mike4aday commented 2 years ago

Hi @perbrondum - I assume User is a custom model struct you defined? If so, then any property defined as optional will work since the automatic decoding provided by Swiftly Salesforce via Swift's Decodable protocol will not throw an error if the server response is missing a value for that property. If the property is required, however, then it must be present in the JSON response, and the relevant field has to be in the SOQL select list.

perbrondum commented 2 years ago

Makes sense. Thanks for looking into this.

Per B Jakobsen CEO Tap2Sales.com @.***

On Mon, Nov 1, 2021 at 8:05 PM Michael Epstein @.***> wrote:

Closed #137 https://github.com/mike4aday/SwiftlySalesforce/issues/137.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mike4aday/SwiftlySalesforce/issues/137#event-5550897530, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALCYWDFYQCSDW7SMPAG6D2TUJ3QIDANCNFSM5CTIISFQ .