You need to sign in to do that
Don't have an account?

Apex Trigger to Insert Task
Hi Friends,
Here I am trying to insert tasks from account using apex trigger. This is working for individual edits but if I am trying to update the accounts using data laoder it is inserting tasks for those 5 accounts instead it is inserting task for one account. Here is my trigger please let me know the how to make this trigger to work for bulk updates using data loader.
trigger TaskInsertTriggerOnAccount on Account (after update) {
List <RecordType> rtList = [SELECT Id from RecordType where SobjectType='Account' and Name = 'BB' Limit 1];
List <task> taskToInsert = new List <task> ();
Set<Id> accountIds = new Set<Id>();
for(Account acc:Trigger.New){
if(acc.Agent_Id__c!=NULL&&acc.Contracting_Completed_Date__c==date.Today()&&acc.Contracting_Complete__c==True&&acc.RecordTypeId==rtList[0].Id&&acc.status__c=='Active'){
accountIds.add(acc.Id);
}
}
Map<Id,Bulk_Product__c> bulkProdMap = new Map<Id,Bulk_Product__c>();
List<Bulk_Product__c> bulkProdList = [Select Id,Bulk_Code__c,Bulk_acc__c from Bulk_Product__c Where Bulk_acc__c IN :accountIds and Bulk_Code__c = 'CCM' Limit 1];
for(Bulk_Product__c bk:bulkProdList ){
if(ag.Bulk_Code__c!=NULL){
bulkProdMap.put(bk.Bulk_acc__c,bk);
}
}
List<RecordType> rtList1=[Select Id from RecordType where SobjectType = 'Task' and Name = 'BB Tasks' Limit 1];
List<Account> accList = [select id,ownerId,Name,Agent_Id__c,Status__c,RecordtypeId from Account Where Id IN:accountIds and RecordTypeId=:rtList[0].Id Limit 1];
for(Account acc:accList){
if((trigger.isUpdate)&&bulkProdMap.containsKey(acc.Id)){
task t1 = new task ();
t1.RecordTypeId = rtList1[0].Id;
t1.ActivityDate = Date.today()+2;
t1.Status = 'Not Started';
t1.Description = 'New Contract----------------. Please make personal contact with the agent';
t1.Subject = 'New Contracted Follow Up';
t1.OwnerId = acc.ownerId;
t1.WhatId = acc.Id;
t1.Priority = 'High';
taskToInsert.add(t1);
}
if(taskToInsert.Size()>0){
insert taskToInsert;
}
}
}
Here I am trying to insert tasks from account using apex trigger. This is working for individual edits but if I am trying to update the accounts using data laoder it is inserting tasks for those 5 accounts instead it is inserting task for one account. Here is my trigger please let me know the how to make this trigger to work for bulk updates using data loader.
trigger TaskInsertTriggerOnAccount on Account (after update) {
List <RecordType> rtList = [SELECT Id from RecordType where SobjectType='Account' and Name = 'BB' Limit 1];
List <task> taskToInsert = new List <task> ();
Set<Id> accountIds = new Set<Id>();
for(Account acc:Trigger.New){
if(acc.Agent_Id__c!=NULL&&acc.Contracting_Completed_Date__c==date.Today()&&acc.Contracting_Complete__c==True&&acc.RecordTypeId==rtList[0].Id&&acc.status__c=='Active'){
accountIds.add(acc.Id);
}
}
Map<Id,Bulk_Product__c> bulkProdMap = new Map<Id,Bulk_Product__c>();
List<Bulk_Product__c> bulkProdList = [Select Id,Bulk_Code__c,Bulk_acc__c from Bulk_Product__c Where Bulk_acc__c IN :accountIds and Bulk_Code__c = 'CCM' Limit 1];
for(Bulk_Product__c bk:bulkProdList ){
if(ag.Bulk_Code__c!=NULL){
bulkProdMap.put(bk.Bulk_acc__c,bk);
}
}
List<RecordType> rtList1=[Select Id from RecordType where SobjectType = 'Task' and Name = 'BB Tasks' Limit 1];
List<Account> accList = [select id,ownerId,Name,Agent_Id__c,Status__c,RecordtypeId from Account Where Id IN:accountIds and RecordTypeId=:rtList[0].Id Limit 1];
for(Account acc:accList){
if((trigger.isUpdate)&&bulkProdMap.containsKey(acc.Id)){
task t1 = new task ();
t1.RecordTypeId = rtList1[0].Id;
t1.ActivityDate = Date.today()+2;
t1.Status = 'Not Started';
t1.Description = 'New Contract----------------. Please make personal contact with the agent';
t1.Subject = 'New Contracted Follow Up';
t1.OwnerId = acc.ownerId;
t1.WhatId = acc.Id;
t1.Priority = 'High';
taskToInsert.add(t1);
}
if(taskToInsert.Size()>0){
insert taskToInsert;
}
}
}
Why are you using LIMIT 1 here? i think this is the issue. Please replace above lines with below code.
Please let us know if it helps you.
you are right. But if i am removing that limit 1 it is thorwing the below error:
caused by: System.DmlException: Insert failed. First exception on row 0 with id 00TL000000CCmCXMA1; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
t1.OwnerId = acc.ownerId;
Hi uma,
.
Do you have any active trigger on Task Object. Please check debug logs if any other code is getting fired in same execution context.