+ Start a Discussion
james1986james1986 

copying data from object1 to object2

I'm trying automate the entry of recurring monthly donations for a non-profit. The process involves an object called monthly_gift__c (where I want to copy from) and an object called donation__c (where I want to copy to).

 

I keep gettinging Error: Compile Error: Incompatible element type SOBJECT:Monthly_Gift__c for collection of SOBJECT:Donation__c at line 22 column 9, and I assume its because the columns in the 2 table don't line up perfectly.

 

I'm not sure how to modify the list/collection before inserting, or if that is even the best method to use here.

 

Any help would be much appreciated,

James

 

public class updatepledges
{


    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
       
        public updatepledges(ApexPages.StandardController controller)
        {
            //initialize the stanrdard controller
            this.controller = controller;
            
   

 


    List<Donation__c> donations = new List<Donation__c>();
    for (Monthly_Gift__c mg: [SELECT Account__c, Contact__c, PaymentType__c FROM Monthly_Gift__c])
// the last line is causing the error. how can i extrract from donation__c and insert into Monthly_Gift__c??? 

{

        Monthly_Gift__c Newmg = mg.clone(false);
        donations.add(Newmg);
    }
    insert donations;

 

 

// the same problem will presumably come up again in the next few lines of code, where i insert detail records

    List<Allocation__c> allocations = new List<Allocation__c>();
    for (Donation__c currdon : donations)  
    {
        for(Monthly_Gift_Allocations__C mga: [SELECT Amount__c, Monthly_Gift__c, Program__c, Stream__c FROM Monthly_Gift_Allocations__c])
        {  
            Monthly_Gift_Allocations__c Newmga = mga.clone(false);
            allocations.add(Newmga);
        }
    }
    insert allocations;

}
}

Best Answer chosen by Admin (Salesforce Developers) 
kyle.tkyle.t

You are trying to a Monthly Gift to a list of donations.  That is like trying to add an account to a list of contacts.  What you need to do is create the donation out of the monthly gift and then add that to the list of donations.  Once we have that we can build on it.

 

 

List<Donation__c> donations = new List<Donation__c>();
for (Monthly_Gift__c mg: [SELECT Account__c, Contact__c, PaymentType__c FROM Monthly_Gift__c])
{
	Donation__c d = new Donation__c();
	d.Account__c = mg.Account__c;
	d.Contact__c = mg.Contact__c;
	d.PaymentType__c = mg.Payment__c;
	donations.add(d);	
}
insert donations;

 

 

Now the next part is going to be a bit trickier.  From what I can see you need to pull all of the monthly allocations and create allocation records for them, tying them to the donations that were just created.  I am assuming, of course, that you can have more than 1 allocation tied to a donation (otherwise the allocation would be 100% and negate the need for the allocation object).  you currently have a SOQL query in a for loop so you will quickly slam into a govenor limit so we need to take a different approach.  The idea is to create a mapping of the Monthly donation to the new donation we just created.

 

Leveraging a Map, you can now loop through all of the Monthly Gift Allocations and create new allocations.  You then check the Map to find the donation just created that is assoiated with the Monthly Gift from the Monthly Gift Allocation and link that to the allocation.  (sorry, I know that is confusing, but since it is your app, you probably understand it more than me).  Here is what I came up with... this was done in a text pad so there may be some syntax errors.  I hope this helps.

 

 

public class updatepledges
{
    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
       
    public updatepledges(ApexPages.StandardController controller)
	{
		//initialize the stanrdard controller
		this.controller = controller;
	
		List<Donation__c> donations = new List<Donation__c>();
		Map<ID,Donation__c> mgDonationMap = new Map<ID,Donation__c>();
		for (Monthly_Gift__c mg: [SELECT Account__c, Contact__c, PaymentType__c FROM Monthly_Gift__c])
		{
			//Create a donation and populate it. then add it to the list
			Donation__c d = new Donation__c();
			d.Account__c = mg.Account__c;
			d.Contact__c = mg.Contact__c;
			d.PaymentType__c = mg.Payment__c;
			donations.add(d);
			//add the id of the monthly donation and the new dontation to a map to be used for the allocations
			mgDonationMap.put(mg.id,d);
		}
		insert donations;

		
		List<Allocation__c> allocations = new List<Allocation__c>();
		for(Monthly_Gift_Allocations__C mga: [SELECT Amount__c, Monthly_Gift__c, Program__c, Stream__c FROM Monthly_Gift_Allocations__c])
		{  
			Allocation__c a = new Allocation__c;
			a.Amount__c = mga.Amount__c;
			//This is the line that is important. 
			//associate the allocation to the correct Donation 
			//by finding the monthly gift in the map which the monthly gift allocation is tied to
			a.Donation__c = mgDonationMap.get(Monthly_Gift__c).id;
			a.Program__c = mga.Program__c;
			a.Stream__c = mga.Stream__c;
			allocations.add(Newmga);
		}
		insert allocations;
	}
}

 

 

All Answers

kyle.tkyle.t

You are trying to a Monthly Gift to a list of donations.  That is like trying to add an account to a list of contacts.  What you need to do is create the donation out of the monthly gift and then add that to the list of donations.  Once we have that we can build on it.

 

 

List<Donation__c> donations = new List<Donation__c>();
for (Monthly_Gift__c mg: [SELECT Account__c, Contact__c, PaymentType__c FROM Monthly_Gift__c])
{
	Donation__c d = new Donation__c();
	d.Account__c = mg.Account__c;
	d.Contact__c = mg.Contact__c;
	d.PaymentType__c = mg.Payment__c;
	donations.add(d);	
}
insert donations;

 

 

Now the next part is going to be a bit trickier.  From what I can see you need to pull all of the monthly allocations and create allocation records for them, tying them to the donations that were just created.  I am assuming, of course, that you can have more than 1 allocation tied to a donation (otherwise the allocation would be 100% and negate the need for the allocation object).  you currently have a SOQL query in a for loop so you will quickly slam into a govenor limit so we need to take a different approach.  The idea is to create a mapping of the Monthly donation to the new donation we just created.

 

Leveraging a Map, you can now loop through all of the Monthly Gift Allocations and create new allocations.  You then check the Map to find the donation just created that is assoiated with the Monthly Gift from the Monthly Gift Allocation and link that to the allocation.  (sorry, I know that is confusing, but since it is your app, you probably understand it more than me).  Here is what I came up with... this was done in a text pad so there may be some syntax errors.  I hope this helps.

 

 

public class updatepledges
{
    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
       
    public updatepledges(ApexPages.StandardController controller)
	{
		//initialize the stanrdard controller
		this.controller = controller;
	
		List<Donation__c> donations = new List<Donation__c>();
		Map<ID,Donation__c> mgDonationMap = new Map<ID,Donation__c>();
		for (Monthly_Gift__c mg: [SELECT Account__c, Contact__c, PaymentType__c FROM Monthly_Gift__c])
		{
			//Create a donation and populate it. then add it to the list
			Donation__c d = new Donation__c();
			d.Account__c = mg.Account__c;
			d.Contact__c = mg.Contact__c;
			d.PaymentType__c = mg.Payment__c;
			donations.add(d);
			//add the id of the monthly donation and the new dontation to a map to be used for the allocations
			mgDonationMap.put(mg.id,d);
		}
		insert donations;

		
		List<Allocation__c> allocations = new List<Allocation__c>();
		for(Monthly_Gift_Allocations__C mga: [SELECT Amount__c, Monthly_Gift__c, Program__c, Stream__c FROM Monthly_Gift_Allocations__c])
		{  
			Allocation__c a = new Allocation__c;
			a.Amount__c = mga.Amount__c;
			//This is the line that is important. 
			//associate the allocation to the correct Donation 
			//by finding the monthly gift in the map which the monthly gift allocation is tied to
			a.Donation__c = mgDonationMap.get(Monthly_Gift__c).id;
			a.Program__c = mga.Program__c;
			a.Stream__c = mga.Stream__c;
			allocations.add(Newmga);
		}
		insert allocations;
	}
}

 

 

This was selected as the best answer
james1986james1986

thanks so much, Kyle. This makes sense.

the only difficulty I am having is with a.Donation__c = mgDonationMap.get(Monthly_Gift__c).id;

I am getting Error: Compile Error: Variable does not exist: mg.Id at line 35 column 47. I've tried using mg, mg.id and no luck. Should I be including Id in the SELECT statement? Please let me know if you have any ideas.

thanks

James

james1986james1986

It is actually compiling now, using the code below.

 

public class updatepledges
{
    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
       
    public updatepledges(ApexPages.StandardController controller)
    {
        //initialize the stanrdard controller
        this.controller = controller;
    
        List<Donation__c> donations = new List<Donation__c>();
        Map<ID,Donation__c> mgDonationMap = new Map<ID,Donation__c>();
        for (Monthly_Gift__c mg: [SELECT Id, Account__c, Contact__c, PaymentType__c FROM Monthly_Gift__c])
        {
            //Create a donation and populate it. then add it to the list
            Donation__c d = new Donation__c();
            d.Account__c = mg.Account__c;
            d.Contact__c = mg.Contact__c;
            d.Payment_Type__c = mg.PaymentType__c;
            donations.add(d);
            //add the id of the monthly donation and the new dontation to a map to be used for the allocations
            mgDonationMap.put(mg.id,d);
        }
        insert donations;

        
        List<Allocation__c> allocations = new List<Allocation__c>();
        for(Monthly_Gift_Allocations__C mga: [SELECT Amount__c, Monthly_Gift__c, Program__c, Stream__c FROM Monthly_Gift_Allocations__c])
        {  
            Allocation__c a = new Allocation__c();
            a.Amount__c = mga.Amount__c;
            //This is the line that is important. 
            //associate the allocation to the correct Donation 
            //by finding the monthly gift in the map which the monthly gift allocation is tied to
            a.Transaction__c = mgDonationMap.get(mga.Monthly_Gift__c).Id;
            a.Program__c = mga.Program__c;
            a.Stream__c = mga.Stream__c;
            allocations.add(a);
        }
        insert allocations;
    }
}

james1986james1986

seems to work! thanks!