0

I tried this code but I am getting error anyone please help on this:

trigger fillValue on Employee__c (after insert, after update) {
    map<decimal,contact> mapcon = new map<decimal,contact>();
    list<employee__C> ee = new list<employee__c>();
    list<contact> con = [select id,test__c from contact where test__C != null];

    if(trigger.isinsert){
        for(employee__c emp : trigger.new){
            for(contact c :con){
                if(emp.test__C == c.test__c){
                    mapcon.put(c.test__c,c);
                }
            }
        }

        for(employee__c eee : trigger.new){
            if(mapcon.size()>0){
                eee.contact__C = mapcon.get(eee.test__C).id;
            }
            ee.add(eee);
        }
        insert ee;
    }
}

Error:

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

0

2 Answers 2

1

You can not update/insert same records which are part of trigger variable Trigger.New in after trigger

You must use before trigger if you want to make changes in same object records on which trigger is written.

trigger fillValue on Employee__c (before insert, before update) {
    map<decimal,contact> mapcon = new map<decimal,contact>();
    list<employee__C> ee = new list<employee__c>();
    list<contact> con = [select id,test__c from contact where test__C != null];

    if(trigger.isinsert){
        for(employee__c emp : trigger.new){
            for(contact c :con){
                if(emp.test__C == c.test__c){
                    mapcon.put(c.test__c,c);
                }
            }
        }

        for(employee__c eee : trigger.new){
            if(mapcon.size()>0){
                eee.contact__C = mapcon.get(eee.test__C).id;
            }
            ee.add(eee);
        }       }
}
2
  • but i need to write trigger using after inser can u plaese give me any suggestions
    – user123
    Commented Jan 12, 2016 at 11:02
  • What is the use case of writing trigger on after insert. As per the code given, there should not be a reason to write a trigger on after event. Also make sure it is not null mapcon.get(eee.test__C)
    – Ashwani
    Commented Jan 12, 2016 at 11:05
0

In after insert you are trying to insert record it will surely give you error. So use @Ashwani's answer and remove the insert ee; line from your code. It will solve your problem.

if(mapcon.size()>0 && mapcon.containsKey(eee.test__C)){
       eee.contact__C = mapcon.get(eee.test__C).id;
}
2
  • i tried what u told but i m getting error in this line "eee.contact__C = mapcon.get(eee.test__C).id;"
    – user123
    Commented Jan 12, 2016 at 11:04
  • @user123 update my answer. Commented Jan 12, 2016 at 11:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .