+ Start a Discussion
tom_patrostom_patros 

RemoteAction Parameters Help

I have an Apex method that is marked as a remoteAction which is attempting to take two parameters: a Lead and a List of custom objects ("Custom_Object__c" isn't the real object's name, just changed to protect the innocent);

 

@RemoteAction
public static Boolean insertData(Lead l, List<Custom_Object__c> customObjects) {
   // insert the Lead
   insert l;
   // assign the lead Id to each custom object in the list
   for(Custom_Object__c c : customObjects) {
      c.Lead__c = l.Id
   }
   // insert the custom objects
   insert customObjects;
   // return true (just for example)
   return true;
}

 Over in Javascript, I'm calling this remoteAction with the following data:

// sample data
var lead = {
   FirstName = 'Test',
   LastName = 'Test',
   Company = 'Test',
   Email = 'test@test.com',
   Phone = '555 555 1212'
};
var customObjects = [
   {
      Custom_Field1__c = 'A',
      Custom_Field2__c = 'B'
   },
   {
      Custom_Field1__c = 'A',
      Custom_Field2__c = 'B'
   }
];
MyApexClass.insertData(lead, customObjects, function(result, event) {
   // this would be the callback mojo
});

This code will make it as far as the insertData method, but I get the following error:

 

Visualforce Remoting Exception: undefined

 

I tried changing the insertData method to take the second parameters as List<sObject> instead of List<Custom_Object__c> (and modded the code to use dynamic DML to support this), but then I get this error:

 

Visualforce Remoting Exception: Unable to determine SObject type: SObject.  Please provide an 'id' or 'sobjectType' value.

 

I interpret this error as needing to supply a 'sobjectType' property to either a) the elements in my "customObjects" array or b) the array itself. I've tried all of the following permutations but still get the same error:

 

// ... add the property to the element doesn't work
var customObjects = [
   {
      Custom_Field1__c = 'A',
      Custom_Field2__c = 'B',
      sobjectType = 'Custom_Object__c'
   },
   {
      Custom_Field1__c = 'A',
      Custom_Field2__c = 'B',
      sobjectType = 'Custom_Object__c'
   }
];

// add the property to the array doesn't work customObjects.sobjectType = 'Custom_Object__c'; // for fun, tried to indicate that it's a list, these don't work either customObjects.sobjectType = 'List<Custom_Object__c>'; customObjects.sobjectType = 'Custom_Object__c[]';

 

Can someone point me in the right direction here? Is there another way to specify the sobjectType property? Are there issues with multiple params on a remoteaction, or issues with collection-based params? 

ca_peterson_oldca_peterson_old

Looking at the data I get back from calling a remote action method that runs JSON.serialize I get it in a form like so:

 

{ "attributes" : { "type" : "Account", "url" : "/services/data/v23.0/sobjects/Account/001A000000nsu2gIAA" }, "Id" : "001A000000nsu2gIAA", "Name" : "Bandersnatchii" }

 

I guess there needs to be an attributes section with a type for it to work properly. Give that a shot?

Starz26Starz26

Shot in the dark here as I have not passed object from java....

 

Leaving the param as List<sObject> customObject

 

use:

 

insert (custom_object__c)customObjects
tom_patrostom_patros

@ca_peterson - I tried the "attributes" and "type" approach, but still no love. I was so hopeful - it totally seemed like the right idea.

 

@Starz26 - I'm actually getting the error back before the insert statement - it's like the remoteAction method bombs when trying to interpret the parameters.

 

I'm going to do a couple of tests to see if it's related to the number / index of the parameters.

 

Starz26Starz26

Where exactly in the line throwing the error? the for loop?

 

maybe set a variable Custom_Object__c var = (custom_object__c)customObject

 

then use that as the for loop object as well as the insert........

 

just thinking outloud

tom_patrostom_patros

Update: this appears to be working correctly now in Spring '12 sandboxes. My original code which passed a single object and a list of objects is working as expected.

 

For what it's worth, I was dealing with another JSON-related issue, which is expected to be resolved in Spring '12 too, so it seems like a lot of JSON fixes are coming with the next release.

dipdip

Hi,

 

Can I use javascript remoting from salesforce site authinticated by customer portal login.

has any one tried it before.

 

Regards

Barman

tom_patrostom_patros

You should be able to - we've done some projects recently using remote actions via Force.com Sites without any trouble. Note that the object and field permissions set on the "Sites Guest User" will persist with remote actions.

sharmapankaj133sharmapankaj133

Hi, I am facing problem with javascript remoting.

I am using Site.createPortalUser() from a javascript function and all data passed to this method correctly but it always returns null instead of any exception or userId.

 

Can you help me regarding this?

tom_patrostom_patros
Hmm - this in an interesting use case. I'm wondering if you can even reference the "Site" namespace in remoteactions (I've never tried). Just to test out something else, have you tried Site.login via RemoteAction?
sharmapankaj133sharmapankaj133
No i didn't tried but this is a problem of forceSSL attribute of apex form.....i don't know how to deal with this.....