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
sevgisevgi 

Is there a constraint on using DML in non-void methods?

My trigger requires a bit complex processing so I wanted to break down logic by creating methods in a Utility class

One of the methods create a detail record, so I declared ıt as type boolean to return status:

 

public with sharing class Utility_MaintainTransferRequests {
    
    public static boolean CreateNewDealer (Case inTRRequest) {

......  control logic

......  create record


        try {
            insert nwDealer;
        }
                    
        catch(Exception e){
            inTRRequest.adderror('Y...........!!!!');      
            return false;                           
        }


But get an error as :

Save error: Non-void method might not return a value or might have statement after a return statement.    Utility_MaintainTransferRequests.cls

 

 

Does it mean I cant use DML in non-void methods or is there sthg wrong with code?

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

You need to set a return value outside the try catch block or set a return value within the try as well as the catch sections. As it currently stands, you only return a value IF the DML fails, if it passes you do not return anything.

All Answers

Starz26Starz26

You need to set a return value outside the try catch block or set a return value within the try as well as the catch sections. As it currently stands, you only return a value IF the DML fails, if it passes you do not return anything.

This was selected as the best answer
sevgisevgi

You are right, thank u...

 

Wont post questions after 12 hrs of work again :)