0

I need help of mapping standard field in Salesforce. I manage to map the Billing address field. Unfortunately, I couldn't map such as Phone field. Could anyone help me please. The code that I am using are as follows: on Account and Custom Object.

Thanks for your help

/*
Account_TriggerHandler
*/

public static void createNewCustomObjectReplicateFromAccount(List<Account> newA) {

        List<Account> lstRequiredRecordTypefromAccount = new List<Account>();
        Id Required_REC_ID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('TypeName').getRecordTypeId();

        List<String> fieldName = customObjService.getFieldName();
        Map<String,String> addressField = customObjService.getMapField();

        for ( Account a : newA ) {
            if ( a.RecordTypeId == Required_REC_ID )
                lstRequiredRecordTypefromAccount.add(a);
        }

        List<customObj__c> customObj2Insert = new List<customObj__c>();

        for ( Account a : lstRequiredRecordTypefromAccount ) {
            customObj__c ss = new customObj__c(Account__c=a.Id);
            //Capture current date
            ss.custom_Date__c = Date.today();
            //system.debug('>>>Name: ' + ss.Name);

            for ( String f : fieldName ) {
                String ssField = (f.startsWith('Billing'))?addressField.get(f):f;
                system.debug('ssfield>>>>>>' + ssField);

                //Only update the Account field when there is a value on customObj__c
                if ( a.get(f) != null ) {
                    ss.put(ssField,a.get(f));
                    system.debug('>>>ss: ' + ssField + ', f: ' + f + ' -> ' + a.get(f));
                }
            }
            system.debug('>>>Name: ' + ss);
            customObj2Insert.add(ss);
        }

        insert customObj2Insert;
    }
}



/*
customObjService
*/

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;
    }
}

Thanks

6
  • What happens when you add the phone field etc. to your map? What is the error that you are seeing?
    – Doug B
    Commented Feb 1, 2016 at 16:39
  • I create a field name Phone__c on my custom Object. The error is System.SObjectException: Invalid field Phone__c for Account: Class.Account_TriggerHandler.createNewCustomObjectReplicateFromAccount: line 135, column 1
    – Mimi
    Commented Feb 1, 2016 at 16:47
  • Phone__c is the name of the field in your custom object but it is not the name of the field in Account (it would be Phone there). So I'd expect mapping entries like tmp.put('Phone','Phone__c'); for the Account to Custom Object.
    – Doug B
    Commented Feb 1, 2016 at 16:52
  • Yes, it is Standard field in account API name is only phone. But for my custom Object the API name is phone__c so it is not mapping it because of the difference. I try to put is under tmp.put('Phone','Phone__c'); it is not picking it up.
    – Mimi
    Commented Feb 1, 2016 at 16:56
  • 1
    this line tmp.add((f.startsWith('Address'))?mapField.get(f):f); is your problem. If you add phone__c to the static map, then you are not going to pick it up from the mapping table
    – cropredy
    Commented Feb 2, 2016 at 2:08

1 Answer 1

-1

I manage to solve the problem by using Ternary operator.

Thanks for your help

1
  • Welcome to SFSE Mimi. Can you edit your answer to include the relevant fix?
    – Matt Lacey
    Commented Feb 5, 2016 at 3:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .