You need to sign in to do that
Don't have an account?
Tamika 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;
}
//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;
}
//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;
}