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
praveen murugesanpraveen murugesan 

Update setup object and insert non setup object in @future method.

Hi all,

In my case initally i need to update setup and not setup object at a time. So I have used @future method to save setup object seperately. But new requirement is to save update setup object and if any error occurs i need to insert non setup object in @future method

Scenario:

If any error occurs while updating user record i need to save that error in custom object.

I tried with 

1. Created one more separate @future method to insert non-setup obj. but we can't call @future method from @future method.

2. Tried with Database.Update, try-catch-finally methods.

But its doesn't work.

Can any one help me pls.

Thanks.
Arunkumar RArunkumar R

Please take a look on the below sample code, i have tested and worked fine.

public class InsertErrorInfo
{
    public static void insertUser()
    {
        try
        {
        
            User u = new User();
            u.Id = '005iAAA001BBBc';
            u.Username = 'test';
            update u;
         }
         
         catch(DMLException e)
         {
             InsertErrorFuture.insertError(e.getMessage());
         }
         
     }
}

Future handler class is,

global class InsertErrorFuture
{  
    @future  
    public static void insertError(String s)
    {
        Account a = new Account();
        a.Name = 'Error';
        a.Description = s;
        insert a;
    
    }
    
}

If still non setup object record is not inserted means, please check some of the below points,

1. Check your storage Usage from the setup menu and verify your non setup object has sufficient data storage.

2. Create a debug log and monitor the issue.

3. Go to apex job from the setup menu you will get the status, if record not inserted then it will show as fail.

4. while inserting non setup object insert all required field values.

praveen murugesanpraveen murugesan
Hi,

public class InsertErrorInfo
{
@future
    public static void insertUser()
    {
        try
        {
        
            User u = new User();
            u.Id = '005iAAA001BBBc';
            u.Username = 'test';
            update u;
         }
         
         catch(DMLException e)
         {
             InsertErrorFuture.insertError(e.getMessage());
         }
         
     }
}

Here insertuser is @future method. and in salesforce we cant call @future from @future.

Thanks.

Praveen Murugesan.