+ Start a Discussion
TBouscalTBouscal 

Add items to an existing List

I would like to understand how to add items to a wrapper, this doesn't compile.  I get Variable does not exist for each item in the list. I'm also at a loss for how to create an index that directly relates to the Case
 
public class TESTING {
    
    public class temptable{
        public Id cId{get;set;}
        public Case Case_obj{get;set;}    
        public Account Account_obj{get;set;}
        public Contact Contact_obj{get;set;}
        public string Email_DevName{get; set;}
    }
    
    public static void theCases(Case[] cs)  {
        
        List<temptable> temptable = new List<temptable>();
        
        String strTitle = '';
        String strReason = ''; 
        String strDetail = ''; 
        String strProduct = ''; 
        
        For (Case c:cs){
            temptable.cid = c.id;
            temptable.Case_obj = c;
            temptable.account_obj = c.Account;
            temptable.Contact_obj = c.Contact;
            
            if(c.Reason == 'Hot'){temptable[c].Email_DevName = 'Hot';}
            else if(c.Reason == 'Cold'){temptable[c].Email_DevName = 'Cold';}
            else {} // do nothing
        }
    }
}

 
Best Answer chosen by TBouscal
Malni Chandrasekaran 2Malni Chandrasekaran 2
TBouscal,
If I have understood your problem correct, you are trying to create list of temptable object using the case list data.
If yes, you have to create a temptable object (Not a list), copy the data and add the temptable object to the list.
You may change your code like below,

public class TESTING {
    
    public class temptable{
        public Id cId{get;set;}
        public Case Case_obj{get;set;}    
        public Account Account_obj{get;set;}
        public Contact Contact_obj{get;set;}
        public string Email_DevName{get; set;}
    }
    
    public static void theCases(Case[] cs)  {
        
        List<temptable> temptableList = new List<temptable>();
        temptable objTempTable ;
        String strTitle = '';
        String strReason = ''; 
        String strDetail = ''; 
        String strProduct = ''; 
        
        For (Case c:cs){
           objTempTable = new temptable();
            objTempTable.cid = c.id;
            objTempTable.Case_obj = c;
            objTempTable.account_obj = c.Account;
            objTempTable.Contact_obj = c.Contact;
            
            if(c.Reason == 'Hot'){objTempTable.Email_DevName = 'Hot';}
            else if(c.Reason == 'Cold'){objTempTable.Email_DevName = 'Cold';}
            temptableList.add(objTempTable);
        }
    }
}

Please provide more details if my understanding about your query is not correct. If this helps, please mark it as best answer.

Thanks.

All Answers

Malni Chandrasekaran 2Malni Chandrasekaran 2
TBouscal,
If I have understood your problem correct, you are trying to create list of temptable object using the case list data.
If yes, you have to create a temptable object (Not a list), copy the data and add the temptable object to the list.
You may change your code like below,

public class TESTING {
    
    public class temptable{
        public Id cId{get;set;}
        public Case Case_obj{get;set;}    
        public Account Account_obj{get;set;}
        public Contact Contact_obj{get;set;}
        public string Email_DevName{get; set;}
    }
    
    public static void theCases(Case[] cs)  {
        
        List<temptable> temptableList = new List<temptable>();
        temptable objTempTable ;
        String strTitle = '';
        String strReason = ''; 
        String strDetail = ''; 
        String strProduct = ''; 
        
        For (Case c:cs){
           objTempTable = new temptable();
            objTempTable.cid = c.id;
            objTempTable.Case_obj = c;
            objTempTable.account_obj = c.Account;
            objTempTable.Contact_obj = c.Contact;
            
            if(c.Reason == 'Hot'){objTempTable.Email_DevName = 'Hot';}
            else if(c.Reason == 'Cold'){objTempTable.Email_DevName = 'Cold';}
            temptableList.add(objTempTable);
        }
    }
}

Please provide more details if my understanding about your query is not correct. If this helps, please mark it as best answer.

Thanks.
This was selected as the best answer
Glyn Anderson 3Glyn Anderson 3
I agree with Malni.  A slightly cleaner implementation is to use a constructor in your temptable class, and use the power of properties a bit more:

<pre>
public class TESTING {

    private static Set<String> validReasons = new Set<String>{ 'Hot', 'Cold' };

    public class temptable {
        public Id cId { get { return Case_obj.id; } }
        public Case Case_obj { get; set; }    
        public Account Account_obj { get { return Case_obj.Account; } }
        public Contact Contact_obj { get { return Case_obj.Contact; } }
        public string Email_DevName { get; set; }

        // constructor
        public temptable( Case c ) {
            this.Case_obj = c;
            this.Email_DevName = validReasons.contains( c.Reason ) ? c.Reason : null;
        }
    }

    public static void theCases( Case[] cs )  {

        List<temptable> temptableList = new List<temptable>();
        for ( Case c : cs ) temptableList.add( new temptable( c ) );
    }
}
</pre>
 
TBouscalTBouscal
Thank you both for your responses, in each I get variable not defined errors for c and/or cid.
Perhaps if I explain the process I'm trying to implement...

When a Case is update I want to send an email to the Contact on the Case and link the activity to the Case and the Contact.
I have 8 unique email templates that will be selected based on the values of two or three fields on the Case triggering the process.

 
Malni Chandrasekaran 2Malni Chandrasekaran 2
TBouscal,
Please replace this line 
   public static void theCases(Case[] cs)
with
   public static void theCases(List<Case> cs)

and let me know
TBouscalTBouscal
thanks folks!