4

I'm developing in native iOS. I use SmartSync to sync up local soup to salesforce server. It works fine when updating to remote, when the record to be updated exists in remote. But it doesn't work when trying to insert to remote of a new record, which is created locally.

This is the code I wrote:

  1. json for the new record:

    let mockJsonData: [String: Any] = [SFKeys.createdById: appLocalData.currentUserId()!,
                                   SFKeys.medicalIWHCP: "0033600000ztTSKAA2",
                                   SFKeys.insight: "My test insight Steve",
                                   SFKeys.interaction: "a0o56000004AVpOAAW",
                                   SFKeys.localId: "test local ID",
                                   "__locally_deleted__": false,
                                   "__locally_updated__": false,
                                   "__locally_created__": true,
                                   "__local__": true,
                                   "SubjectId__": "a1J36000004Ol5LEAS",
                                   "KeyTopicString__c": "ISS",
                                   "Key_Topic__c": "a1856000000OVrHAAW"]
    
  2. insert this record to local soup:

     _ = try dataStore?.upsert(entries: [mockJsonData], forSoupNamed: HCPInsight.objectName, withExternalIdPath: SFKeys.localId)
    

It works fine so far at this point, the new record is created properly in the local soup with "local" and "locally_created" set to true.

Then I try to use SmartSync to sync up this record to remote, by creating my custom sync up target.

  1. Sync up operation:

    syncManager?.syncUp(target: SyncUpTarget(createFieldlist: [SFKeys.createdById], updateFieldlist: ["Insight__c","Key_Topic__c","SubjectId__c","HCP__c","Interaction__c","KeyTopicString__c","Resource__c"]), options: SyncOptions.newSyncOptions(forSyncDown: .overwrite), soupName: HCPInsight.objectName, syncName: nil, onUpdate: { (syncState) in switch syncState.status { case .done: print("sync up done") case .failed: print("sync up failed") default: break } })

The sync up failed. It didn't reset the "local" and "locally_created" fields to false. and generated a "last_error" field to the record, which says:

"last_error" : "Error Domain=https://vrtx-mosaic--staging.cs42.my.salesforce.com/services/data/v42.0/sobjects/(null) Code=404 \"(null)\" UserInfo={error=(\n {\n errorCode = \"NOT_FOUND\";\n message = \"The requested resource does not exist\";\n }\n)}",

The error is 404, which says The requested resource does not exist My guess is that, it seems like the server asks for the id of that record. But currently we don't have id for this record because it's generated locally, the id should be generated by the server when the record is inserted to it. Is there something wrong with my sync up target?

Many thanks!

Edit: Here is more information: For updating existing record, the request built by SmartSync is this:

endpoint: /services/data
method: PATCH  
path: /v42.0/sobjects/HCPInsight__c/a34560000005Xi2AAE

This works fine because the Id is treated as external id when upsert to the remote, and in this case, the Id field is defined.

But for insert a new record, the path it built is this:

endpoint: /services/data
method: POST
path: /v42.0/sobjects/(null)

because Id is nil when record is created locally. the path omits the Id and HCPInsight__c. I don't want it to omit HCPInsight__c. Any idea how I can modify the path of the request built by SmartSync?

0

1 Answer 1

0

I ran into a similar issue using version 7.2 of the SDK. I found that adding the Id field to the soup and adding the Id to syncUp and syncDown and populating the field with a value (i used "New" + CurrentDateTime), it would successfully create the record on the server/SFDC and smartsync updated the local record with the actual server Id. Hope this helps!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .