I want to copy field data from one object to another with a click on a button.
I want to get a snapshot of the current family status and copy the data from related son
and father
data.
I have an custom object son
with the following fields:
s_name
, s_street
and father
, where father
is a lookup to object father
, which has the fields f_name
and f_age
.
I want to have a button on the son
page, saying new family
, which populates another custom object family
with the respective fields from the originating objects: s_name
, s_street
etc.
How would I realize such a copy action via Apex
?
Attribution to: mcbetz
Possible Suggestion/Solution #1
If you want on a button call go for custom button with apex behind it doing job of creation and populating the data.
If you want that as soon as a new Son record and automatic family record is created and populated go for trigger,
//apex method pseudo code
public pagerefrence createfamilyrecord(){
Id recordId = System.currentPagereference().getParameters().get('Id');
List<Son__c> lstson=[Select Id ,s_name, s_street from Son__c where id =:recordId];
if(lstson__c.size()>0){
Family__c fam=new Family__c();
fam.field1=lstson.get(0).s_name;
fam.field2=lstson.get(0).s_street;
insert faml;
return null;//Return where you want the navigation to be
}
}
//Visualforce pseudo code
<apex:commandButton action="{!createfamilyrecord}" value="NEw Family" id="theButton"/>
Attribution to: Mohith Shrivastava
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/5349