0

I have two managed custom object, so I can use master detail relationship to update the field on the parent record.

For example:

Object 1 - Parent__c
Field - Firmware__c
Object 2 - Child__c
Field - Firmware__c

I just want to create a custom button to update the firmware field of the parent record with the firmware field in the child record. This way, my users can choose which child record to update the parent record with. How can I achieve this?

  • Welcome to SFSE, Pracha. Please read the Help section and then update your question to let us know what you have tried so far. – Mike Chale Jan 14 '14 at 19:05
1

I would create a custom button on the child object page layout. This button would open a VF page. I would use the action attribute of that page to do this logic, and then return the user to the child record.

VF

<apex:page StandardController="Child__c" extensions="customExtension" action="{!copyFirmwareField}"/>

Apex

public class customExtension{

     private final Child__c myChild;

     public CheckForExpiredMilestonesNowExtension(ApexPages.StandardController stdController){
          this.myChild = (Child__c)stdController.getRecord();
          this.myChild = [Select Id, Name, Parent__c From Child__c Where Id =: myChild.Id];
     }

     public PageReference copyFirmwareField(){
          Parent__c parent = new Parent__c(Id = myChild.Parent__c, Firmware__c = myChild.Firmware__c);
          update parent;
          PageReference page = new PageReference('/' + myChild.Id);
          page.setRedirect(true);
          return page;
     }
}
|improve this answer|||||
  • Thanks for your suggestion. I had to make two modification to the code your provided. I removed final from declaring the Child__c variable and in the constructor, I also queried for Firmware__c. After the two modification it worked. I was getting "System.FinalException: Final variable has already been initialized" with the final keyword. – Pracha Yang Jan 14 '14 at 19:46
  • Great. Yes, only did the code in the text editor, so I did not compile it. Glad it helped. – Chris Duncombe Jan 14 '14 at 20:03
0

A less supported way, but doesn't require deploying (which you should be doing for all things code related in my opinion), would be to use the AJAX Toolkit and use a JavaScript button.

It can get a little messy, however here is an example of what you could put into a Javascript function button:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
var parentRecord = new sforce.SObject("Parent__c");
parentRecord.Id = {!Child__c.Parent__c};
parentRecord.Firmware__c = {!Child__c.Firmware__c}
updParent = sforce.connection.update([parentRecord]);
if (updParent[0].getBoolean("success")) {
  alert('Parent object Firmware__c has been updated to: {!Child__c.Firmware__c}');
  window.parent.location.href = '/{!Child__c.Id}';
} else {
  alert('Failed to update parent object');
}

The above isn't tested, has some fairly lousy error checking, but hopefully it gives you the right idea. For a bit of an explanation however, it creates a new sforce javascript object, which you specify the Id (no updates otherwise), and the field to update. It then calls the connection to run the update statement, which will return an error/success boolean value.

This also assumes the Parent relationship on your child record is called Parent__c

Also, for your consumption, you may want to look at the AJAX Toolkit here: AJAX Toolkit Reference

NOTE: There are some issues with referential URLS and communities that break these kind of buttons.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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