Skip to content

Instantly share code, notes, and snippets.

@peterknolle
Last active March 6, 2020 21:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save peterknolle/4761339 to your computer and use it in GitHub Desktop.
Save peterknolle/4761339 to your computer and use it in GitHub Desktop.
Visualforce Remoting with Relationships
<apex:page controller="RemotingController">
<script type="text/javascript">
function Test_Object__c() {
this.Name = '';
this.Test_Object_2__r = null;
}
function Test_Object_2__c() {
this.Name = '';
}
var b = new Test_Object_2__c();
b.Name = 'New B';
var a = new Test_Object__c();
a.Name = 'New A';
a.Test_Object_2__r = b;
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.RemotingController.UpsertA}',
a,
function(result, event) {
console.log(result);
}
);
</script>
</apex:page>
global class RemotingController {
// Assumes two custom objects: Test_Object__c with lookup to Test_Object_2__c
@RemoteAction
global static void UpsertA(Test_Object__c obj1) {
upsert obj1.Test_Object_2__r;
obj1.Test_Object_2__c = obj1.Test_Object_2__r.Id;
upsert obj1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment