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
Robert Davis 1Robert Davis 1 

Apex Class - For Loop Question - Assign Data from one List to Another

Hello,

I am trying to pull data from the Account and Opportunity Objects and insert into a Custom Object named RD_SnapShot_Opportunity. I have the SOQL Query assigned to a list. I think I need to loop through the list and assign the elements in that list to another list and when done insert with a DML statement.

I can not figure out how to take a sObject of Opportuntity that results from the SOQL query and assign to a new List to insert into the table.

Please point me in the right direction. Have I assigned the Opportunity list resulting from the SOQL to the wrong type of sObject?
 
public class RD_SnapshotOpportunityAggregation {
    //To take key information from the Opportunity Object on a schedule to store in the OpportunitySnapshot Object
    
    public static void transferOpportunityData() {
        List<Opportunity> oppList = [ SELECT account.id, 
                                   account.name, 
                                   account.type, 
                                   account.industry, 
                                   account.recordtypeid, 
                                   account.recordtype.name, 
                                   account.ownerId, 
                                   Owner.firstname, 
                                   Owner.lastname, 
                                   Name, 
                                   StageName, 
                                   FTEs__c, 
                                   TotalNumberofSeats__c, 
                                   Amount, 
                                   Annualized_Revenue__c   
                                   FROM Opportunity
                                   ];
        
        // is the RD_Snapshot_Opportunity__c the correct object 
        for(RD_Snapshot_Opportunity__c snap: oppList) {
            //not sure how to assign values to the appropriate field in the snap sObject        
            snap.SsAccountId = oppList[1];
            snap.SsAccountName = oppList[2];
            snap.SsAccountType = oppList[3];
            snap.SsAccountIndustry = oppList[4];
            
            //and each field as we go down the list.
            
        }
        insert opp
    }
}

Any help would be appreciated. Thank you

Thanks in advance,
Robert
Best Answer chosen by Robert Davis 1
Bryan JamesBryan James
Lines 4 - 21 are fine the way they are. What you are doing there is creating a list / array of Opportunity records.
When you start your for loop however it breaks because you are telling the for loop that each record in oppList is a RD_Snapshot_Opportunity. This is not the case since you are trying to loop over your returned oppList records. Those records are of type Opportunity. So here its a simple fix of changing the RD_Snapshot_Opportunity__c snap to Opportunity opp.

The next problem is that you are then trying to create a new record of Type RD_Snapshot_Opportunity__c. To do this you need to instantiate a new record object like : RD_Snapshot_Opportunity__c snap = new RD_Snapshot_Opportunity__c();
**Note before you can add the newly created snapshots to a list you must have a list of them to add it to. So above your for loop create a list of type RD_Snapshot_Opportunity__c.

Now that we have created a new record of type RD_Snapshot_Opportunity__c we can begin setting the field data.
In your code you are attemping to assign a single fields value to an entire opportunity record. What I believe you are attempting to do however is to assign it to that field in an opportunity record. To do this you are going to pull the field data from the current opportunity record you are looping over and assign it to the feild like this:
snap.SsAccountId = opp.Account;
snap.SsAccountName = opp.Account.Name;
snap.SsAccountType = opp.Account.Type;
snap.SsAccountIndustry = opp.Account.Industry;

once you are done assigning the data you can add the snapshot record to our list
snapList.add(snap);
then outside your for loop on line 34 change insert opp to insert snapList.

when done it should look something like this

List<RD_Snapshot_Opportunity__c> snapList = new List<RD_Snapshot_Opportunity__c>();
for(Opportunity opp : oppList) {
    RD_Snapshot_Opportunity__c snap = new RD_Snapshot_Opportunity__c();
    snap.SsAccountId = opp.Account;
    snap.SsAccountName = opp.Account.Name;
    snap.SsAccountType = opp.Account.Type;
    snap.SsAccountIndustry = opp.Account.Industry;

    snapList.add(snap);
}
insert snapList;

hope this helps

All Answers

Veenesh VikramVeenesh Vikram
public class RD_SnapshotOpportunityAggregation {
    //To take key information from the Opportunity Object on a schedule to store in the OpportunitySnapshot Object
    
    public static void transferOpportunityData() {
        //Create a List of Custom Object, in which you will store new Records to be inserted
        List<RD_Snapshot_Opportunity__c> snapList = new List<RD_Snapshot_Opportunity__c>();
        //This Query is Fine
        List<Opportunity> oppList = [ SELECT account.id, 
                                   account.name, 
                                   account.type, 
                                   account.industry, 
                                   account.recordtypeid, 
                                   account.recordtype.name, 
                                   account.ownerId, 
                                   Owner.firstname, 
                                   Owner.lastname, 
                                   Name, 
                                   StageName, 
                                   FTEs__c, 
                                   TotalNumberofSeats__c, 
                                   Amount, 
                                   Annualized_Revenue__c   
                                   FROM Opportunity
                                   ];
        
        // Here Loop on Opportunity
        for(Opportunity opp: oppList) {
            //not sure how to assign values to the appropriate field in the snap sObject        
            snap.SsAccountId = opp.account.id;
            snap.SsAccountName = opp.account.name;
            snap.SsAccountType = opp.account.type;
            
            //and each field as we go down the list.
            
            //Add this Snap to the List
            snapList.add(snap);
        }
        
        //Insert SnapList if it has elements
        if(snapList.size() > 0){
            insert snapList;
        }
      insert opp
    }
}

Hope this helps.

Veenesh
Bryan JamesBryan James
Lines 4 - 21 are fine the way they are. What you are doing there is creating a list / array of Opportunity records.
When you start your for loop however it breaks because you are telling the for loop that each record in oppList is a RD_Snapshot_Opportunity. This is not the case since you are trying to loop over your returned oppList records. Those records are of type Opportunity. So here its a simple fix of changing the RD_Snapshot_Opportunity__c snap to Opportunity opp.

The next problem is that you are then trying to create a new record of Type RD_Snapshot_Opportunity__c. To do this you need to instantiate a new record object like : RD_Snapshot_Opportunity__c snap = new RD_Snapshot_Opportunity__c();
**Note before you can add the newly created snapshots to a list you must have a list of them to add it to. So above your for loop create a list of type RD_Snapshot_Opportunity__c.

Now that we have created a new record of type RD_Snapshot_Opportunity__c we can begin setting the field data.
In your code you are attemping to assign a single fields value to an entire opportunity record. What I believe you are attempting to do however is to assign it to that field in an opportunity record. To do this you are going to pull the field data from the current opportunity record you are looping over and assign it to the feild like this:
snap.SsAccountId = opp.Account;
snap.SsAccountName = opp.Account.Name;
snap.SsAccountType = opp.Account.Type;
snap.SsAccountIndustry = opp.Account.Industry;

once you are done assigning the data you can add the snapshot record to our list
snapList.add(snap);
then outside your for loop on line 34 change insert opp to insert snapList.

when done it should look something like this

List<RD_Snapshot_Opportunity__c> snapList = new List<RD_Snapshot_Opportunity__c>();
for(Opportunity opp : oppList) {
    RD_Snapshot_Opportunity__c snap = new RD_Snapshot_Opportunity__c();
    snap.SsAccountId = opp.Account;
    snap.SsAccountName = opp.Account.Name;
    snap.SsAccountType = opp.Account.Type;
    snap.SsAccountIndustry = opp.Account.Industry;

    snapList.add(snap);
}
insert snapList;

hope this helps
This was selected as the best answer
Robert Davis 1Robert Davis 1
Veenesh Vikram and Bryan James thank you very much. This is my first shot at this and I really appreciate all the help.

I will try tomorrow and let you know how it went. If it were up to me I would bestow Superhero status on both of you. Capes and all.

Robert
Veenesh VikramVeenesh Vikram
Hahaha.......