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
MimiAMimiA 

Using startsWith

I would like to change this code:

tmp.add((f.startsWith('Address'))?mapField.get(f):f);

To add all the value in mapField into tmp? Any help is appreciated.

Thanks
Best Answer chosen by MimiA
MimiAMimiA
Thanks Lakshman,
I have managed to what I wanted to do.
 

All Answers

LakshmanLakshman
Can you elaborate what is the defination of mapField and temp ?
MimiAMimiA
Here is the code that I want to amend. The code that is in bold.

public with sharing class customObjService {
    
    public static final Set<String> FIELD_NOT_MAPPED_TO_ACCOUNT = new Set<String>{'field1__c','field2__c','field3__c','Account__c','Id'};
    
    public static Map<String,String> addressField = getMapField();
    
            
    public static List<String> fieldName = getFieldName();
    

        
    public static List<String> getFieldName(){
        
        List<String> tmp = new List<String>();
        
        Map<String,String> mapField = getMapField();
        
        
        System.debug('>>>mapField: ' + mapField);
        
        Map<String, Schema.SObjectField> customObjMap = Schema.getGlobalDescribe().get('customObj__c'.toLowerCase()).getDescribe().Fields.getMap();
        //Get all field name from customObj__c sObject
        if (customObjMap != null){
            for (Schema.SObjectField ft : customObjMap.values()){
                String f = ft.getDescribe().getName();
                Integer len = f.length() - 3;
                
                if(f.indexOf('__c') == len && !customObjService.FIELD_NOT_MAPPED_TO_ACCOUNT.contains(f)){
                    tmp.add((f.startsWith('Address'))?mapField.get(f):f);
                    
                    system.debug('tmp >>>' + tmp);
                }
                
            }
        }
        //system.debug('tmp >>>' + tmp);
        return tmp;
    }
    
   
    
    public static Map<String,String> getMapField(){
        
        Map<String,String> tmp = new Map<String,String>();
        //From Custom Object to Account
        tmp.put('Address_Street__c','BillingStreet');
        tmp.put('Address_City__c','BillingCity');
        tmp.put('Address_Country__c','BillingCountry');
        tmp.put('Address_State_Province__c','BillingState');
        tmp.put('Address_Zip_Postal_Code__c','BillingPostalCode');
        
        
        
        //From Account to Custom Object
        tmp.put('BillingStreet','Address_Street__c');
        tmp.put('BillingCity','Address_City__c');
        tmp.put('BillingCountry','Address_Country__c');
        tmp.put('BillingState','Address_State_Province__c');
        tmp.put('BillingPostalCode','Address_Zip_Postal_Code__c');
        
        
        return tmp;
    }
}
 
LakshmanLakshman
So you want to add all values of mapField irrespective of any conditions ? or values containing a particular string ?
MimiAMimiA
Thanks Lakshman,
I have managed to what I wanted to do.
 
This was selected as the best answer