5

When I run the APEX PMD, getting Design Error for the Below method.

ExcessiveParameterList Avoid long parameter lists.

Checked below link not able to solve this issue, Can anyone please help me on this.

https://pmd.github.io/latest/pmd_rules_apex_design.html#excessiveparameterlist

@AuraEnabled   
    public static boolean updatestudentProgramDiscount(Id recId, string discountType, decimal discountPercentage, string discountDetails) {
       
        try{
            students_Programs__c spt;         
                    spt =[select Id from students_Programs__c where Id=:recId];
                   spt.Discount_Type__c = discountType;
                   spt.Discount__c = discountPercentage;
                   spt.Discount_Details__c = discountDetails;
                   Update spt;  
                   return true;                          
        }catch (Exception e) {
            return false;
        }      
    }
1
  • 1
    When I find myself in these situations, I usually create a wrapper class and pass that wrapper class as argument.
    – javanoob
    Aug 31, 2020 at 15:07

2 Answers 2

5

As the rule says, you have too many parameters. Consider passing in an entire record:

@AuraEnabled
public static updatestudentProgramDiscount(students_programs__c studentRecord) {
    try {
        update studentRecord;
    } catch(Exception e) {
        throw new AuraHandledException(e.getMessage());
    }
}

Note: You should use Security.stripInaccessible to prevent invalid field access. Note also using an exception thrown to trigger the "catch"/"error" handler on the client.

4

From the PMD link mentioned in your question, you can see that the minimum property value in the rule set ExcessiveParameterList is 4 (screenshot below for ref). This means that if any of your apex method has 4 or more input parameters, PMD will display the rule violation error.

enter image description here

IMO, since the input params in your apex method are of different data types, it is not a big deal (but still not a best practice). You can modify the ruleset minimum value in PMD configuration to 5 to get rid of this error, but that's not a best practice & not recommended.

Instead, modify the apex method as shown below (or similar). There are other ways to do it and this is just one way.

@AuraEnabled   
public static boolean updatestudentProgramDiscount(Id recId, string discountData) {   
    try{
        students_Programs__c spt = [SELECT Id FROM students_Programs__c WHERE Id = :recId];
        
        Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(discountData);
        spt.Discount_Type__c = String.valueOf(m.get('discountType'));
        spt.Discount__c = Decimal.valueOf(m.get('discountPercentage'));
        spt.Discount_Details__c = String.valueOf(m.get('discountDetails'));
        Update spt;  
        return true;                          
    }catch (Exception e) {
        return false;
    }      
}

In the Aura or LWC code, pass the value for discountData in string format as shown below:

// Replace appropriate values in the JS code
for var discountData = '{"discountType":"", "discountPercentage":"", "discountDetails":""}';

Also, try and bulkify the apex method, if possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.