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
ckellieckellie 

Populating Field from Map

The below code is perplexing me. The concept is to look up the default account's language and return it to the Opportunity. The opportunity's language is note being updated. Below is the code:
public class DefaultOpportunityLanguage{
  // These variables store Trigger.oldMap and Trigger.newMap
   Map<Id, Opportunity> oldOpps;
   Map<Id, Opportunity> newOpps;
   Map<Id, Account> accmap = new Map<Id, Account>();
   Set<id> aid = new Set<id>();
   Set<id> oid = new Set<id>();
   String scountry;
   
  // This is the constructor
  // A map of the old and new records is expected as inputs
  public DefaultOpportunityLanguage(
    Map<Id, Opportunity> oldTriggerOpps, 
    Map<Id, Opportunity> newTriggerOpps) {
      oldOpps = oldTriggerOpps;
      newOpps = newTriggerOpps;
  }

  // The one method your master trigger will call
  public void DefaultOpportunityLanguage() {
    string u = 'Angola';
    Map<string, pw_cc__CountryObject__c> countrymap = new Map<string, pw_cc__CountryObject__c>();
    
    for(pw_cc__CountryObject__c p : [select name,Primary_Language__c from pw_cc__CountryObject__c]){
      countrymap.put(p.name, p);
    }
    system.debug('*************5countrymap size:' +countrymap.size());    
    
    for (Opportunity newOpp : oldOpps.values()) {
            system.debug('*************1accountid:' +newOpp.accountid);
            aid.add(newOpp.accountid);
            oid.add(newOpp.id);
            
  } 
      for(account a : [select id, shippingcountry from account where id in:aid]){
          accMap.put(a.id,a);
      }

    for (Opportunity newOpp : [select id, accountid, Language_to_prepare_docs_in__c from Opportunity where id =: oid]) {
        system.debug('*************3id:' +newOpp.id);

           scountry = accMap.get(newOpp.accountid).shippingCountry;
            system.debug('*************4scountry:' +scountry);
            if(countrymap.get(scountry).Primary_Language__c<>''){
               newOpp.Language_to_prepare_docs_in__c = countrymap.get(scountry).Primary_Language__c;

           scountry = '';
         }
  } 
  
 }
}
The variable scountry is populated, the countrymap is populated, and I have confirmed that the database is complete with the appropriate records. But the language is not being written back tothe opportunity.
 
Best Answer chosen by ckellie
Mahesh DMahesh D
Hi Ckellie,

Please find the modified code:
 
public class DefaultOpportunityLanguage{
  // These variables store Trigger.oldMap and Trigger.newMap
   Map<Id, Opportunity> oldOpps;
   Map<Id, Opportunity> newOpps;
   Map<Id, Account> accmap = new Map<Id, Account>();
   Set<id> aid = new Set<id>();
   Set<id> oid = new Set<id>();
   String scountry;
   
  // This is the constructor
  // A map of the old and new records is expected as inputs
  public DefaultOpportunityLanguage(
    Map<Id, Opportunity> oldTriggerOpps, 
    Map<Id, Opportunity> newTriggerOpps) {
      oldOpps = oldTriggerOpps;
      newOpps = newTriggerOpps;
  }

  // The one method your master trigger will call
  public void DefaultOpportunityLanguage() {
    string u = 'Angola';
    Map<string, pw_cc__CountryObject__c> countrymap = new Map<string, pw_cc__CountryObject__c>();
    
    for(pw_cc__CountryObject__c p : [select name,Primary_Language__c from pw_cc__CountryObject__c]){
      countrymap.put(p.name, p);
    }
    system.debug('*************5countrymap size:' +countrymap.size());    
    
    for (Opportunity newOpp : newOpps.values()) {
            system.debug('*************1accountid:' +newOpp.accountid);
            aid.add(newOpp.accountid);
            oid.add(newOpp.id);
            
  } 
      for(account a : [select id, shippingcountry from account where id in:aid]){
          accMap.put(a.id,a);
      }

    for (Opportunity newOpp : [select id, accountid, Language_to_prepare_docs_in__c from Opportunity where id =: oid]) {
        system.debug('*************3id:' +newOpp.id);

           scountry = accMap.get(newOpp.accountid).shippingCountry;
            system.debug('*************4scountry:' +scountry);
            if(countrymap.get(scountry).Primary_Language__c<>''){
               newOpp.Language_to_prepare_docs_in__c = countrymap.get(scountry).Primary_Language__c;

           scountry = '';
         }
  } 
  
 }
}

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Abhishek BansalAbhishek Bansal
Hi,

Can you please share your trigger code also. That would help us to understand the execution flow of your code and also helps us in providing a better solution for this.

Thanks,
Abhishek Bansal.
ckellieckellie
Hi Abhishek,

My general trigger calling the class is below. I have underlined the code that will call the class.
trigger MasterOpportunityTrigger on Opportunity (
  before insert, after insert, 
  before update, after update, 
  before delete, after delete) {

  if (Trigger.isBefore) {
    if (Trigger.isInsert) {
     ReviewChatteronLayout layoutpost = new ReviewChatteronLayout(Trigger.oldMap, Trigger.newMap);
                  layoutpost.ReviewChatteronLayout();
     OldOpportunityLink ool = new OldOpportunityLink(Trigger.oldMap, Trigger.newMap);
                  ool.OldOpportunityLink();
     OpportunityOwnersAddress3 ooa = new OpportunityOwnersAddress3 (Trigger.oldMap, Trigger.newMap);
                  ooa.OpportunityOwnersAddress3();
     DefaultOpportunityLanguage dol = new DefaultOpportunityLanguage(Trigger.oldMap, Trigger.newMap);
                  dol.DefaultOpportunityLanguage();
    } 
    if (Trigger.isUpdate) {
     ReviewChatteronLayout layoutpost = new ReviewChatteronLayout(Trigger.oldMap, Trigger.newMap);
                  layoutpost.ReviewChatteronLayout();
     ForecastOverride Forecast = new ForecastOverride(Trigger.oldMap, Trigger.newMap);
                  Forecast.ForecastOverride();
     OpportunityOwnersAddress3 ooa = new OpportunityOwnersAddress3 (Trigger.oldMap, Trigger.newMap);
                  ooa.OpportunityOwnersAddress3();
     SendUndefinedAppTask uat = new SendUndefinedAppTask (Trigger.oldMap, Trigger.newMap);
                  uat.SendUndefinedAppTask();
     DefaultOpportunityLanguage dol = new DefaultOpportunityLanguage(Trigger.oldMap, Trigger.newMap);
                  dol.DefaultOpportunityLanguage();
    }

    if (Trigger.isDelete) {
      // Call class logic here!
    }
  }

  if (Trigger.IsAfter) {
    if (Trigger.isInsert) {
     NewContactRole contactrole = new NewContactRole(Trigger.oldMap, Trigger.newMap);
                  contactrole.NewContactRole();
    EMEIAProductSpecialist emeiaps = new EMEIAProductSpecialist(Trigger.oldMap, Trigger.newMap);
                  emeiaps.EMEIAProductSpecialist();
    } 
    if (Trigger.isUpdate) {
    EMEIAProductSpecialist emeiaps = new EMEIAProductSpecialist(Trigger.oldMap, Trigger.newMap);
                  emeiaps.EMEIAProductSpecialist();
    }
    if (Trigger.isDelete) {
      // Call class logic here!
    }
  }
}
Thank you
 
Mahesh DMahesh D
Hi Ckellie,

Please find the modified code:
 
public class DefaultOpportunityLanguage{
  // These variables store Trigger.oldMap and Trigger.newMap
   Map<Id, Opportunity> oldOpps;
   Map<Id, Opportunity> newOpps;
   Map<Id, Account> accmap = new Map<Id, Account>();
   Set<id> aid = new Set<id>();
   Set<id> oid = new Set<id>();
   String scountry;
   
  // This is the constructor
  // A map of the old and new records is expected as inputs
  public DefaultOpportunityLanguage(
    Map<Id, Opportunity> oldTriggerOpps, 
    Map<Id, Opportunity> newTriggerOpps) {
      oldOpps = oldTriggerOpps;
      newOpps = newTriggerOpps;
  }

  // The one method your master trigger will call
  public void DefaultOpportunityLanguage() {
    string u = 'Angola';
    Map<string, pw_cc__CountryObject__c> countrymap = new Map<string, pw_cc__CountryObject__c>();
    
    for(pw_cc__CountryObject__c p : [select name,Primary_Language__c from pw_cc__CountryObject__c]){
      countrymap.put(p.name, p);
    }
    system.debug('*************5countrymap size:' +countrymap.size());    
    
    for (Opportunity newOpp : newOpps.values()) {
            system.debug('*************1accountid:' +newOpp.accountid);
            aid.add(newOpp.accountid);
            oid.add(newOpp.id);
            
  } 
      for(account a : [select id, shippingcountry from account where id in:aid]){
          accMap.put(a.id,a);
      }

    for (Opportunity newOpp : [select id, accountid, Language_to_prepare_docs_in__c from Opportunity where id =: oid]) {
        system.debug('*************3id:' +newOpp.id);

           scountry = accMap.get(newOpp.accountid).shippingCountry;
            system.debug('*************4scountry:' +scountry);
            if(countrymap.get(scountry).Primary_Language__c<>''){
               newOpp.Language_to_prepare_docs_in__c = countrymap.get(scountry).Primary_Language__c;

           scountry = '';
         }
  } 
  
 }
}

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
ckellieckellie
Mahesh,

Thank you for your help, your suggestion and one tweek solved the problem.

Thank you,
ckellie
 
Mahesh DMahesh D
Hi Ckellie,

Glad that your issue got resolved with the above code.

Please mark the best answer properly so that it will be helpful to others in the future. I think by mistake you selected thank you msg as a best answer.

Regards,
Mahesh