I want to update a check box in Opportunity obj.but it is throwing a error that field is read only. Please suggest me how I can update my field. My logic is if account having Lead and lead status not converted then for that same account when I am creating a opportunity at that time my opportunity field get checked as true.Please help me.
trigger CopyleadvaluetoOpp on Opportunity (after insert) {
Set <String> OppotunityCustId = new Set <String> ();
for (Opportunity Opp : trigger.New) {
OppotunityCustId.add(Opp.Customer_Id__c);
}
//Map <String, Lead> matchingLeadMap = new Map <String, Lead> ();
List <Lead> newLead = new List<Lead> ([Select Id,Status,Customer_Code__c From Lead Where Customer_Code__c IN :OppotunityCustId AND Status != 'Converted']);
/*{
matchingLeadMap.put(Ld.Status, Ld);
}*/
List <Opportunity> opportunityToUpdate = new List <Opportunity> ();
for (Opportunity Opp : trigger.New) {
if (newLead.size() > 0) {
if (newLead != null) {
// we found a mathing one
Opp.Lead_Exception__c = true;
}
//Opp.adderror('Lead already exists for this account. Please convert lead to create account.');
// add it to a separate list and update it
//opportunityToUpdate.add(Opp);
}
}
update OpportunityToUpdate;
}