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
Michael Kolodner 13Michael Kolodner 13 

Newby needs code help on my method, please

I'm working on a method to take a phile of ContentDocumentLinks, find those that are linked to Expense__c, and then put those Expense__c records into a list. The bold line below won't compile with the error: Method does not exist or incorrect signature: void add(Id) from the type List<Expense__c>
I know this must be a simple fix, but help, please?

    //First attempted method:
    public list<Expense__c> Transformer(List<ContentDocumentLink> incomingcdls){
        //1. instantiate a list to hold the Expenses
        list<Expense__c> returnedexpenses = new list<Expense__c>();
        
        //2. Loop through the incoming CLDs. If the LinkedEntityID is that of an expense, add it to the list.
        for(ContentDocumentLink currentcdl : incomingcdls){
            if(currentcdl.LinkedEntityId.getSObjectType() == Schema.Expense__c.getSObjectType()){
               System.debug(currentcdl + ' was linked to a ' + currentcdl.LinkedEntityId.getSObjectType() + ' so it should go in the list.');
               //Now how to add it to the list? The below doesn't work
               returnedexpenses.add(currentcdl.LinkedEntityId);

            } else {
                System.debug(currentcdl + ' was linked to a ' + currentcdl.LinkedEntityId.getSObjectType() + ' so it should NOT go in the list.');
            }

    }
            
        //3. return the list of Expenses
        return returnedexpenses;
}
Best Answer chosen by Michael Kolodner 13
Maharajan CMaharajan C
Hi  Michael,

You are adding the Id (LinkedEntityId) to List so it throws the error.

 public list<Expense__c> Transformer(List<ContentDocumentLink> incomingcdls){
        //1. instantiate a list to hold the Expenses
        list<Expense__c> returnedexpenses = new list<Expense__c>();
        Set<Id> LinkedEntityIdSet = new Set<Id>();
        
        //2. Loop through the incoming CLDs. If the LinkedEntityID is that of an expense, add it to the list.
        for(ContentDocumentLink currentcdl : incomingcdls){
            if(currentcdl.LinkedEntityId.getSObjectType() == Schema.Expense__c.getSObjectType()){
               System.debug(currentcdl + ' was linked to a ' + currentcdl.LinkedEntityId.getSObjectType() + ' so it should go in the list.');
               //Now how to add it to the list? The below doesn't work
              // returnedexpenses.add(currentcdl.LinkedEntityId);
                 LinkedEntityIdSet.add(currentcdl.LinkedEntityId);
            } else {
                System.debug(currentcdl + ' was linked to a ' + currentcdl.LinkedEntityId.getSObjectType() + ' so it should NOT go in the list.');
            }
           
    }

If(LinkedEntityIdSet.size() > 0)
{
            returnedexpenses = [Select Id from Expense__c where Id IN : LinkedEntityIdSet];  // Add the Needed fields you want from Expense__c object
}

            
        //3. return the list of Expenses
        return returnedexpenses;
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
Raj

 

All Answers

Maharajan CMaharajan C
Hi  Michael,

You are adding the Id (LinkedEntityId) to List so it throws the error.

 public list<Expense__c> Transformer(List<ContentDocumentLink> incomingcdls){
        //1. instantiate a list to hold the Expenses
        list<Expense__c> returnedexpenses = new list<Expense__c>();
        Set<Id> LinkedEntityIdSet = new Set<Id>();
        
        //2. Loop through the incoming CLDs. If the LinkedEntityID is that of an expense, add it to the list.
        for(ContentDocumentLink currentcdl : incomingcdls){
            if(currentcdl.LinkedEntityId.getSObjectType() == Schema.Expense__c.getSObjectType()){
               System.debug(currentcdl + ' was linked to a ' + currentcdl.LinkedEntityId.getSObjectType() + ' so it should go in the list.');
               //Now how to add it to the list? The below doesn't work
              // returnedexpenses.add(currentcdl.LinkedEntityId);
                 LinkedEntityIdSet.add(currentcdl.LinkedEntityId);
            } else {
                System.debug(currentcdl + ' was linked to a ' + currentcdl.LinkedEntityId.getSObjectType() + ' so it should NOT go in the list.');
            }
           
    }

If(LinkedEntityIdSet.size() > 0)
{
            returnedexpenses = [Select Id from Expense__c where Id IN : LinkedEntityIdSet];  // Add the Needed fields you want from Expense__c object
}

            
        //3. return the list of Expenses
        return returnedexpenses;
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
Raj

 
This was selected as the best answer
Michael Kolodner 13Michael Kolodner 13
Thanks, Raj, I'm about to go try this. This feels a little clunky, though. Is there not a way to just take the ID and put it into the list using some system method (not sure if that's the right term) that would take an ID and return the actual SObject? I'm concerned that the SOQL query within the loop could result in some governor limits when I try to do this in bulk.
Michael Kolodner 13Michael Kolodner 13
It does work this way. But if you know the answer to my question about just assigning directly from some method on the Id to put it in the List, without going via a Set, I'd love to hear back!
Maharajan CMaharajan C
First am not using the soql inside the for loop so you no need to worry about Governor limits. LinkedEntityId is the parentid of the content so you can't add this to expense list so simple way is add the entityid in set inside loop then check the set size and get qurey from the expense object based on the IDs in the set is the simplest and good approach so try this and let me know.
Michael Kolodner 13Michael Kolodner 13
Got it. Thank you so much for this!
ForceWaveForceWave
Try Below Code. The mistake your doing is adding Id's to the List of Expense__c.

public list<Expense__c> Transformer(List<ContentDocumentLink> incomingcdls){
        //1. instantiate a list to hold the Expenses
        list<Expense__c> returnedexpenses = new list<Expense__c>();
       // Use this to store expense Id's
       Set<Id> expensIds=new set<id>();
        
        //2. Loop through the incoming CLDs. If the LinkedEntityID is that of an expense, add it to the list.
        for(ContentDocumentLink currentcdl : incomingcdls){
            if(currentcdl.LinkedEntityId.getSObjectType() == Schema.Expense__c.getSObjectType()){
               System.debug(currentcdl + ' was linked to a ' + currentcdl.LinkedEntityId.getSObjectType() + ' so it should go in the list.');
               //Now how to add it to the list? The below doesn't work
               // collect all expense id's here.
               expensIds.add(currentcdl.LinkedEntityId);
            } else {
                System.debug(currentcdl + ' was linked to a ' + currentcdl.LinkedEntityId.getSObjectType() + ' so it should NOT go in the list.');
            }

    }
          // make sure you include all fields you need to return from the Expense__c object in the SOQL Query 
        returnedexpenses =[Select id, Name from Expense__c where Id in : expensIds] ;
        //3. return the list of Expenses
        return returnedexpenses;
}

Please mark it as best answer if this is helpful.