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
sugandha arya 2sugandha arya 2 

Apex trigger updateFee1 caused an unexpected exception, contact your administrator: updateFee1: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0: Trigger.updateFee1: line 30, column 1

Hi,
my trigger is not showing error while saving  but it is dhowing error while creating new contact.I am not able to figure it out.
My requirement for trigger are:
I have one custom setting for fee structure,I have to get total fee in contact from custom setting by matching 'course__c','semester__c', 'program__c' and 'batch__c'.In contact 'course__c' and 'Program__c' are lookup fields but these fields are text data type in custom setting.On basis of matching these fields,i have to populate 'total_fee__c' in contact with custom setting field named as'total_fee__c'.
I have done with trigger.Please help me to resolve the issues.
Below is my trigger code:

//updated trigger for fee update in contact-------------->>

trigger updateFee1 on Contact (before insert, before update){
    //Get all values of custom setting in a map(Assuming that Semester is Key and Fee is value in custom setting)
    Set<String> semester=new Set<String>();
    List<Contact> course=new List<Contact>();
    Set<String> batch=new Set<String>();
    //List<Contact> program=new List<Contact>();
    course=[Select id,Courses__r.Name,Program__r.Name from Contact where id IN : Trigger.new];
    
    System.Debug('###' +course);
    for(Contact c:Trigger.new)
    {
    if(c.Term__c!=null)
    {
    semester.add(c.Term__c);
    }
    //if(c.Courses__c!=null){
    //course.add(c.Courses__c);
    //System.Debug('@@@' + course);
    //}
    if(c.Batch__c!=null){
    batch.add(c.Batch__c);
    }
    }
    //for(Id i : course){
    //String name=i.Name;
    //coursename.add(name);
    //}
     //If (semester != null &&  course[0].Courses__r.Name != null && course[0].Program__r.Name != null && batch != null){
    List<Fee_Structure__c> fee=[Select Course__c,Term__c,Batch__c,Total__c from Fee_Structure__c where Term__c IN : semester AND Course__c = : course[0].Courses__r.Name AND Batch__c IN : batch And Program__c =: course[0].Program__r.Name ];
   Map<String, Fee_Structure__c> mapfee = new Map<String, Fee_Structure__c> ();
    
    
    for(Fee_Structure__c f : fee){      
    mapfee.put(f.Term__c,f);
    }
    for(Contact c:Trigger.new){  
     if(c.Term__c!=null){
     Fee_Structure__c fees=mapfee.get(c.Term__C);
     if(fees!=null){
     c.Total_Fee_To_Be_Paid__c=fees.Total__c;
    }
    }
}
}
Anupama SamantroyAnupama Samantroy
Hi Sugandha,

Please find the below code for your help. You dont need to query the custom setting. You can get all the entries of it in a map and then iterate through it.
trigger updateFee1 on Contact (before insert, before update){
    //Get all values of custom setting in a map(Assuming that Semester is Key and Fee is value in custom setting)
    List<Contact> course=new List<Contact>();   
    course=[Select id,Courses__r.Name,Program__r.Name,Term__c,Batch__c from Contact where id IN : Trigger.new];
    map<String,Fee_Structure__c> mapFees = Fee_Structure__c.getAll();
    
    for(Contact c : course)
    {
		if(c.Term__c!=null && c.Batch__c!=null)
		{
			for(Fee_Structure__c fee: mapFees.values()){
				if(fee.Term__c == c.Term__c &&
				   fee.Course__c == c.Courses__r.Name &&
				   fee.Batch__c == c.Batch__c &&
				   fee.Program__c == Program__r.Name ){
					   c.Total_Fee_To_Be_Paid__c = fee.Total__c;
					   break;
				   }
			}
			
		}
		
    }    
}

Please let me know if that helps.

Thanks 
Anupama
 
sugandha arya 2sugandha arya 2
Hi Anupama,
Thanks for your reply,but it is still not working.field not getting updated 
Anupama SamantroyAnupama Samantroy
I would suggest please check all the necessary permissions for updating the field and put system.debug and check all values are correct.
Please let me know of it still doesnt work.

Thanks
Anupama
sugandha aryasugandha arya
Hi,
I have checked debug logs,I am getting values in debug logs but still not getting field updated.Then what would be the reason for this?can you suggest me?
sugandha aryasugandha arya
I have figured out the issue.When I am comparing the fiellds with same data type then it is working right.but when I am comparing lookup field of contact with custom setting's text field then it is not working.for example:- if(fee.course__c==c.courses__r.Name)

Here ,I am giving my code tat is working fine:

trigger updateFee1 on Contact (before insert, before update){
    //Get all values of custom setting in a map(Assuming that Semester is Key and Fee is value in custom setting)
    List<Contact> course=new List<Contact>();   
    course=[Select id,Courses__r.Name,Program__r.Name,Term__c,Batch__c,Courses__c,Program__c,Total_Fee_To_Be_Paid__c  from Contact where id IN : Trigger.new];
    
    map<String,Fee_Structure__c> mapFees = Fee_Structure__c.getAll();
    System.debug('###' +mapFees);
    //if(trigger.isbefore || trigger.isupdate){
    for(Contact c : Trigger.new)
    {
        if(c.Term__c!=null && c.Batch__c!=null && c.Program__c!=null && c.Courses__c!=null)
        {
            for(Fee_Structure__c fee: mapFees.values()){
                if(fee.Term__c == c.Term__c &&
                   //fee.Course__c == c.Courses__r.Name &&
                   fee.Batch__c == c.Batch__c ){
                   //fee.Program__c == c.Program__r.Name ){
                       c.Total_Fee_To_Be_Paid__c = fee.Total__c;
                       System.debug('$$$' +fee.Total__c );
                       break;
                   }
            }
            
        }
        
    }    
}

but i have to compare it with lookup fields,if possible then can you help me?