You need to sign in to do that
Don't have an account?

m-detail trigger question
This class is invoked by a before insert trigger on the ExpenseLineItems object. It's supposed to assign the sales_rep__c which is the contact.ownerid associated to expense.contact__c. But when I debug the following, the size is correct but the contents of the list if null.
Try below code,it should work :-
List<Id> cList = new List<Id>();
List<Id> expenseIdList = new List<Id>();
List<expense__c> expenseList = new List<expense__c>();
for(ExpenseLineItem__c eli: eliList)
{
expenseIdList.add( eli.Expense__c);
}
expenseList = [select Contact__c from expense__c where id in:expenseIdList];
for(expense__c tm : expenseIdList){
cList.add(tm.Contact__c);
}
Map<Id,Contact> cMap = new Map<Id, Contact>([SELECT OwnerId FROM Contact WHERE Id =:cList]);
All Answers
It is really confusing,you are iterating on eliList in for loop and again adding to eliList.What are you trying to achieve,please make your requirement clear so that I can help.
The below code is not correct :
Hi My apologies but i accidenlty didn;t include an important section in my code due to copy and paste error. I have a lookup called Sales_Rep__c on the expense_line_item_c object which is a user lookup. Contact is master to expense__c detail and expense__c is master to expenselineitem__c detail ...
Try below code,it should work :-
List<Id> cList = new List<Id>();
List<Id> expenseIdList = new List<Id>();
List<expense__c> expenseList = new List<expense__c>();
for(ExpenseLineItem__c eli: eliList)
{
expenseIdList.add( eli.Expense__c);
}
expenseList = [select Contact__c from expense__c where id in:expenseIdList];
for(expense__c tm : expenseIdList){
cList.add(tm.Contact__c);
}
Map<Id,Contact> cMap = new Map<Id, Contact>([SELECT OwnerId FROM Contact WHERE Id =:cList]);
It makes sense to me in the logic but right now getting this syntax error:
Compile Error: Loop variable must be of type Id at line 20 column 24
i think i am close now but after i populate the map how do i iterate through the eliList?? i think i need to an a loop through eliList.size() ...feel like we ae close..
Try below :
Map<Id,Contact> cMap = new Map<Id, Contact>([SELECT OwnerId FROM Contact WHERE Id =:cList]);
for (ExpenseLineItem__c eli: eliList)
{
try
{
for(Contact c : cMap.values()){
if (c != null)
eli.Sales_Rep__c = c.OwnerId;
}
catch(System.Exception e)
{
eli.addError('Unexpected error, please contact support');
System.debug(e.getMessage());
}