I have a trigger which fires after I insert a applicant record. In the trigger, I want all the fields entered by the applicant in the map as keys and their corresponding values as value in a map.

How to go about this?

If I create a map like Map<String,Applicant__c> it throws error for the decimal fields I have. Please help. This is the code I have so far but it looks inefficient.

criteriaList=new LIST<CustBusinessPro__Criteria__c>([SELECT Id,Name,CustBusinessPro__Where_Clause__c FROM CustBusinessPro__Criteria__c] );

for (CustBusinessPro__Criteria__c cd: criteriaList) {

     for(CustBusinessPro__Applicant__c app:Trigger.new)
         System.debug('>>>>>>>>>>>>>>>>>'+ cd.CustBusinessPro__Where_Clause__c);
         test=test.replaceAll(' CustBusinessPro__Country__c',app.CustBusinessPro__Country__c);
         test=test.replaceAll('CustBusinessPro__Gender__c ',app.CustBusinessPro__Gender__c);
         test=test.replaceAll('CustBusinessPro__Marital_Status__c ',app.CustBusinessPro__Marital_Status__c);
         test=test.replaceAll('CustBusinessPro__City__c ',app.CustBusinessPro__City__c);
         System.debug('>>>>>>>>>>>>>>>>>value after replacing'+ test);

  • ** Map<string,applicant__c> – Jyoti Zanwar Mar 11 '16 at 17:17
  • 2
    Is there a particular reason why you need a separate Map? An sObject record is effectively already a map of fieldName => fieldValue. If you have the API Name of a field, you can write mySObjectRecord.get(<field's API name>); – Derek F Mar 11 '16 at 17:26
  • I have a filter logic stored in a string. I need to compare all the fields of the applicant whether they satisfy the logic. I have a filter logic say: Age>20 AND salary<600000 .Now I want to replace the applicants values of age and salary here and check if it evaluates to true. How can I do this without a map? – Jyoti Zanwar Mar 11 '16 at 17:42
  • Hi Jyoti, welcome to SFSE! Please take the time to visit the Help center and read How do I ask a good question. If you post the code you've written, showing us where you get the errors, what's broken or where you're stuck, I'm confident you'll find plenty of people willing to assist you. – crmprogdev Mar 11 '16 at 17:43
  • I don't see how the code you have posted relates to the question. You may find this answer helpful salesforce.stackexchange.com/questions/113244/…. Just make it a Map<String, Object> and don't use the String.valueOf. – Keith C Mar 11 '16 at 18:12

As mentioned by Derek F you don't need to create the map as you can get a field value by using the field name (obtained in the code below through the describe API but you could hard code a list of field names if you are only interested in a few fields):

SObjectType sobType = CustBusinessPro__Applicant__c.SObjectType;
Set<String> fields = sobType.getDescribe().fields.getMap().keySet();

for (CustBusinessPro__Applicant__c app : Trigger.new) {
    for (String name : fields) {
        Object value = app.get(name);
        if (value != null) {
            // use name and value in your logic here

Not the answer you're looking for? Browse other questions tagged or ask your own question.