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
tdevmantdevman 

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. 

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

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

Vinit_KumarVinit_Kumar

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 :

 

for(ExpenseLineItems__c eli: eliList)
{
eliList.add(Expense.__r.Contact__c);
}
tdevmantdevman

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 ...

 

 

Vinit_KumarVinit_Kumar

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]);  

 

 

This was selected as the best answer
tdevmantdevman

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

 

 

tdevmantdevman

 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..

 

Vinit_KumarVinit_Kumar

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());
}