How to populate maximum time string repeat in record /Child Record - Similar Rollup summary Max function.
Hi Rowdies,
This is a Sample code for displaying maximum time repeat string in child records like Invoice and Invoice line.
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9744265961313227"
crossorigin="anonymous"></script>
for (custom__c obc : [SELECT Id , customfield__c, customfield2__c FROM custom__c ]){
if(String.isNotBlank(oinvline.customfield__c)){
lstTaxCode.add(oinvline.customfield__c);
}
}
for (string settemp : lstTaxCode){
strSetTaxCode.add(settemp);
}
for(String str: strSetTaxCode){
Integer countofChar = 0;
for(String strl: lstTaxCode ){
if(str == strl){
countofChar++;
}
}
myMap.put(str, countofChar);
}
for(String strcode: strSetCountryCode){
Integer countofCharccode = 0;
for(String strl: lstTaxCountryCode ){
if(strcode == strl){
countofCharccode++;
}
}
myMapcode.put(strcode, countofCharccode);
}
for(String strPrimary: setPrimaryTaxRate){
Integer countofChar = 0;
for(String strlPr: lstPrimaryTaxRate ){
if(strPrimary == strlPr){
countofChar++;
}
}
myMapPrimaryTAX.put(strPrimary, countofChar);
}
for(String strSecondaryTax: setSecondaryTaxRate){
Integer countofChar = 0;
for(String strl: lstSecondaryTaxRate ){
if(strSecondaryTax == strl){
countofChar++;
}
}
myMapSecondaryTAX.put(strSecondaryTax, countofChar);
}
List<Integer> i_valuesPrimaryTax = myMapPrimaryTAX.values();
i_valuesPrimaryTax.sort();
//get the max value of the count
string sSAPPrimaryTaxes ;
if (i_valuesPrimaryTax.size() > 0 ){
Integer i_max_value = i_valuesPrimaryTax[(i_valuesPrimaryTax.size()-1)];
for(String sSAPPrimaryTax : myMapPrimaryTAX.keySet()){
Integer isapSecondaryTax = myMapPrimaryTAX.get(sSAPPrimaryTax);
if(isapSecondaryTax == i_max_value){
System.debug(' The Maximum value is' + sSAPPrimaryTax + 'and repeated for Primary Tax ' + isapSecondaryTax + 'times' );
sSAPPrimaryTaxes=sSAPPrimaryTax;
}
}
}
List<Integer> i_valuesSecondary = myMapSecondaryTAX.values();
i_valuesSecondary.sort();
//get the max value of the count
string sSapSecondaryTaxes ;
if (i_valuesSecondary.size() > 0 ){
Integer i_max_value = i_valuesSecondary[(i_valuesSecondary.size()-1)];
for(String sSapSecondary : myMapSecondaryTAX.keySet()){
Integer isapSecondaryTax = myMapSecondaryTAX.get(sSapSecondary);
if(isapSecondaryTax == i_max_value){
System.debug(' The Maximum value is' + sSapSecondary + 'and repeated for Secondary Tax ' + isapSecondaryTax + 'times' );
sSapSecondaryTaxes=sSapSecondary;
}
}
}
List<Integer> i_values = myMapcode.values();
i_values.sort();
//get the max value of the count
string staxcountrycode ;
if (i_values.size() > 0 ){
Integer i_max_value = i_values[(i_values.size()-1)];
for(String scountrycode : myMapcode.keySet()){
Integer sapTaxCountryCode = myMapcode.get(scountrycode);
if(sapTaxCountryCode == i_max_value){
System.debug(' The Maximum value is' + scountrycode + 'and repeated ' + sapTaxCountryCode + 'times' );
staxcountrycode=scountrycode;
}
}
}
List<Integer> i_valuestax = myMap.values();
i_valuestax.sort();
string staxcode ;
//get the max value of the count
if (i_valuestax.size() >0 ){
Integer i_max_valuecode = i_valuestax[(i_valuestax.size()-1)];
for(String stax : myMap.keySet()){
Integer saptaxcode = myMap.get(stax);
if(saptaxcode == i_max_valuecode){
System.debug(' The Maximum value is' + stax + 'and repeated ' + saptaxcode + 'times' );
staxcode= stax;
}
}
}
Map<String , Tax_Mapping_Configuration__c> tableofTaxMappingRecords = New Map<String , Tax_Mapping_Configuration__c>();
for(Tax_Mapping_Configuration__c taxData : [Select id,Tax_Code__c,Tax_Country__c,Primary_Tax_Rate__c,Local_Currency_ISO_Code__c,Secondary_Tax_Rate__c,Tax_Text__c , Tax_Registration_No__c from Tax_Mapping_Configuration__c])
{
String str = taxData.Tax_Code__c+taxData.Tax_Country__c+taxData.Primary_Tax_Rate__c;
System.debug('TaxMappingStr' +str);
tableofTaxMappingRecords.put(str , taxData);
}
for (blng__Invoice__c oInvoice :[Select id, Active__c , CurrencyIsoCode,ShowReverseCharge__c,VAT_Country_Conga__c, SAP_Tax_Country__c , SAP_Tax_Code__c, SAP_Primary_Tax_Rate__c,SAP_Secondary_Tax_Rate__c,Event_Series_Alternate_Currency__c,InformaTRN__c from blng__Invoice__c where Id IN : ids]){
if(!String.isBlank(staxcountrycode) || !String.isBlank(staxcode) ){
oInvoice.SAP_Tax_Country__c = staxcountrycode;
oInvoice.SAP_Tax_Code__c = staxcode;
}
oInvoice.SAP_Primary_Tax_Rate__c = sSAPPrimaryTaxes;
oInvoice.SAP_Secondary_Tax_Rate__c = sSapSecondaryTaxes;
String strtax = '';
if (oInvoice.SAP_Primary_Tax_Rate__c != '' && oInvoice.SAP_Primary_Tax_Rate__c != null){
strtax = oInvoice.SAP_Primary_Tax_Rate__c.remove('%');
}
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9744265961313227"
crossorigin="anonymous"></script>
Comments
Post a Comment