+ Start a Discussion
Salesforce2015Salesforce2015 

Copy one object record data create two object records using apex class

If user clicks on custom button VF page will create object2 and object3 records using apex class.

Object1 Fields:
X1, x2, x3, and x4

Object2 Fields:
X1, x2, and x3

Object3 fields:
X2, x3 and x4

Thanks in advance.

Thanks,
Manu
srinu vassrinu vas
 
public class Example
{
public list<object1> l1=new list<object1>();
public list<object2> l2=new list<object2>();
public list<object3> l3=new list<object3>();

public void runBatch()
{
object2 ob2=new object2();
object2 ob2=new object2();

li=[select id,x1,x2,x3 from object1 limit 1];
for(object1 ob1:li)
{
ob2.x1=ob1.x1;
ob2.x2=ob1.x2;
ob2.x2=ob1.x2;
l2.add(ob2);
ob3.x1=ob1.x1;
ob3.x2=ob1.x2;
ob3.x2=ob1.x2;
l3.add(ob3);

}
insert l2;
insert l3;

}

}
Salesforce2015Salesforce2015
Hi Srinu,

Thanks for your quick response.

Below is my actual question, and i resolved myself. Please look into it and please help me on test class.

Requirement:
Create custom VF button on “Partner Group Requirements” object.
Button: Send Requirements (i.e. Detail Page Button)
VF Page:  SendPartnerGroupRequirements
Apex Class:  SendPartnerGroupRequirementsExt
User clicks on button page will create request and recipient object records using apex class code and save those records and then run the existing batch class.
 
VF Page Code:
--------------------------------------------------
<apex:page standardController="Partner_Group_Requirement__c" extensions="SendPartnerGroupRequirementsExt" action="{!SendRequest}">
</apex:page>
 
Apex Class:
---------------------------------------
public class SendPartnerGroupRequirementsExt {
    public String RequirementID{get;set;}
    public SendPartnerGroupRequirementsExt(ApexPages.StandardController controller) {
 
    }   
    public PageReference SendRequest(){
        RequirementID=ApexPages.currentPage().getParameters().get('id');
        list<Partner_Group_Requirement__c> PartgroupRequrement=[select id,Name,Partner_Group__c,Request_Type__c,Document_Type__c,Due_Date__c from Partner_Group_Requirement__c where id=:RequirementID];
        list<Request__c> ReqList=new list<Request__c>();
       for(Partner_Group_Requirement__c PGR : PartgroupRequrement){
            Request__c Req=new Request__c();
            //Req.Partner_Group__c=PGR.Partner_Group__c;
            Req.Request_Type__c=PGR.Request_Type__c;
            Req.Container_Template__c=PGR.Document_Type__c;
            Req.Due_Date__c =PGR.Due_Date__c;
            ReqList.add(Req);
             }
             if(ReqList.size()>0){
          system.debug('&&&&&sample&&&&&'+ReqList);
           insert ReqList;
           for(Request__c Req : ReqList){
               Recipient__c Recp=new Recipient__c();
               //Recp.Partner_Group__c=Req.Partner_Group__c;
               Recp.Request__c=Req.id;
               insert Recp;
               system.debug('******sampletest******'+Recp);
           }
        }
        PageReference orderPage = new PageReference('/'+RequirementID);
        orderPage.setRedirect(true);
        return orderPage;
    }
}
 
 
Before PageReference in apex class I’m calling batch class using below code.
Database.executeBatch(new ComplianceSendRequestBatch(objLstReq),1);
 
As of now everything working as expected, please help me on test class. That is fine for me.

Thanks,
Manu