# Luhn Algorithm: How to get check digit of MOD 10v1 in Batch Apex Class?

I would like to ask some help on how I can get the Mod 10 check digit of Contact.DonorID. the result should be

``````Contact.CRN__c = Contact.DonorID + CheckDigit
``````

Just like this:

Check Digit is 2

CRN: 00184572

It is my first time to do it and I don't have any idea, I've been struggling for 3 days searching about it. I found this link -- Luhn algorithm check digit in formula field but it is using formula field, so, it can't help me.

• Have you made an attempt at it? The Wikipedia article linked to by the formula field question does give a pseudocode implementation. If you have tried to code this, please edit your question to show us what you've tried and point to a specific sport that you're stuck on. Mar 10, 2021 at 2:55
• Sorry, I couldn't format the code well in reply. Mar 10, 2021 at 5:42
• @DerekF, I tried this code. But wasn't sure. Is it correct? Is it Mod 10? `String donorId = '0115839'; Integer sum = 0; Integer len = donorId.length(); for(Integer i=len-1;i>=0;i--){ Integer num = Integer.ValueOf(donorId.substring(i,i+1)); if ( math.mod(i , 2) == math.mod(len, 2) ) { Integer n = num * 2; sum += (n / 10) + ( math.mod(n, 10)); System.debug('Output1: ' + sum); String result = donorId + sum; System.debug('CRN: ' + result); } else{ sum += num; System.debug('Output2: ' + sum); } }` Mar 10, 2021 at 6:05

This is pretty simple, you just need to implement the algorithm.

``````static Integer accumulate(String[] digits, Boolean validate) {
// This algorithm goes from the *left*, so we need to know which to double
Integer checkSize = validate? 1: 0;
Integer accumulator = 0;
while(!digits.isEmpty()) {
Integer nextDigit = Integer.valueOf(digits.remove(0));
// We should double the value when true
if((digits.size()&1) == checkSize) {
nextDigit <<= 1;
// And "add together" the individual digits when 10+
if(nextDigit > 9) {
nextDigit -= 9;
}
}
accumulator += nextDigit;
}
return accumulator;
}
// Returns true if the valid Luhn check digit present
public static Boolean validateLuhnCheckDigit(String source) {
return 0 == Math.mod(accumulate(source.split(''), true), 10);
}
// Returns the check digit for a given string
public static String calculateLuhnCheckDigit(String source) {
return ''+Math.mod(accumulate(source.split(''), false) * 9, 10);
}
``````

No error checking is provided, and is left as an exercise to the reader.

• I'm sorry but I can't understand the code.. and I don't know how to convert these to Apex Batch code. Mar 10, 2021 at 7:38
• @juandelacruz `Contact.CRN__c = Contact.DonorId + calculateLuhnCheckDigit(Contact.DonorId);` Mar 10, 2021 at 9:31
• I got it now. I'll post the code here for the correct answer. Mar 11, 2021 at 0:44
• @juandelacruz My code is perfectly correct. If you're not sure how to call a function, you might want to do some reading. Mar 11, 2021 at 2:04
• Yes, I believe you sir.. I was just looking for easier solution. Thanks sir. Mar 12, 2021 at 4:07

I got it and it's working! I'll share my apex code. You can test it on Anonymous Window.

``````                String donorId = Contact.DonorID__c;
Integer sum = 0;
String resultCRN;
Integer l = donorId.length();

for(Integer i=l-1;i>=0;i--){
Integer num = Integer.ValueOf(donorId.substring(i,i+1));
if (math.mod(i, 2) == math.mod(l, 2))
{
Integer n = num * 2;
sum += (n / 10) + (math.mod(n, 10));
}
else{
sum += num;
}
}
resultCRN= donorId + sum;
Contact.CRN__c = resultCRN;
System.debug('Donor ID is: ' + donorId );
System.debug('Check digit result is: ' + sum);
System.debug('CRN is: ' + resultCRN);
``````