Skip to content

Instantly share code, notes, and snippets.

@sudikrt
Created August 24, 2017 06:11
Show Gist options
  • Save sudikrt/5434a3d825325e3e1d331e8fecfaed6b to your computer and use it in GitHub Desktop.
Save sudikrt/5434a3d825325e3e1d331e8fecfaed6b to your computer and use it in GitHub Desktop.
Create one Object Reporting country . Create 3 fields country_code,country,capital. In object Account create field country_code. Now Create Lookup to Reporting country in Opportunity. Lets take example one record of Object Reporting country country_code country capital 91 India Delhi if In Account Object country_code = 91 then all opportunities …
public class Testhandler {
public static void handleTrigger (Map<Id, Account> newVal) {
List<Opportunity> listToUpdate = new List<Opportunity> ();
Map<Decimal, Reporting_country__c> ctrMap = getCountryData (newVal);
for (Account ac : getRelatedOpportunity (newVal).values()) {
for (Opportunity opp : ac.Opportunities) {
if (ctrMap.containsKey(ac.country_code__c)) {
opp.reporting_country__c = ctrMap.get (ac.country_code__c).Id;
listToUpdate.add (opp);
}
}
}
if (listToUpdate.size() > 0) {
update listToUpdate;
}
}
private static Map<Id, Account> getRelatedOpportunity (Map<Id, Account> newVal) {
return new Map<Id, Account> ([SELECT Id, country_code__c,(SELECt id, reporting_country__c from Opportunities) from Account where id in : newVal.keySet()]);
}
private static Map<Decimal, Reporting_country__c> getCountryData (Map<Id, Account> newVal) {
Map<Decimal, Reporting_country__c> ctrMap = new Map <Decimal, Reporting_country__c> ();
Set<Decimal> cCode = new set<Decimal> ();
for (Account a : newVal.values()) {
cCode.add (a.country_code__c);
}
for (Reporting_country__c ctr : [Select country_code__c from Reporting_country__c where Country_Code__c in : cCode] ) {
ctrMap.put(ctr.country_code__c, ctr);
}
return ctrMap;
}
}
public class Testhandler {
public static void handleTrigger (Map<Id, Account> newVal) {
Set<Decimal> cCode = new set<Decimal> ();
Map<Decimal, Reporting_country__c> ctrMap = new Map <Decimal, Reporting_country__c> ();
List<Opportunity> listToUpdate = new List<Opportunity> ();
for (Account a : newVal.values()) {
cCode.add (a.country_code__c);
}
System.debug('Country Code : ' + cCode );
for (Reporting_country__c ctr : [Select country_code__c from Reporting_country__c where Country_Code__c in : cCode] ) {
ctrMap.put(ctr.country_code__c, ctr);
}
Map<Id, Account> relatedOpportunity = new Map<Id, Account> ([SELECT Id, country_code__c,(SELECt id, reporting_country__c from Opportunities) from Account where id in : newVal.keySet()]);
System.debug('CtrMap : '+ ctrMap );
for (Account ac : relatedOpportunity.values()) {
for (Opportunity opp : ac.Opportunities) {
System.debug('ac.Opportunities : '+opp);
System.debug('Code :' + ctrMap.get(ac.country_code__c));
opp.reporting_country__c = ctrMap.get (ac.country_code__c).Id;
listToUpdate.add (opp);
}
}
System.debug(listToUpdate);
update listToUpdate;
}
}
trigger AssignmentSecondTrigger on Account (before update, after update) {
if (Trigger.isUpdate) {
if (Trigger.isBefore) {
System.debug('*****Before Update');
}
if (Trigger.isAfter) {
System.debug('*****After Update');
Testhandler.handleTrigger (Trigger.newMap);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment