function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
amritamrit 

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);
        
    
}

}

 

 

Yoganand GadekarYoganand Gadekar

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.

amritamrit

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.

if(cpi1.size()>0){
if((Trigger.isBefore))
{
if((Trigger.isInsert))
{
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;
       
        
}
 } 
 }
 if(Trigger.isUpdate)
 {
 for(Opportunity__c l2 : trigger.new){

          l2.Actual_CPI__c = cpi1.get(l2.Completes_Required__c).Actual_CPIchanged__c;
       // l1.CPI_in__c = cpi1.get(l1.Completes_Required__c).Actual_CPIchanged__c;
       
        system.debug('ActualCPI'+ l2.Actual_CPI__c);   
}
}  

}

 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

Avidev9Avidev9
By seeing the code, Seems like you are using wrong terminology. If I am not wrong you are trying to assign values to CPI_in__c and Actual_CPI__c fields. Can you tell me what you are getting in the debug that you slated. I would probably assume that

somehow if(cpi1.size()>0 ) is evaluated as false. and the lines inside are not executed at all
Avidev9Avidev9
Hey it has to be a before trigger so that you can assign values. Seems like you are using "After Insert" unlike the posted code where it is "Before Insert".


You have to change After Insert to Before Insert
amritamrit

Hi ,

 

Yes im using before insert and before update in trigger.im gettiing Actual cpi in debug log like this

 

13:15:03.055 (55766000)|USER_DEBUG|[45]|DEBUG|CPI{750=CPI__c:{Name=General, Market__c=India, Actual_CPIchanged__c=4.10, Id=a0QN0000000AT1wMAG, Audience__c=Gen Pop, Completes_Required__c=750, Bid_LOI_in_minutes__c=5}}
13:15:03.055 (55782000)|SYSTEM_METHOD_EXIT|[45]|System.debug(ANY)
13:15:03.055 (55799000)|SYSTEM_METHOD_ENTRY|[42]|Database.QueryLocatorIterator.hasNext()
13:15:03.055 (55821000)|SYSTEM_METHOD_EXIT|[42]|Database.QueryLocatorIterator.hasNext()
13:15:03.055 (55848000)|SYSTEM_METHOD_ENTRY|[47]|System.debug(ANY)
13:15:03.055 (55875000)|USER_DEBUG|[47]|DEBUG|@@@@@@@@@@@@@@<@@@cpi @@@@@@@@cpi1 
13:15:03.055 (55890000)|SYSTEM_METHOD_EXIT|[47]|System.debug(ANY)
13:15:03.055 (55909000)|SYSTEM_METHOD_ENTRY|[52]|MAP<Decimal,CPI__c>.size()
13:15:03.055 (55950000)|SYSTEM_METHOD_EXIT|[52]|MAP<Decimal,CPI__c>.size()
13:15:03.056 (56001000)|SYSTEM_METHOD_ENTRY|[54]|LIST<Opportunity__c>.iterator()
13:15:03.056 (56036000)|SYSTEM_METHOD_EXIT|[54]|LIST<Opportunity__c>.iterator()
13:15:03.056 (56057000)|SYSTEM_METHOD_ENTRY|[54]|system.ListIterator.hasNext()
13:15:03.056 (56078000)|SYSTEM_METHOD_EXIT|[54]|system.ListIterator.hasNext()
13:15:03.056 (56134000)|SYSTEM_METHOD_ENTRY|[56]|MAP<Decimal,CPI__c>.get(Object)
13:15:03.056 (56163000)|SYSTEM_METHOD_EXIT|[56]|MAP<Decimal,CPI__c>.get(Object)
13:15:03.056 (56248000)|SYSTEM_METHOD_ENTRY|[57]|MAP<Decimal,CPI__c>.get(Object)
13:15:03.056 (56274000)|SYSTEM_METHOD_EXIT|[57]|MAP<Decimal,CPI__c>.get(Object)
13:15:03.056 (56320000)|SYSTEM_METHOD_ENTRY|[54]|system.ListIterator.hasNext()
13:15:03.056 (56335000)|SYSTEM_METHOD_EXIT|[54]|system.ListIterator.hasNext()
13:15:03.056 (56365000)|SYSTEM_METHOD_ENTRY|[61]|LIST<Opportunity__c>.iterator()
13:15:03.056 (56385000)|SYSTEM_METHOD_EXIT|[61]|LIST<Opportunity__c>.iterator()
13:15:03.056 (56397000)|SYSTEM_METHOD_ENTRY|[61]|system.ListIterator.hasNext()
13:15:03.056 (56408000)|SYSTEM_METHOD_EXIT|[61]|system.ListIterator.hasNext()
13:15:03.056 (56444000)|SYSTEM_METHOD_ENTRY|[63]|MAP<Decimal,CPI__c>.get(Object)
13:15:03.056 (56474000)|SYSTEM_METHOD_EXIT|[63]|MAP<Decimal,CPI__c>.get(Object)
13:15:03.056 (56557000)|SYSTEM_METHOD_ENTRY|[66]|String.valueOf(Object)
13:15:03.056 (56577000)|SYSTEM_METHOD_EXIT|[66]|String.valueOf(Object)
13:15:03.056 (56587000)|SYSTEM_METHOD_ENTRY|[66]|System.debug(ANY)
13:15:03.056 (56596000)|USER_DEBUG|[66]|DEBUG|ActualCPI4.10

 

Avidev9Avidev9
i can see the reocrds are assigned DEBUG|ActualCPI4.10

what is the problem now ?