1

So I am working on the code below to migrate the attachments of an old object to a new object. I have scraped the web for code that could do this.

I am stuck though on this specific for loop:

for(Attachment temp: attlist){
    tempatt=temp.clone(false,false);
    tempatt.parentid=ids.Selection_Record__c;

I realize the issue is with the last line which gives the following error: Initial term of field expression must be a concrete SObject: List . How else do I assign a specific field to the parent ID on the opportunity object other then through a list?

To clarify further, I want the new parentID number to be assigned by a custom field on the opportunity object. The field contains the record id of the new custom object record that i want the attachment to be cloned to.

Please find the entire code below:

trigger SecondStepClone on Opportunity (After update) {

        List<Attachment> ToInsertList = new List<Attachment>();  
        Attachment tempatt; 

    List<id> ids = new List<id>(); 

    for(Opportunity curr: [SELECT ID, Selection_Record__c 
                           FROM Opportunity 
                           WHERE RecordTypeID = '012j0000000A8g3' 
        OR RecordTypeID = '012j00000000Bvo' OR RecordTypeID = '012j00000000Bvj'])
    ids.add(curr.id);

    List<Attachment> attlist = [SELECT id, parentid, body, name 
                                FROM Attachment 
                                WHERE parentid IN: ids];

    for(Attachment temp: attlist){
        tempatt=temp.clone(false,false);
        tempatt.parentid=ids.Selection_Record__c;

        ToInsertList.add(tempatt);

    }    
    insert ToInsertList
    delete attlist
}

Thank you in advance for your help!

  • Don't hard-code your ids. Ever. It's one of the most basic and obvious best practices, and there is never a good reason to violate it. – Adrian Larson Oct 19 '16 at 2:02
0

You need the proper field mapping to move attachments one level above, Using a map could a good option here,

Updated code with Map

trigger SecondStepClone on Opportunity (After update) {
    List<Attachment> ToInsertList = new List<Attachment>();  
    Attachment tempatt; 

    //List<id> ids = new List<id>(); 
    Map<Id, Id> newParentMap = new Map<Id, Id>();

    for(Opportunity curr: [SELECT ID, Selection_Record__c 
                            FROM Opportunity 
                            WHERE RecordTypeID = '012j0000000A8g3' 
                            OR RecordTypeID = '012j00000000Bvo' 
                            OR RecordTypeID = '012j00000000Bvj']) {

        newParentMap.put(curr.Id, curr.Selection_Record__c);        //Change
    }

    List<Attachment> attlist = [SELECT id, parentid, body, name 
                                FROM Attachment 
                                WHERE parentid 
                                IN: newParentMap.keyset()];  //Changed the param

    for(Attachment temp: attlist){
        tempatt=temp.clone(false,false);
        tempatt.parentid=newParentMap.get(temp.ParentId);   //change

        ToInsertList.add(tempatt);
    }    
    insert ToInsertList
    delete attlist
}

Edit 1 : Explanation on Maps

Map is a data structure that basically falls under the collection. Map is a collection of key value pairs;

In this case, I have stored map as OpportunityId(Key) and Selection_Record__c(Value) newParentMap.put(curr.Id, curr.Selection_Record__c);

In maps if key is given then map returns the corrospoing value for that key. So, if we pass key as OpportunityId we will get the Selection_Record's Id;

For more detailed information you may find these links useful.

  • Hi Ishwar, thanks for replying to my question. I am still a noob when it comes to Apex coding, and have had no experience with Maps. In your code when you write: tempatt.parentid=newParentMap.get(temp.ParentId) – ConstantineC Oct 18 '16 at 18:54
  • Accidently pressed enter there. When you write 'tempatt.parentid=newParentMap.get(temp.ParentId)' does that pull the Selection_Record__c field that was added to the map? I do not see how the code assigns the tempatt.parentid the Selection_Record__c field. – ConstantineC Oct 18 '16 at 18:56
  • @ConstantineC, I have updated the answer to understand it quickly, have a look at it once. – Ishwar Mete Oct 19 '16 at 1:08
  • Also, As you are new in this community, It's worth mentioning that, If this has helped you hit the upvote and finally if your problem is solved hit the accept so that others may get some help if they face same situation. – Ishwar Mete Oct 19 '16 at 1:09
  • Thanks again for the help and thank you for the lesson on maps Ishwar! I have hit accept on your post – ConstantineC Oct 19 '16 at 16:29
0

Please remember, you can never use a list like an SObject and use the . operator to get the fields.

To fix your code, modify it into this:

for(Attachment temp: attlist){
    tempatt=temp.clone(false,false);
    tempatt.parentid=temp.parentid; //This is the line changed

    ToInsertList.add(tempatt);
}    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.