When can we use lists?

Whenever we have a requirement of storing some values and displaying it on a visual page or hold them for further processing, we can use list. ListlistofAccoutKeys = new List(); listofAccoutKeys.add(account.accountkey); // we can add values to a list using add method

Consider the below code: Public integer calculate_remainder (integer param){ Integer Quotient = param; Integer dividend = 2; Integer remainder = Math.Mod(Quotient, dividend); } This method shows the error ‘no value was returned.’ How to solve it?

The above code has a return type specified in the method definition. Hence, this method should return an integer value. To solve the error, include ‘return remainder;’ as the last statement of the method.

On basis of previous example, please explain the below code: For (account acc :list_of_accounts){ If(acc.industry__c.isEmpty()){ Return; } }

Here return statement is used in the For loop. Return statement is responsible for stopping the whole program flow when not in For loop. Return has an additional capability to first break the for loop and then stop the program execution when it is used inside a for loop; this is happening in the above code.

Consider the below code in a class: Public void method1(integer param){ Integer Quotient = param; Integer dividend = 2; Integer remainder = Math.Mod(Quotient, dividend); If(remainder!=0){ // stop the execution } } Developer want to stop the execution of a whole program when quotient is not completely divisible. May you please help the developer?

We can use return statement here to stop the complete program flow. Just write ‘return;’.

Consider the below code in a class: For (account acc :list_of_accounts){ If(acc.industry__c.isEmpty()){ // skip the record } } Developer wants to skip the processing of the record if the industry field on account is blank. May you help him?

We can use continue statement to skip processing for a particular loop.

Break halts the execution of loop whereas continue just skips and move to next record.

For (account acc :list_of_accounts){

> 100){




Consider the below code in a trigger: For (account acc{ If(acc.accountnumber.isempty()){ // throw an error } } Developer want to throw an error for accounts where account number is blank. How can it be achieved?

We can use addError method to error out records or show them on visual force screen.

For (account acc{


acc.accountNumber.addError(‘Account number cannot be blank’);



Recent Posts

Recent Comments