+ Start a Discussion
AribaGalaxyAribaGalaxy 

Before insert trigger not working; works on before update

We're working on our first trigger. I am trying to update a custom field in TASK with a list of Partner accounts related to the account the task is being created for. The code below works if I update a task, but it does not work upon creation of a new task. I tried to execute the trigger after update but received a read only error.

 

Any help is greatly appreciated. We're working on this in the free version of developer force if that matters.

 

trigger JWGetAssociations on Task (before insert, before update) {
    
    Map<Id, Id> TaskToAccountMap = new Map<Id, Id>();
    Map<Id, Id> AcctToPartnerMap = new Map<Id, Id>();
    Map<Id, Account> PartnerResponseMap = new Map<Id, Account>();

    for(Task jwtask : [SELECT Id, AccountID FROM Task WHERE ID in: trigger.new]){
        TaskToAccountMap.put(jwtask.id, jwtask.AccountId);
    }
    
    list <Id> partner1 = new list <Id>();
    for(Partner partner : [SELECT Id, AccountToId, AccountFromId FROM Partner WHERE AccountFromId in: TaskToAccountMap.values()]){
     
        AcctToPartnerMap.put(partner.AccountToId , partner.AccountToId);
    } 

    for(Account part : [SELECT Id, Name FROM Account WHERE ID in: AcctToPartnerMap.values()]){
        PartnerResponseMap.put(part.ID, part);
    }
    
    for(Task o : trigger.new){

        Id acctid = TaskToAccountMap.get(o.Id);

         list <String> partnerAcc = new list <String>(); 
         for( id Accid : AcctToPartnerMap.keyset()){
             Account acc = PartnerResponseMap.get(Accid);
             partnerAcc.add(acc.Name+'\n');
             partnerAcc.add('\n\n');
         }
         for(integer i=0;i<partnerAcc.size();i++){
         }
)
            if(o.jw_Associated_Distributors__c == null){
                o.jw_Associated_Distributors__c = '';
               for(integer i=0;i< partnerAcc.size();i++){
                       o.jw_Associated_Distributors__c += partnerAcc[i];  
                }
        }
    } 
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Update the following piece of code..you will need to allocate memory to update the task

for(integer i=0;i<partnerAcc.size();i++){
}
Task t = new task(Id = o.Id);
if(t.jw_Associated_Distributors__c == null){
t.jw_Associated_Distributors__c = '';
for(integer i=0;i< partnerAcc.size();i++){
t.jw_Associated_Distributors__c += partnerAcc[i];
}
}
tListToBeUpdated.add(t);
}

All Answers

souvik9086souvik9086

Do like this

 

trigger JWGetAssociations on Task (after insert, after update) {
if(FutureTriggerController.isFutureUpdate != true){

FutureTriggerController.isFutureUpdate =true;
Map<Id, Id> TaskToAccountMap = new Map<Id, Id>();
Map<Id, Id> AcctToPartnerMap = new Map<Id, Id>();
Map<Id, Account> PartnerResponseMap = new Map<Id, Account>();
List<Task> tListToBeUpdated = new List<Task>();

for(Task jwtask : [SELECT Id, AccountID FROM Task WHERE ID in: trigger.new]){
TaskToAccountMap.put(jwtask.id, jwtask.AccountId);
}

list <Id> partner1 = new list <Id>();
for(Partner partner : [SELECT Id, AccountToId, AccountFromId FROM Partner WHERE AccountFromId in: TaskToAccountMap.values()]){

AcctToPartnerMap.put(partner.AccountToId , partner.AccountToId);
}

for(Account part : [SELECT Id, Name FROM Account WHERE ID in: AcctToPartnerMap.values()]){
PartnerResponseMap.put(part.ID, part);
}

for(Task o : trigger.new){

Id acctid = TaskToAccountMap.get(o.Id);

list <String> partnerAcc = new list <String>();
for( id Accid : AcctToPartnerMap.keyset()){
Account acc = PartnerResponseMap.get(Accid);
partnerAcc.add(acc.Name+'\n');
partnerAcc.add('\n\n');
}
for(integer i=0;i<partnerAcc.size();i++){
}
)
if(o.jw_Associated_Distributors__c == null){
o.jw_Associated_Distributors__c = '';
for(integer i=0;i< partnerAcc.size();i++){
o.jw_Associated_Distributors__c += partnerAcc[i];
}
}
tListToBeUpdated.add(o);
}
if(tListToBeUpdated.size()>0){
upsert tListToBeUpdated;
}
}
}

 

Then create a controller to prevent recursive trigger call

public class FutureTriggerController{

public static boolean isFutureUpdate = false;

}

 

We are using this variable in the trigger.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

AribaGalaxyAribaGalaxy

Hi souvik, thank you for your reply.

When I tried your changes; I got the following error when creating a new task: (same I got before changes when I used after insert)

 

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger JWGetAssociations caused an unexpected exception, contact your administrator: JWGetAssociations: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.JWGetAssociations: line 34, column 1

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Update the following piece of code..you will need to allocate memory to update the task

for(integer i=0;i<partnerAcc.size();i++){
}
Task t = new task(Id = o.Id);
if(t.jw_Associated_Distributors__c == null){
t.jw_Associated_Distributors__c = '';
for(integer i=0;i< partnerAcc.size();i++){
t.jw_Associated_Distributors__c += partnerAcc[i];
}
}
tListToBeUpdated.add(t);
}

This was selected as the best answer
AribaGalaxyAribaGalaxy

Sam_SFDC15 -- Thank you so much. It works now.

 

 

I appreciate both of you for helping. Now, if I read correctly, I need to create a test case. Wish me luck.