+ Start a Discussion

remote object

I am trying for two days to update (upsert) records (over a 1000) from a table to a custom object. From the code below, the first alert correctly shows the initial value and after upsert, the second alert shows the correct updated value but if I physically check the data in the custom object through a standard SOQL query or through a view, the data hasn't changed. 
target.retrieve((err, results)=>{
                 			alert(`before: ${r.get('q1_target_volume__c')}`);
                			target.set('q1_target_volume__c', r.get('q1_actual_volume__c') * (1+volPercentage.value/100));
                			target.set('q2_target_volume__c', r.get('q2_actual_volume__c') * (1+ volPercentage.value/100));
                			target.set('q3_target_volume__c', r.get('q3_actual_volume__c') * (1+ volPercentage.value/100));
                			target.set('q4_target_volume__c', r.get('q4_actual_volume__c') * (1+ volPercentage.value/100));
							target.set('q1_target_target__c', r.get('q1_actual_volume__c') * (1+ gpPercentage.value/100));
                			target.set('q2_target_target__c', r.get('q2_actual_volume__c') * (1+ gpPercentage.value/100));
                			target.set('q3_target_target__c', r.get('q3_actual_volume__c') * (1+ gpPercentage.value/100));
                			target.set('q4_target_target__c', r.get('q4_actual_volume__c') * (1+ gpPercentage.value/100));
                			alert(`after: ${target.get('q1_target_volume__c')}`);

Best Answer chosen by Kevlangdo
I see my error. i set some fields are required in the object but I am not including them in my update or upsert.

All Answers

It is a sales forecasting application
The reference https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_remote_objects_using_upsert.htm doesn't mention anything about passing an Id. Each record "r" is accessed and updated. the second alert clearly shows the updated values, except the underlaying object doesn't have the saved values. It is on the in-memory object "target" that have the saved values. How do I write those values back to the server
srlawr uksrlawr uk
In this situation, I suppose I would say that it is entirely possible that if the upsert action could be firing successfully - but the persistance layer might be booting it out.

You see, Remote Object go async, so once it's fired, you get no feedback. If on your database layer you had any data validation rules, or triggers/events firing - that then fail.. your records will not be updated.

Perhaps the "q1_target_volume__c" object has a required field that isn't being set? Or there is a trigger that tries to divide some number by "q1_target_volume__c" - and you are setting a zero into one of those fields (thus throwing a maths exception).... anything like that will cause the transaction to fail and the records not update.

You will have to use the Salesforce logs to try to trace any triggers on q1_target_volume__c, and manually check the validation rules etc. to make sure the data you are loading is valid.

That's where I'd start with this anyway! Hope that helps.
There are no triggers or validation rules on the object. I built it. It simply an object to store forecasted sales data once that data is calculated in the UI.
srlawr uksrlawr uk
hmm. ok

Could you try leveraging the callback function parameter in the upsert call to debug?

Something like:
target.upsert(null, new function(error, results, event) {
// or alert(error); whichever is your flavor

In the Chrome tools, I do have this error, but I am not sure if it is related

VFMetadataSender.js:64 Uncaught TypeError: Cannot read property 'generateUrl' of undefined
    at Object.createAppDomainFrame (VFMetadataSender.js:64)
    at Object.sendViewstate (VFMetadataSender.js:66)
    at SalesTargetPlanner?core.apexpages.request.devconsole=1:1050
I need to validate the write security of my fields
Error: The following fields on Targets__c are invalid or not writeable: q1_target_target__c,q2_target_target__c,q3_target_target__c,q4_target_target__c
    at d._handleResult (VFSObjectCrud.js:62)
    at d._handleResult (VFSObjectCrud.js:28)
    at d._handleCreateResult (VFSObjectCrud.js:62)
    at d._handleCreateResult (VFSObjectCrud.js:28)
    at VFSObjectCrud.js:56
    at e.cb (VFRemote.js:133)
    at constructor.doCallback (VFRemote.js:99)
    at constructor.onData (VFRemote.js:94)
    at VFExt3.data.Connection.handleResponse (VFRemote.js:75)
    at a (VFRemote.js:39)

I see my error. i set some fields are required in the object but I am not including them in my update or upsert.
This was selected as the best answer