1

Trigger is updated to Before UPdateI am using Trigger isBefore In System.debug(opp.get(metaData.get(0).Opportunity_Field_Name__c) it is showing correct Values but not Updating in Opportunity Object

Below is Trigger and its Apex Class Trigger

trigger MetadataObjectFieldMapping on Opportunity (before insert, before update) 
{
    if(Trigger.isInsert || Trigger.isUpdate )
    {
        MetadataObjectFieldMappingHandler oppHandler = new MetadataObjectFieldMappingHandler();
        oppHandler.Show(Trigger.new);
    }
}

and Apex Class

public class MetadataObjectFieldMappingHandler {
    List<String> strAccField = new List<String>();
    //Getting List of MetaData Values
    List<Object_Field_Mapping__mdt> metaData = new List<Object_Field_Mapping__mdt>
                                                        ([SELECT Account_Field_Name__c,
                                                             Opportunity_Field_Name__c 
                                                                 FROM Object_Field_Mapping__mdt]);
    //Function to check if Field Name Exists in Object or not
    public Boolean hello(String objName, String fieldName)
    {
        Boolean temp = False;
        //Creating Schema to get all fields from Account and Opportunity Object
        Map<String, Schema.SObjectField> accFields = Schema.getGlobalDescribe().get(objName).getDescribe().fields.getMap();     
        for(Schema.SObjectField field : accFields.values())
        {
                    strAccField.add(field+'');
        }
        //Calling Account and Opportunity Object in fieldName
        if(strAccField.contains(fieldName)){
            System.debug('PASS '+fieldName);
            temp = true;
        }
        return temp;
    }
    public void Show(List<opportunity> newOppList)
    {       
        Boolean test1 = hello('Account',metaData.get(0).Account_Field_Name__c);
        Boolean test2 = hello('Opportunity',metaData.get(0).Opportunity_Field_Name__c);
        //If both Field Value exists
        if(test1 && test2){
            //Getting value from Opp using dynamic Query
            String query = 'Select Account.'+metaData.get(0).Account_Field_Name__c+', '+metaData.get(0).Opportunity_Field_Name__c+' from Opportunity where Id IN : newOppList ';
            List<Opportunity> oppList =database.query(query);
            for(Opportunity opp : oppList){
                opp.put(
                    metaData.get(0).Opportunity_Field_Name__c, 
                    opp.Account.get(metaData.get(0).Account_Field_Name__c)
                );
                System.debug(opp.get(metaData.get(0).Opportunity_Field_Name__c));
            }
        }
}

@David - Can you please tell me, why Value is not Updating..?

6
opp.get(metaData.get(0).Opportunity_Field_Name__c) = opp.Account.get(metaData.get(0).Account_Field_Name__c);      

The result of opp.get() is not an lvalue; it does not represent a location to which you can assign a value on the left-hand side of an assignment statement. Put another way, it returns a value, but not a reference to the value's location.

The inverse of get() is put(), and I think that's what you meant to do here:

opp.put(
    metaData.get(0).Opportunity_Field_Name__c, 
    opp.Account.get(metaData.get(0).Account_Field_Name__c)
);

Rolling up from comments:

  • You also appear to have suffered from trigger recursion while working in after update context, possibly stemming from a misdesign of where the trigger runs. Each time an Opportunity is updated, you update it again, firing another trigger invocation.

    If this trigger really belongs on Opportunity (as opposed to on Account), you should consider the two possibilities for avoiding trigger recursion that I describe in another recent answer:

    • Checking before doing work.
    • Using a static Boolean only to stop the recursive call.

    Here, it looks like your trigger really just needs to check whether it has any updates to make before firing the update DML.

  • You've also run afoul of the fact that magic before update behavior is only available if you modify the sObject instances in Trigger.new. You're re-querying them here, which results in your changes being lost. To persist those changes in before update context, you have to use Maps to correlate your queried objects back to Trigger.new via Id, and make your updates exclusively through Trigger.new.
-3
if(test1 && test2){
            //Getting value from Opp using dynamic Query
            for(Opportunity opp : newOppList){
                opp.put(
                    metaData.get(0).Opportunity_Field_Name__c, 
                    opp.Account.get(metaData.get(0).Account_Field_Name__c)
                );
                System.debug(opp.get(metaData.get(0).Opportunity_Field_Name__c));
            }

    }
  • 1
    Welcome to SFSE! Please take the tour and read How to Answer. Right now, this is what we'd call a code dump. Code dumps tend not to be great answers because they don't go over why the proposed code should work, how it works, or how it addresses the issue. Code dumps don't usually help the person asking the question understand the issue (so they can avoid similar problems in the future), and tend to generate a lot of "that didn't work" and "try this instead" in the comments, which makes it hard for other people with similar issues to apply to their own situations. – Derek F Jul 22 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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