+ Start a Discussion
andyaldis1.3969086903835708E12andyaldis1.3969086903835708E12 

Batch Create TaskRelation

I am writing a batch apex class to insert Task Relations,  The class is basically going to query a custom object that we used to add multiple contacts to a class.  The custom object is not actually related to the task itself but does have a text field that was updated with the Record Id from the original task.  Now that SF allows users to add multiple contacts to a task we want to correct our data and add the contacts to the task.  My question is how will that work with the 50 contact limit for the multiple contacts.  There may or may not be instances where there are more than 50 contacts related to a task in our custom object.  Our plan is to only create the taskRelation records for the first 50 entered and we are not going to worry about the rest.  If the class runs into the 50 record limit while creating the taskRelation records what will happen will the entire batch fail and roll back, or will 50 taskRelation records be created and the records that went over fail?

my class is below.


    global DataBase.QueryLocator start(Database.BatchableContext BC) {
        string contactQuery = 'Select Activity_Id__c, Contact__c From Custom_Activity_Contact__c';
        return Database.getQueryLocator(contactQuery);
    }

       global void execute(Database.BatchableContext BC, List<task> taskList) {
     List<TaskRelation> taskRelationList = new List<TaskRelation>();
            TaskRelation tr = new TaskRelation();
            Tr.TaskId = Custom_Activity_Contact__c.Activity_Id__c;
            Tr.RelationId = Custom_Activity_Contact__c.Contact__c;
            Tr.IsWhat = False;
            taskRelationList.add(tr);

            }
                insert taskRelationList;
            }
    
    }
    
    global void finish(Database.BatchableContext BC) {
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
            JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id = :bc.getJobId()];
        EmailUtils.sendMessage(a, recordsProcessed);

    }