+ Start a Discussion
neckrneckr 

Need help Updating field values in after trigger

I realize that trigger.new is read only so I created a map to update the fields in my trigger but still getting a read-only error. I cut out most of my code and left the relevant parts.  My code compiles but gives and error when running.

 

Any suggestions?

 

 

trigger ConVerificationRecordSetup on Con_Service_Task_Request__c (after insert, after update) 
{

if (FireTrigger.getRun()) {


Map<ID, List<Trade_Reference_Report__c>> mapExistingTRVs = new Map<ID, List<Trade_Reference_Report__c>>();
Map<ID, Con_Service_Task_Request__c> mapST = new Map<ID, Con_Service_Task_Request__c>();



List <Trade_Reference_Report__c> listTRV1 = new List<Trade_Reference_Report__c>();
List <Contact> listTRVCompanyContact1 = new List<Contact>();

List <Trade_Reference_Report__c> listTRV2 = new List<Trade_Reference_Report__c>();
List <Contact> listTRVCompanyContact2 = new List<Contact>();

List <Trade_Reference_Report__c> listTRV3 = new List<Trade_Reference_Report__c>();
List <Contact> listTRVCompanyContact3 = new List<Contact>();


for(Con_Service_Task_Request__c ST : trigger.new){
         
         
         List<Trade_Reference_Report__c> ExistingTRVs = [SELECT ID FROM Trade_Reference_Report__c 
         WHERE Account__c = : ST.Account__c AND TRV_Work_Category__c = : ST.Category__c limit 3]; 
             if(ExistingTRVs == NULL)  {
                ExistingTRVs = New List<Trade_Reference_Report__c>();
                }
         mapExistingTRVs.put(ST.id, ExistingTRVs); 
         mapST.put(ST.id, ST);
    }
    


for (Con_Service_Task_Request__c ST : Trigger.new) {



//CHECK TO SEE IF TRADE REFERENCE FOR PARTICULAR CATEGORY EXISTS, IF SO ASSIGN ID'S TO ST, IF NOT CREATE 3
//NEW TRV RECORDS
if (mapExistingTRVs.get(ST.id).size() > 0 )
{

mapST.get(ST.id).Trade_Reference_Report_1__c = mapExistingTRVs.get(ST.id).get(0).ID;

mapST.get(ST.id).Trade_Reference_Report_2__c = mapExistingTRVs.get(ST.id).get(1).ID;

mapST.get(ST.id).Trade_Reference_Report_3__c = mapExistingTRVs.get(ST.id).get(2).ID;

}

if (mapExistingTRVs.get(ST.id).size() == 0 )
{ 
 Contact TRVCompanyContact1 = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'TRV Company', LastName = 'Enter Last Name 1');
 listTRVCompanyContact1.add(TRVCompanyContact1);
 
 Contact TRVCompanyContact2 = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'TRV Company', LastName = 'Enter Last Name 2');
 listTRVCompanyContact2.add(TRVCompanyContact2);

Contact TRVCompanyContact3 = new Contact(AccountId = ST.Account__c , Contact_Type__c = 'TRV Company', LastName = 'Enter Last Name 3');
 listTRVCompanyContact3.add(TRVCompanyContact3);
       
}

}

insert listTRVCompanyContact1;
insert listTRVCompanyContact2;
insert listTRVCompanyContact3;


for (Integer i=0; i<Trigger.new.size(); i++) { 


if(listTRVCompanyContact1.size()> 0){ 
Trade_Reference_Report__c TRV1 = new Trade_Reference_Report__c(Account__c = Trigger.new[i].Account__c , TRV_Company_Contact__c = listTRVCompanyContact1[i].ID, TRV_Principal_Contact__c = mapPrincipalAcctCont.get(Trigger.new[i].id), TRV_Work_Category__c = Trigger.new[i].Category__c );    
listTRV1.add(TRV1);
}

if(listTRVCompanyContact2.size()> 0){ 
Trade_Reference_Report__c TRV2 = new Trade_Reference_Report__c(Account__c = Trigger.new[i].Account__c , TRV_Company_Contact__c = listTRVCompanyContact2[i].ID, TRV_Principal_Contact__c = mapPrincipalAcctCont.get(Trigger.new[i].id), TRV_Work_Category__c = Trigger.new[i].Category__c );    
listTRV2.add(TRV2);
}

if(listTRVCompanyContact3.size()> 0){ 
Trade_Reference_Report__c TRV3 = new Trade_Reference_Report__c(Account__c = Trigger.new[i].Account__c , TRV_Company_Contact__c = listTRVCompanyContact3[i].ID, TRV_Principal_Contact__c = mapPrincipalAcctCont.get(Trigger.new[i].id), TRV_Work_Category__c = Trigger.new[i].Category__c );    
listTRV3.add(TRV3);
}

} 



insert listTRV1;
insert listTRV2;
insert listTRV3;


for (Integer i=0; i<Trigger.new.size(); i++) { 

Con_Service_Task_Request__c ST = Trigger.new[i];

if(listTRV1.size()> 0){ 

ST.Trade_Reference_Report_1__c = listTRV1[i].id;

}

if(listTRV2.size()> 0){ 

ST.Trade_Reference_Report_2__c = listTRV2[i].id;

}


if(listTRV3.size()> 0){ 

ST.Trade_Reference_Report_3__c = listTRV3[i].id;

}
}


update mapST.values();

}



}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
jhurstjhurst

When you are doing the:

 

Con_Service_Task_Request__c ST : trigger.new

 

And:

 

Con_Service_Task_Request__c ST = Trigger.new[i];

 

You are simply making a shallow copy of the record (it will just be a reference rather than an independent record).  Since it is a reference it simply points to the same un-editable data.

 

There are a couple of ways to do this:

 

1. Build a new list from a query at the start of the trigger:

 

Con_Service_Task_Request__c[] sts = [select ID, <FIELDs> from Con_Service_Task_Request__c where ID in :Trigger.new];

You should then have an independent map that you can work with.

 

2. Build a new list using the deepClone() function for lists (

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_methods_system_list.htm?SearchType=Stem):

 

Con_Service_Task_Request__c[] sts = Trigger.new.deepClone();

 

Hope this helps.

Jay

All Answers

jhurstjhurst

When you are doing the:

 

Con_Service_Task_Request__c ST : trigger.new

 

And:

 

Con_Service_Task_Request__c ST = Trigger.new[i];

 

You are simply making a shallow copy of the record (it will just be a reference rather than an independent record).  Since it is a reference it simply points to the same un-editable data.

 

There are a couple of ways to do this:

 

1. Build a new list from a query at the start of the trigger:

 

Con_Service_Task_Request__c[] sts = [select ID, <FIELDs> from Con_Service_Task_Request__c where ID in :Trigger.new];

You should then have an independent map that you can work with.

 

2. Build a new list using the deepClone() function for lists (

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_methods_system_list.htm?SearchType=Stem):

 

Con_Service_Task_Request__c[] sts = Trigger.new.deepClone();

 

Hope this helps.

Jay

This was selected as the best answer
neckrneckr

Thank you. Choose option 2