+ Start a Discussion

Dynamic code to access custom setting

Hi There,


My req. is,I've Account obj as a master and Contract(custom obj)as detail.I've created 2 custom fields on Account eg.Asset Management Calculated Status,Built Environment Calculated Status with picklist values 'Customer' and 'Ex-Customer'.and Contract have 2 recordtypes say,'Asset Management','Built Environment'.Now whenever user select Contract recordtype as 'Asset Management' with Status 'Active' then it's corresponding Account 'Asset Management Calculated Status' field should get updated as 'Customer' value. and in other case, if user select Status as 'Terminated' then it's corresponding Account 'Asset Management Calculated Status' field should be updated as 'Ex-Customer'.


Business wants to write trigger on Contract and call apex class from there and class will hold all the logic......


plus I've to use custom setting for this scenario.in future if more Contract recordtype added then no need to touch code it should run dynamically......here are details:


Created custom setting named Record Maping added 2 custom fields and thru manage button inserted 2 records.

Custom Setting : Record Mapping


Record Type Name: Asset Management

Field Name :Asset Management Calculated Status


Record Type Name: Built Environment

Field Name :Built Environment Calculated Status


here is my code:


trigger ContractUpdate on MITIE_Contract__c (after insert,after update) {

// Creating an object named 'obj' of class "AccountUpdate"
    AccountUpdate obj = new AccountUpdate();
    system.debug('object calling 1');
  // Calling the 'CheckCondition' function of class "AccountUpdate"



Public with sharing class AccountUpdate{

List<ID> accid= New List<ID>();

public void CheckCondition(MITIE_Contract__c[] m){

List<Record_Mapping__c> mcs = Record_Mapping__c.getAll().values();

//For Asset Management recordtpe on Contract
for(integer j=0;j<mcs.size();j++){
if(m[0].Status__c =='Active' && mcs[j].RecordType_Name__c=='Asset Management' &&m[0].Organisation_Name__c != null){

List<Account> oppList = [SELECT id, Asset_Management_Calculated_Status__c FROM Account WHERE id in :accid]; 

for(integer i = 0 ; i < oppList.size(); i++){
oppList[i].Asset_Management_Calculated_Status__c ='Customer';
update oppList;







Didnt have time to read all your ques but you might be able to use


String dynamicCustomSettingObj = decide which CS;


 Database.query('Select ... From ' + dynamicCustomSettingObj + '....');




but how can I map this custom setting to Account fields to update it's value.