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
tzuvytzuvy 

Custom Settings and SF Custom Object fields

Hi,

 

I'm writing an APEX class that should look for some strings on an xml and populate a SF custom object with its respective fields and values.

 

The function currently works fine as follows:

 

 public Chat_Details__c UpsertChatDetails ( String sessionId, MAP<String,String> XMLNode){

 

Chat_Details__c chatInfo = new Chat_Details__c();

 

pChatInfo.VI_Country__c = getValueFromXML(sessionId , XMLNode , 'geoCountry');
pChatInfo.VI_IP_Address__c = getValueFromXML(sessionId , XMLNode , 'geoIP');
pChatInfo.VI_City__c = getValueFromXML(sessionId , XMLNode , 'geoCity');
pChatInfo.VI_State_Province__c = getValueFromXML(sessionId , XMLNode , 'geoReg');
pChatInfo.VI_ISP__c = getValueFromXML(sessionId , XMLNode , 'geoISP');

 

return chatinfo;

 

}

 

I would like to create the same value assignment by using custom settings in order to give standard users the ability to add new fields to the custom object and be able to look in the XML for the right value.

 

For this I created a custom settings object called ChatDetailFieldMapping which has two fields (SF API Field Name and XML attribute to be looked for) see attached screenshot.

 

Custom Settings

 

My question is, how would I go about the same function I created to be able to look into the Custom Setting object and populate the fields based on the custom mapping?

 

Thanks

 

Tzuvy

 

 


TheIntegratorTheIntegrator

from what I understand, this should work

 

public List<ChatDetailFieldMapping__c> cdMapping;

public Chat_Details__c UpsertChatDetails ( String sessionId, MAP<String,String> XMLNode){

	cdMapping = new List<ChatDetailFieldMapping__c>([SELECT Id, Name, LP_UDE__c FROM ChatDetailFieldMapping__c]);
 
	Chat_Details__c chatInfo = new Chat_Details__c();
		pChatInfo.VI_Country__c = getValueFromXML(sessionId , XMLNode , getMapping('VI_Country__c'));
		pChatInfo.VI_IP_Address__c = getValueFromXML(sessionId , XMLNode , getMapping('VI_IP_Address__c'));
		pChatInfo.VI_City__c = getValueFromXML(sessionId , XMLNode , getMapping('VI_City__c'));
		pChatInfo.VI_State_Province__c = getValueFromXML(sessionId , XMLNode , getMapping('VI_State_Province__c'));
		pChatInfo.VI_ISP__c = getValueFromXML(sessionId , XMLNode , getMapping('VI_ISP__c'));
 
	return chatinfo;
}

public String getMapping(String fieldName){
	
	String strName;
	for(ChatDetailFieldMapping__c cd: cdMapping){
		if(cd.Name == fieldName)
			strName=cd.LP_UDE__c;
	}
	return strName;
}

 

let me know if this is what you are looking for