0

A user is automatically created in the backend whenever a contact is created in the sf console. A custom field, DivisionCs is filled during that automated process. We want the same field to be updated in the contact too. I've created a trigger and it calls the following apex class to do the job. We've also planned to run a batch once to update all the contacts.

public class TriggerCUClass {

    public static void CUMethod(User updatedUser) 
    {
        String strDivisionCs;
        String strUserEmail;

        strDivisionCs = updatedUser.DivisionCs__c;
        strUserEmail = updatedUser.Email;

        List<Contact> con = [SELECT Id, DivisionCs__c FROM Contact WHERE email =: strUserEmail LIMIT 1];

        if(con.size() != 0) {
            con.get(0).DivisionCs__c = strDivisionCs;
            update con;
        }    
     }
 }

This code is working when I modify User object fields but the issue is I'm not able to make a user inactive as it throws an Error: MIXED_DML_OPERATION as it is an operation between the User(setup) and the Contact(non setup) objects. I’ve also created a batch class that calls the previous class which is executed and updates all the contacts successfully.

To overcome the MIXED_DML_OPERATION, I've modified the code as given below, using a method with future annotation to make an asynchronous web service. Now I can make a user inactive and make any other changes to the user and the field in the contact gets updated.

public class TriggerCUClass {

    public static void CUMethod(User updatedUser)
    {
        String strDivisionCs;
        String strUserEmail;

        strDivisionCs = updatedUser.DivisionCs__c;
        strUserEmail = updatedUser.Email;

        CUUpdate(strDivisionCs, strUserEmail);  
    }

    @future
    public static void CUUpdate(String strDivisionCs, String strUserEmail) {
        List<Contact> con = [SELECT Id, DivisionCs__c FROM Contact WHERE email =: strUserEmail LIMIT 1];

        if(con.size() != 0) {
            con.get(0).DivisionCs__c = strDivisionCs;
            update con;
        }
    }
}

But now the issue is I'm not able to run the batch class as the @future methods cannot be executed in a batch. Do I have to write a separate batch class without calling this class or what is the best way to proceed in this? Any help would be appreciated.

0

Nope, just do something like this

public void run_batch(ID i){ //This is called from batch
    //Code executed here
};

@future
public void run_future(ID i){ //This is called from trigger/class

    run_batch(i);

}
  • Hey @Eric, I'm trying with a few records and it's working fine. But I'm just curious to know, will it work when there are more than 10K records with @ future method? – Seesh Nov 25 '14 at 20:27
  • @Seesh, Nope. You will most likely need to change the signatures to use List<ID> and modify the code to bulkify. – Eric Nov 26 '14 at 1:35
  • But the Batch for 10K records is not going to use the future method, it directly calls the run_batch method and executed the code. Even then we have to modify to bulkify? – Seesh Nov 26 '14 at 4:02
  • @Seesh - Yes or you will hit governor limits. Not to mention all sorts of other potential issues with updating or querying records one at a time in a bulk transaction – Eric Nov 26 '14 at 5:14
  • Eric, I understand. I'm trying to modify the code for bulk transactions. So once I've coded, can I update in the post for you to scan it? – Seesh Nov 26 '14 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.