Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Question

Is it possible to leverage relationships with Visualforce remoting, including one object as a property of another?

Background

I've been playing around but always seem to get a VF remoting exception in the console with no useful information:

> Visualforce Remoting Exception:  VFRemote.js:118 $VFRM.Util.error
> VFRemote.js:118 (anonymous function) VFRemote.js:133 a.Event.fire
> VFRemote.js:52 a.Observable.fireEvent VFRemote.js:47
> _Visualforce.Direct._Visualforce.extend.onProviderData VFRemote.js:87 a.Event.fire VFRemote.js:52 a.Observable.fireEvent VFRemote.js:47
> _Visualforce.direct.RemotingProvider._Visualforce.extend.onData VFRemote.js:95
> _Visualforce.extend.handleResponse VFRemote.js:76 a VFRemote.js:39 (anonymous function)

I'm assuming that it's just not supported and I'm making the type-conversion routines throw a bit of a hissy fit, but it'd be good to know if it's just me doing something stupid.

Rough Code Example

If i Had ObjectA_c and ObjectB_c, where A has a lookup to B, is there a way to pass an instance of ObjectA_c to a remote method with an instance of ObjectB_c going along for the ride?

In the page

function ObjectA__c()
{
  this.Name = '';
  this.ObjB__r = null;              
}

function ObjectB__c()
{
  this.Name = '';
}

**snip**

var b = new ObjectB__c();
b.Name = 'New B';

var a = new ObjectA__c();
a.Name = 'New A';
a.ObjB__r = b;

Visualforce.remoting.Manager.invokeAction(
  '{!$RemoteAction.TheController.UpsertA}',
  a,
  function(result, event) {
    console.log(result);
  }
);

In the controller

global static void UpsertA(ObjectA__c objA)
{
  upsert objA.ObjB__r;
  upsert objA;
}
share|improve this question

1 Answer 1

up vote 3 down vote accepted

It is possible, although I'm not sure what the error message is that you are seeing.

I see an issue in the controller code. The Id reference field needs to be assigned after the first upsert is done.

global static void UpsertA(ObjectA__c objA)
{
  upsert objA.ObjB__r;
  // Add this line
  objA.ObjB__c = objA.ObjB__r.Id;
  upsert objA;
}

Here's a gist I put together with stripped down code that has it working.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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