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
WilmerWilmer 

Math.mod Method doesn't work with variables as arguments.

Hi, I'm trying to develop a trigger that checks a document number format (validation digit), so I need to use the Math.mod method in ApexCode but I found it seems to reject the Double variable as Argument. Here is the related code:
 
Code:
trigger AccountDigitoVerificacion_tgr on Account (before insert, before update) {
    if (Trigger.new.size() > 1 ) { // bulk process only
    }else{    
        Integer DIV = 0;
        Double DocNumber = 0;
        Boolean Calculate = false;
                
        for (Account MyAccount : System.Trigger.new) {
            if(MyAccount.RecordTypeId=='012T0000000CiygIAC' || MyAccount.RecordTypeId=='012T0000000CissIAC'){
                DocNumber = MyAccount.Document_Number__c;
                if(System.Trigger.isInsert){
                    Calculate = true;
                }else{
                    if(System.Trigger.isUpdate){
                        for (Account MyAccountOld : System.Trigger.old) {
                            if(MyAccount.Document_Number__c!=MyAccountOld.Document_Number__c){
                                Calculate=true;
                            }
                        }
                    }
                }
                if(Calculate==true){ // Calculates the validation digit
                    Integer M=0;
                    Integer S=1;
                    for(;DocNumber!=0;DocNumber=Math.floor(DocNumber/10)){
                        // Here is where the code validator shows an Error : S+Math.mod(DocNumber, 10)
                        S=Math.mod((S+Math.mod(DocNumber, 10)*(9-Math.mod(M++,6))), 11);
                    }
                    MyAccount.Validation_Digit__c = S?S-1:'k'; 
                }
            }
        } 
    }
}

 I tried inserting a value in the first argument directly and it works, but, when I replace it by a variable, it shows the following error (NO MATTER the value source):
"Method does not exist or incorrect signature: Math.mod(Double, Integer)".
 
Any suggestion to correct it or replace that function??
 
Thanks,
 
WILMER
 
 
Best Answer chosen by Admin (Salesforce Developers) 
WilmerWilmer
If anytime you find you can not get the MOD value by using Math.mod method because your parameters are double type numbers, here is customized method I built to replace it and I hope this could be useful for everybody.
 
Code:
Double dblMOD(Double num1, Double num2){
        // MOD Funtion from Double Type numbers.
        // Developed by: Wilmer Piedrahita - NEXTANT
        Double c = 0;
        Double result;
        result = num1 / num2;
        c= Math.floor(result);
        result = num1 - (num2 * c);
        return result;
}

 

All Answers

WilmerWilmer
Well, I finally found that the Method Math.mod only accepts Integer values as arguments, so now my problem is that I need to calculate the mod value from a Double type variable. Any idea about it?
WilmerWilmer
If anytime you find you can not get the MOD value by using Math.mod method because your parameters are double type numbers, here is customized method I built to replace it and I hope this could be useful for everybody.
 
Code:
Double dblMOD(Double num1, Double num2){
        // MOD Funtion from Double Type numbers.
        // Developed by: Wilmer Piedrahita - NEXTANT
        Double c = 0;
        Double result;
        result = num1 / num2;
        c= Math.floor(result);
        result = num1 - (num2 * c);
        return result;
}

 
This was selected as the best answer
rakeshrockb123rakeshrockb123

Thanks wilmer. This is exactly what i'm looking for.

WilmerWilmer

You're welcome!

 

It is so nice to see that things I've shared 5 years ago are still useful to others.