Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in fields array: Protocol 'Encodable' as a type cannot conform to the protocol itself #133

Closed
perbrondum opened this issue Aug 1, 2021 · 7 comments
Assignees
Labels

Comments

@perbrondum
Copy link

Used to use [String : Encodable] as type for fields array. In 9.0 Error: Protocol 'Encodable' as a type cannot conform to the protocol itself

let  fields : [String : Encodable] = [keys.Subject.rawValue: self.subjectInp.text! ,...]
DB.salesforce.insert(type: objects.Event.rawValue, fields: eventNewFields)

@mike4aday mike4aday self-assigned this Aug 2, 2021
@perbrondum
Copy link
Author

perbrondum commented Aug 4, 2021 via email

@mike4aday
Copy link
Owner

@perbrondum yes, correct. Try a concrete type that conforms to Encodable instead, e.g. let fields: [String: String] = ...

@perbrondum
Copy link
Author

perbrondum commented Aug 4, 2021 via email

@mike4aday
Copy link
Owner

@perbrondum - you could use a type-erased wrapper, e.g. AnyEncodable:

let app = try ConnectedApp()
let fields: [String : AnyEncodable] = [
  "Name" : "Acme Corp., Inc.",
  "Revenue__c" : 123456.35,
  "Employee_Count__c" : 23,
  "US_Based__c" : true]
let _ = app.insert(type: "Account", fields: fields)

But I believe it's simpler to just apply a map function to each field value and convert it to a String before passing to insert.

@perbrondum
Copy link
Author

perbrondum commented Aug 5, 2021 via email

@perbrondum
Copy link
Author

perbrondum commented Aug 5, 2021 via email

@mike4aday
Copy link
Owner

mike4aday commented Aug 5, 2021

@perbrondum as long as Salesforce can deserialize the JSON values from string into the expected type it should work.

FYI you could use Swiftly Salesforce's date formatter and a value map on the fields dictionary:

let fields: [String : String] = [
   "Subject__c" : "My Subject",
   "Revenue__c" : 123456.35,
   "My_Date__c" : Date().addingTimeInterval(-100000),
   "Employee_Count__c" : 23,
   "US_Based__c" : true
]
.mapValues { value in
  switch value {
    case let date as Date:
      return DateFormatter.salesforce.string(from: date)
    default:
      return String(describing: value)
  }
}

let app = try ConnectedApp()
cancellable = app
.insert(type: "My_Object__c", fields: fields)
.sink(receiveCompletion: { completion in
  switch completion {
    case .failure(let error):
      print(error)
    case .finished:
      print("Success")
    }
  }, receiveValue: { 
    print("NEW RECORD ID: \($0)")
  }
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants