You need to sign in to do that
Don't have an account?
amrit
How to make field editable in trigger
Hi,
I have a trigger which updating values in two fields ActualCPI and Proposed CPI.Here im able to insert the values in both field. Proposed CPI is an editable field.I want to change the value in Proposed CPI.But Im not able to update the value in Proposed CPI.
How can we make a field editable in trigger.
trigger CPIupdatevalues on Opportunity__c (before insert, before update) { Set<Decimal> comp = new Set<Decimal>(); Set<Decimal> bid = new Set<Decimal>(); Set<Decimal> inc=new Set<Decimal>(); List<String> aud=new List<String>(); List<String> market=new List<String>(); List<String> ratecard =new List<String>(); for(Opportunity__c l : trigger.new){ if(l.Completes_Required__c != null){ comp.add(l.Completes_Required__c); } if(l.Bid_LOI_in_minutes__c != null){ bid.add(l.Bid_LOI_in_minutes__c); } if(l.Incidence_Rate__c != null) { inc.add(l.Incidence_Rate__c); } if(l.Audience_B2B_B2C__c == 'B2B' || l.Audience_B2B_B2C__c == 'Gen Pop' &&l.Audience_B2B_B2C__c != 'Niche' ) { aud.add(l.Audience_B2B_B2C__c); } if(l.Market__c != null) { market.add(l.Market__c); } if(l.Rate_Card__c != null) { ratecard.add(l.Rate_Card__c); } } if(aud.size()> 0) { Map<Decimal, CPI__c> cpi1 = new Map<Decimal, CPI__c>(); for(CPI__c obj1 : [SELECT Id, Bid_LOI_in_minutes__c,Actual_CPI__c,Actual_CPIchanged__c,Audience__c, Completes_Required__c,Name,Market__c FROM CPI__c WHERE Bid_LOI_in_minutes__c IN :bid AND Completes_Required__c IN: comp AND Incidence_Rate__c IN:inc AND Audience__c IN:aud AND Name IN:ratecard AND Market__c IN:market ]){ cpi1.put(obj1.Completes_Required__c , obj1); system.debug('CPI'+cpi1); } system.debug('@@@@@@@@@@@@@@<@@@cpi @@@@@@@@'+'cpi1 '); if(cpi1.size()>0 ){ for(Opportunity__c l1 : trigger.new){ l1.Actual_CPI__c = cpi1.get(l1.Completes_Required__c).Actual_CPIchanged__c; l1.CPI_in__c = cpi1.get(l1.Completes_Required__c).Actual_CPIchanged__c ;//Here Im updating Proposed CPI } } else { for(Opportunity__c l1 : trigger.new){ l1.Actual_CPI__c =0; } } } if(aud.size()== 0) { // Now we have a set of unique bid names we want to verify, time to look them up. for(Opportunity__c l : trigger.new){ l.Actual_CPI__c =0; } } }
if(cpi1.size()>0 ){ for(Opportunity__c l1 : trigger.new){ l1.Actual_CPI__c = cpi1.get(l1.Completes_Required__c).Actual_CPIchanged__c; l1.CPI_in__c = cpi1.get(l1.Completes_Required__c).Actual_CPIchanged__c ;//Proposed CPI system.debug('ActualCPI'+ l1.Actual_CPI__c); } }
you cannot do that in a trigger.. you need to check the field level security of your field, if its editable you will be able to edit it in trigger...
Also make yourself aware of "when the records are locked in trigger", in such cases the records are read only and any attemt to change/update such records throws exceptions as record is read only.
Hi yoganad,
Thanks for the reply.
Yes I checked the field levelsecurity the field is editable only. Still not able to edit the field.
Yes it is throwing error when i changed the code like this.
Apex script unhandled trigger exception by user/organization: 005N0000000Rapk/00DN00000003Tye
Source organization: 00D90000000Ythf (null)
CPIupdatevalues: execution of AfterInsert
caused by: System.FinalException: Record is read-only
Trigger.CPIupdatevalues: line 60, column 1
Thanks
somehow if(cpi1.size()>0 ) is evaluated as false. and the lines inside are not executed at all
You have to change After Insert to Before Insert
Hi ,
Yes im using before insert and before update in trigger.im gettiing Actual cpi in debug log like this
what is the problem now ?