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