+ Start a Discussion
Shakena WatsonShakena Watson 

Can I do a right pad in a Select Statement

Hello, I need to do a RPAD on the following statement, but it does not seem to work in a the Select or Where Clause. 

Can a RightPad be use in the following way?

v_year = '2024';
      
       for (Candidate_Recs__c firstAssign : recsToProcess) {
             for (Contact seqAssign : [ Select MIN(lastName.RightPad(40, ' ') +
                    firstName.RightPad(40, ' ') +
                    middleName.RightPad(40, ' ') )
                    , MIN(contact.pre_code)
                 FROM
                  Contact
               WHERE
                      lastName.RightPad(40, ' ') +
                      firstName.RightPad(40, ' ') +
                      middleName.RightPad(40, ' ') +
                     >
                      firstAssign.lastName.RightPad(40, ' ') +
                      firstAssign.firstName.RightPad(40, ' ') +
                      firstAssign.MiddleNameRightPad(40, ' ') +
                  AND Contact.status IN ('AX', 'TQ', 'RO')
                  AND Contact.class_started = :v_class_yr
                  AND Contact.pre_code IS NOT NULL
                  AND Contact.employee_nbr IS NOT NULL]) {
                  
                  //Do Something
              }
}

Thanks for any help in advance.
Best Answer chosen by Shakena Watson
Vishwajeet kumarVishwajeet kumar
Hello,
Sorry missed this part, you can do the query and then perfom operations on it after getting the data if you wanna get result as Contact object list from query. If you use Aggergate Function (https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_agg_functions.htm) like MIN the return data will be in Different type of object list.

Like : 
for (Contact seqAssign : [ Select lastName, firstName, middleName, pre_code FROM Contact
                   WHERE  Contact.status IN ('AX', 'TQ', 'RO')
                  AND Contact.class_started = :v_class_yr
                  AND Contact.pre_code IS NOT NULL
                  AND Contact.employee_nbr IS NOT NULL]) {

        //Calculate/Perform operation on data

               for (Candidate_Recs__c firstAssign : recsToProcess) {
                  if( seqAssign.lastName.RightPad(40, ' ') +
                      seqAssign.firstName.RightPad(40, ' ') +
                      seqAssign.middleName.RightPad(40, ' ') +
                     >
                      firstAssign.lastName.RightPad(40, ' ') +
                      firstAssign.firstName.RightPad(40, ' ') +
                      firstAssign.MiddleNameRightPad(40, ' ') ){
                            //Do Something
                  }
               }
   }


Thanks

All Answers

Vishwajeet kumarVishwajeet kumar
Hello,
Method : rightPad(Integer length, String padStr) is correct method for doing such operation as doc (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm). 

In Above code, SOQL query is being done in a For loop: for (Candidate_Recs__c firstAssign : recsToProcess) {  which is not recommanded by salesforce as it can easily hit governer limits of no. of SOQL query allowed in a transaction. i would recommand to do the query first and then do the comparison/operation on data.

Something like this(Reverse loop, query data then perform operations): 

 for (Contact seqAssign : [ Select MIN(lastName.RightPad(40, ' ') +
                    firstName.RightPad(40, ' ') +
                    middleName.RightPad(40, ' ') )
                    , MIN(contact.pre_code)
                 FROM
                  Contact
               WHERE  Contact.status IN ('AX', 'TQ', 'RO')
                  AND Contact.class_started = :v_class_yr
                  AND Contact.pre_code IS NOT NULL
                  AND Contact.employee_nbr IS NOT NULL]) {

               for (Candidate_Recs__c firstAssign : recsToProcess) {
                  if( seqAssign.lastName.RightPad(40, ' ') +
                      seqAssign.firstName.RightPad(40, ' ') +
                      seqAssign.middleName.RightPad(40, ' ') +
                     >
                      firstAssign.lastName.RightPad(40, ' ') +
                      firstAssign.firstName.RightPad(40, ' ') +
                      firstAssign.MiddleNameRightPad(40, ' ') ){
                            //Do Something
                  }
               }

   }


Thanks
Shakena WatsonShakena Watson
Hi Vishwajeet,

The RightPad and MIN do not seem to be working. I am getting back the following errors:

Unexpected token '.'.

Shakena
Vishwajeet kumarVishwajeet kumar
Hello,
Sorry missed this part, you can do the query and then perfom operations on it after getting the data if you wanna get result as Contact object list from query. If you use Aggergate Function (https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_agg_functions.htm) like MIN the return data will be in Different type of object list.

Like : 
for (Contact seqAssign : [ Select lastName, firstName, middleName, pre_code FROM Contact
                   WHERE  Contact.status IN ('AX', 'TQ', 'RO')
                  AND Contact.class_started = :v_class_yr
                  AND Contact.pre_code IS NOT NULL
                  AND Contact.employee_nbr IS NOT NULL]) {

        //Calculate/Perform operation on data

               for (Candidate_Recs__c firstAssign : recsToProcess) {
                  if( seqAssign.lastName.RightPad(40, ' ') +
                      seqAssign.firstName.RightPad(40, ' ') +
                      seqAssign.middleName.RightPad(40, ' ') +
                     >
                      firstAssign.lastName.RightPad(40, ' ') +
                      firstAssign.firstName.RightPad(40, ' ') +
                      firstAssign.MiddleNameRightPad(40, ' ') ){
                            //Do Something
                  }
               }
   }


Thanks
This was selected as the best answer