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
Célio XavierCélio Xavier 

Creating a case with Apex

Hello,

I need help please, how do I prevent the case from being created in the first condition?
 
public Case generateLog(AccountHistory event) {

               if(OldValue_c.equals(NewValue_c)){ //Help                      
                    Case log = new Case();                        
        			return log;

    			}else{   
                     Case log = new Case();
                         log.RecordTypeId     = recordTypePrevention;
                         log.AccountId        = event.AccountId;           
                         log.Origin           = 'Phone';           
                     return log;
		}
}

 
Best Answer chosen by Célio Xavier
Maharajan CMaharajan C
Hi Xavier,

Return Null as David metioned.

But Add one condition check before the adding the case in logs list for loop to avoid this error:

// logs.add(generateLog(event));  comment this line
Case cs  =  generateLog(event);
if(cs != null)
   logs.add(cs);



Thanks,
Maharajan.C

All Answers

David Zhu 🔥David Zhu 🔥
You can "return null;" if the condition is met.
Célio XavierCélio Xavier
Thanks David for the support, I tried to use the empty return, but he is showing a DML error (ERROR 'System.ListException: DML statement found null SObject at position 2'), I am publishing the rest of the class:
public with sharing class GenerateFraudPreventionEventLogView { 
    public void executeLog() { 
        List<Case> logs = new List<Case>();
         for(AccountHistory event: [SELECT Account.CPF__c, 
                                           Account.Name,  
                                           Field, 
                                           OldValue, 
                                           NewValue,
                                           CreatedDate
                                    FROM AccountHistory WHERE CreatedDate = Yesterday])             
         {                           
            logs.add(generateLog(event));                                    
         }    
    try {
        if(logs.isEmpty()) {
            return; 
        }
        else {
            insert logs; //ERROR 'System.ListException: DML statement found null SObject at position 2'
        }
    }
    catch (DmlException e) {
        System.debug('The following exception has ocurred: ' + e.getMessage()); 
        }
    }

 
Maharajan CMaharajan C
Hi Xavier,

Return Null as David metioned.

But Add one condition check before the adding the case in logs list for loop to avoid this error:

// logs.add(generateLog(event));  comment this line
Case cs  =  generateLog(event);
if(cs != null)
   logs.add(cs);



Thanks,
Maharajan.C
This was selected as the best answer
Célio XavierCélio Xavier
Thanks for the help David and Maharajan, it worked and I managed to finish the code!

A big hug friends.