1

I am trying to call a custom apex controller from javascript code in my visual force page but keep getting the following error :

 Visualforce Remoting Exception: No such column '_name' on sobject of type facture__c 

Here's the javascript call :

   var createFacture = function() {
          var facture = new SObjectModel.Facture();
          var factureReferenceField = document.getElementById('facture-reference');
          var factureClientField = document.getElementById('select-client');
          facture.Reference__c = factureReferenceField.value ;
          facture.Client__c = factureClientField.value;
          console.log(facture);

        Visualforce.remoting.Manager.invokeAction(
            //Invoking controller action getcon
            '{!$RemoteAction.TestController.saveFacture}',
             facture, factureLines,
            function(result, event){
                console.log("in callback func");   
                console.log(event.status);
                console.log(event.message);
            },
            {escape: true}
        );


         console.log('hell yeah');                          
      }

and here's the apex controller method Im trying to call :

@RemoteAction
public static Facture__c saveFacture(Facture__c facture, Ligne__c[] lines) {
    // Perform isUpdatable() checking first, then
    System.debug('facture: '+ facture);
    System.debug('lines: '+ lines);
    upsert facture;
    for(Ligne__c line : lines) {
        line.facture__c = facture.Id;
        upsert line;
    }
    return facture;
}

I ve made the full code of the page and the controller available on github if you need to see more :

https://gist.github.com/davidgeismar/163f099df6d1c8d814d04eb76dffbcc6

How to fix this error and make my remote call work ?

1

apex:remoteObjects provides a JS-only interface; you can use this to perform DML operations and queries without Apex.

For a remote action method, you'd use a simple object:

var facture = { };
...
facture.Reference__c = factureReferenceField.value;
facture.Client__c = factureClientField.value;
    Visualforce.remoting.Manager.invokeAction(
        //Invoking controller action getcon
        '{!$RemoteAction.TestController.saveFacture}',
         facture, ...
  • using a simple object also leads to an error : VFRemote.js:117 Visualforce Remoting Exception: Unexpected type for TestController.saveFacture(facture__c) (I simplified the controller action to only accept a single facture__c parameter for testing purpose – David Geismar Jan 15 at 9:34
0

The structure of the instance created from sObjectModel will be different from the actual structure of your object. Hence, you cannot perform DML on that instance in your remote action method, that is the reason you are getting the above mentioned error. You can refer the below link from Salesforce documents to understand more on the structure created using sObjectModel (It creates a attribute containing comma separated API names of fields and not attributes for each field)
http://releasenotes.docs.salesforce.com/en-us/spring14/release-notes/rn_vf_remote_objects_remoteobjectmodel_component.htm

If you want to create a record from javascript, you can use the create method of Remote Object. You can find further help on this topic in this below link
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_remote_objects_using_create.htm

You can use the callback function of the create method and send the ID of the record to your controller for any further actions.

  • ok so if I understand correctly javascript remoteObject can only be sent for backend creation to standard controller methods, is that right ? Isnt there a way to easily create a javascript object that will be recognized as a custom backend object and that I can send to a custom controller action ? – David Geismar Jan 11 at 11:47
  • I updated the second link which got duplicated earlier, also updated the answer as well, you can refer the second link which will help you create the record in javascript and using the callback function, you can pass the ID of the newly created record to your controller for further actions. – Vijay Ganji Jan 14 at 6:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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