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 aryasugandha arya 

Update field of contact with field of custom setting

I have created one custom setting named as "Fee_Structure__c" with fields semester__c and total_fee__c where total_fee__c value is different for different semester.I want to get the value of total_fee__c on one of the field of contact which will update the field by matching semester in both contact and custom setting.How we can do it??  
Veenesh VikramVeenesh Vikram
Something like this will do:
trigger updateFee on Contact (before insert, before update){
	//Get all values of custom setting in a map
	Map<String, Fee_Structure__c> mapCustomSetting = new Map<String, Fee_Structure__c> ();
	mapCustomSetting = Fee_Structure__c.getAll();
	
	for(contact con : trigger.new){
		con.CUSTOM_TOTAL_FEE_FIELD = mapCustomSetting.get(con.SEMESTER_FIELD);
	}
}

Kindly mark as solved if it helps!

Veenesh
sugandha aryasugandha arya
Hii Veenesh,Thanks for the reply but i think you are not getting it right.I want to match both fields named as semester in custom setting and contact if the values are same then want to update the fee field of the contact with the value in field of custom setting
 
Veenesh VikramVeenesh Vikram
Yes Sugandha,

I got it, I guess this code will work. I request you to try to run this one and see if its working as expected.
trigger updateFee 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)
	Map<String, Fee_Structure__c> mapCustomSetting = new Map<String, Fee_Structure__c> ();
	mapCustomSetting = Fee_Structure__c.getAll();
	
	for(contact con : trigger.new){
		con.Fee_Structure__c = mapCustomSetting.get(con.semester__c).Fee_Structure__c;
	}
}

Veenesh