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
Jim Minichiello 2Jim Minichiello 2 

Future Method Error

We have an apex integration to an external system. A trigger calls two Classes.
  • The first Class checks to see if the corresponding record exists in the external system. If it does not, the Class sends a create command to the external system and the record should be created.
  • The second Class comes into play if the corresponding record already exists in the external system. If it does, the Class sends an update command to the external system
I am getting this error whenever the trigger is fired:
Update failed. First exception on row 0 with id 0010v000007pcrVAAQ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountTrigger: execution of BeforeUpdate

caused by: System.AsyncException: Future method cannot be called from a future or batch metho

Can someone help my developer and I solve this issue? I can provide the code for the trigger and the two classes.
Jim Minichiello 2Jim Minichiello 2
Here is the code for the Trigger:

trigger AccountTrigger on Account (before insert, before update) {

  if(Trigger.isBefore){
    if(Trigger.isUpdate){
      //Add first, which will check if it should add
      ArchticsAccountAdd.archticsAddAccount(Trigger.new, Trigger.oldMap);

      //Update second, will see if it should be updated
      ArchticsAccountUpdate.archticsUpdateAccount(Trigger.new, Trigger.oldMap);
    }
  }
}
Jim Minichiello 2Jim Minichiello 2
//MAIN METHOD
  //--------------------------------------------------
  public static void archticsAddAccount(List<Account> listAccts, Map<Id, Account> oldMap){
    //Get list of all Accounts that need to be added to the Archtics system
    Set<Id> archticsAddAcctIds = validateCreateAccount(listAccts, oldMap);
        Set<Id> archticsOtherAcctIds = validateCreateOther(listAccts);
        Boolean validateUser = ArchticsUtility.validateRunningUser();
        System.debug('Create Archtics Size: ' + archticsAddAcctIds.size());
        System.debug('Other Create Size: ' + archticsOtherAcctIds.size());
        System.debug('valid User: ' + validateUser);

    //call the future method for creating Primary information
    if(archticsAddAcctIds.size() > 0 && validateUser){
      createPrimary(archticsAddAcctIds);
    }
        else if(archticsOtherAcctIds.size() > 0 && archticsAddAcctIds.size() <=0 && validateUser){
            createOther(archticsOtherAcctIds);
        }
  }

    public static void createPrimary(Set<Id> acctIds){
        List<Account> acctList = [SELECT Id, Name, Create_In_Archtics__c, Archtics_Account_Type__c, 
                                    Archtics_Account_ID__c, Archtics_Integration_Info__c 
                                    FROM Account 
                                    WHERE Id in :acctIds];
        for(Account a : acctList){
            FutureCreateAccount(a.Id);
        }
    }

    public static void createOther(Set<Id> acctIds){
        List<Contact> otherContactList = ArchticsUtility.getListOtherContactRecords(acctIds);
        List<Account> acctList = [SELECT Id, Name, Create_In_Archtics__c, Archtics_Account_Type__c, 
                                    Archtics_Account_ID__c, Archtics_Integration_Info__c 
                                    FROM Account 
                                    WHERE Id in :acctIds];
        Map<Id, List<Contact>> acctContactOtherMap = ArchticsUtility.otherAcctContactMap(otherContactList);

        for(Account acct : acctList){
            System.debug('Account information: ' + acct.Name);
            for(Contact con : acctContactOtherMap.get(acct.Id)){
                System.debug('Contact information: ' + con);
                if(con.Archtics_CustName_ID__c == null){
                    FutureCreateOtherCallOut(acct.Id, con.Id);
                }
            }
        }

    }

  //FUTURE METHOD
  //This method is used to loop through
  //--------------------------------------------------
  @future (callout = true)
  private static void FutureCreateAccount(Id acctId){
    Account a = [SELECT Id, Name, Create_In_Archtics__c, Archtics_Account_Type__c, 
                Archtics_Account_ID__c, Archtics_Integration_Info__c 
            FROM Account 
            WHERE Id =: acctId];
    createArchtics(a);
  }

    @future (callout = true)
    private static void FutureCreateOtherCallOut(Id acctId, Id contactId){
        Account a = [SELECT Id, Name, Create_In_Archtics__c, Archtics_Account_Type__c, 
                                Archtics_Account_ID__c, Archtics_Integration_Info__c 
                        FROM Account 
                        WHERE Id =: acctId];

        Contact c = [SELECT Id, FirstName, LastName, MiddleName, AccountId, Archtics_CustName_ID__c, Birth_Day__c, Birth_Month__c,
                                Birth_Year__c, Salutation, Suffix, Title, BirthDate, Archtics_Account_ID__c, Archtics_Name_Type__c, Nickname__c
                        FROM Contact 
                        WHERE Archtics_Relationship_Type__c = 'Other' 
                            AND Id =: contactId];

        if(c.LastName != null && a.Name != null){
            createOtherName(a,c);
        }   
    }

 
Jim Minichiello 2Jim Minichiello 2
The above comment is the First Class that the trigger calls.