function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tamika King 5Tamika King 5 

How to transfer object data to another object

Hello i'm trying to transfer all contributions from contribution object over to my plans object. I'm able to transfer everything else except for the contributions. I haven't received an error but not sure why they don't come over. Please see my code below and my attachment.


//Query plans from last year that have not been created for facilities in new year (to prevent duplicates).
List<Plan__c> plansthisYear = [
    SELECT Approved_Amount__c,Donation_Year__c,Facility__c,Id,Name,Plan_Status__c,Proposed_amount__c
    FROM Plan__c
    WHERE Donation_Year__c = :newYear
    ];

//Placeholder for all new contributions
List<Contribution__c> contributionList = new List<Contribution__c>();

//Loop through all new plans.
for (Plan__c p : plansthisYear) {

    String myId = p.Name;
 //get contributions for this plan
 List<Contribution__c> contributionsFromLastYear = [
 SELECT Amount__c,Community_Organization__c,Contribution_Status__c,Name,Project_Title__c FROM Contribution__c where Plan__c = :myId ];
 
 //Loop through all contributions from last year for this plan
 for(Contribution__c cont : contributionsFromLastYear ){

 //add the values to a new contribution
 Contribution__c contributions = new Contribution__c();
 contributions.Previous_Amount__c = cont.Amount__c;
 contributions.Contribution_Status__c =  cont.Contribution_Status__c;
 contributions.Community_Organization__c = cont.Community_Organization__c;
 contributions.Project_Title__c = cont.Project_Title__c;
 contributions.Plan__c =  myId; 
       
 contributionList.add(contributions);
 } 


insert contributionList;

}User-added image
Tamika King 5Tamika King 5
This is what i'm trying to see

User-added image
Raj VakatiRaj Vakati
try this code
 
//Query plans from last year that have not been created for facilities in new year (to prevent duplicates).
List<Plan__c> plansthisYear = [
    SELECT Approved_Amount__c,Donation_Year__c,Facility__c,Id,Name,Plan_Status__c,Proposed_amount__c
    FROM Plan__c
    WHERE Donation_Year__c = :newYear
    ];

//Placeholder for all new contributions
List<Contribution__c> contributionList = new List<Contribution__c>();
Set<String> pIds = new Set<Id>() ;
//Loop through all new plans.
for (Plan__c p : plansthisYear) {

    //String myId = p.Name;
	pIds.add(p.Id);
}
	
 //get contributions for this plan
 List<Contribution__c> contributionsFromLastYear = [
 SELECT Amount__c,Community_Organization__c,Contribution_Status__c,Name,Project_Title__c FROM Contribution__c where Plan__r.In :pIds ];
 
 //Loop through all contributions from last year for this plan
 for(Contribution__c cont : contributionsFromLastYear ){

 //add the values to a new contribution
 Contribution__c contributions = new Contribution__c();
 contributions.Previous_Amount__c = cont.Amount__c;
 contributions.Contribution_Status__c =  cont.Contribution_Status__c;
 contributions.Community_Organization__c = cont.Community_Organization__c;
 contributions.Project_Title__c = cont.Project_Title__c;
 contributions.Plan__c =  myId; 
       
 contributionList.add(contributions);
 } 
 
 insert contributionList;

 
Tamika King 5Tamika King 5
I receive the error below, i checked to make sure all the semicolons were there and changed Plan__r to Plan__c but still received the error below.

User-added image
Tamika King 5Tamika King 5
i tried changing a few other things but still no results, here is the entire code which will clone the previous plans from 2018 and bring over to 2019 and will also bring in the contributions, the cloning of the plans works fine, the contributions just don't come over

//Set last year and new year values
String lastYear = '2018';
String newYear = '2019';

//Query any plans from new year that have already been created, and get set of facility IDs.
Set<Id> facilityIdsAlreadyInNewYear = new Set<Id>();
for (Plan__c p : [SELECT Id, Facility__c FROM Plan__c WHERE Donation_Year__c = :newYear]) {
    facilityIdsAlreadyInNewYear.add(p.Facility__c);
}

//Query plans from last year that have not been created for facilities in new year (to prevent duplicates).
List<Plan__c> plansFromLastYear = [
    SELECT Approved_Amount__c,Donation_Year__c,Facility__c,Id,Name,Plan_Status__c,Proposed_amount__c
    FROM Plan__c
    WHERE Donation_Year__c = :lastYear
    AND Facility__c NOT IN :facilityIdsAlreadyInNewYear
];

//Populate list with new plans based on queried plans.
List<Plan__c> plansForNewYear = new List<Plan__c>();
for (Plan__c p : plansFromLastYear) {
    Plan__c planForNewYear = new Plan__c(
        Plan_Status__c = 'Draft',
        Donation_Year__c = newYear,
        Facility__c = p.Facility__c
    );
    plansForNewYear.add(planForNewYear);
}

//Insert new plans.
insert plansForNewYear;




//Query plans from last year that have not been created for facilities in new year (to prevent duplicates).
List<Plan__c> planForNewYear = [
    SELECT Approved_Amount__c,Donation_Year__c,Facility__c,Id,Name,Plan_Status__c,Proposed_amount__c
    FROM Plan__c
    WHERE Donation_Year__c = :newYear
    ];

//Placeholder for all new contributions
List<Contribution__c> contributionList = new List<Contribution__c>();
Set<String> pIds = new Set<String>();
//Loop through all new plans.
for (Plan__c p : planForNewYear) {

    //String myId = p.Name;
 pIds.add(p.Id);

 
 //get contributions for this plan
 List<Contribution__c> contributionsFromLastYear = [
 SELECT Amount__c,Community_Organization__c,Contribution_Status__c,Name,Project_Title__c FROM Contribution__c where Plan__c In :pIds ];
 
 //Loop through all contributions from last year for this plan
 for(Contribution__c cont : contributionsFromLastYear ){

 //add the values to a new contribution
 Contribution__c contributions = new Contribution__c();
 contributions.Previous_Amount__c = cont.Amount__c;
 contributions.Contribution_Status__c =  cont.Contribution_Status__c;
 contributions.Community_Organization__c = cont.Community_Organization__c;
 contributions.Project_Title__c = cont.Project_Title__c;
 contributions.Plan__c = cont.Name;
      
 contributionList.add(contributions);
 }
 
 insert contributionList;

}