You need to sign in to do that
Don't have an account?
Integration 39
Update opportunity field when won
Hello,
I have got a trigger the insert into custom object when opportunity is won.
The problem i am having is if someone update the opportunity after it is won another record is inserted into the custom object.
To avoid this i tried adding a custom field to specify if this is the first time it has been updated when the opportunity has been won.
I am getting a front end error saying the opportunity is readonly, how can i fix this?
trigger CreateEnrolment on Opportunity (After update) {
List<enrolment__c> createenrolment = new List <enrolment__c> ();
for (Opportunity o: trigger.new) {
if (o.isWon == true && o.converted_2_enrolment__c == FALSE) {
createenrolment.add(
new enrolment__c(
Campaign_Medium__c = o.Campaign_Medium__c,
Campaign_Name__c = o.Campaign_Name__c,
Product__c = o.Product__c,
Opportunity__c = o.id,
Account__c = o.AccountId,
Call_Centre__c = o.Call_Centre__c,
Who_Enrolled__c = UserInfo.getUserId()));
o.converted_2_enrolment__c = TRUE;
update o;
}
}
try {
insert createenrolment;
}
catch (Exception Ex){
system.debug(Ex);
}
}
I have got a trigger the insert into custom object when opportunity is won.
The problem i am having is if someone update the opportunity after it is won another record is inserted into the custom object.
To avoid this i tried adding a custom field to specify if this is the first time it has been updated when the opportunity has been won.
I am getting a front end error saying the opportunity is readonly, how can i fix this?
trigger CreateEnrolment on Opportunity (After update) {
List<enrolment__c> createenrolment = new List <enrolment__c> ();
for (Opportunity o: trigger.new) {
if (o.isWon == true && o.converted_2_enrolment__c == FALSE) {
createenrolment.add(
new enrolment__c(
Campaign_Medium__c = o.Campaign_Medium__c,
Campaign_Name__c = o.Campaign_Name__c,
Product__c = o.Product__c,
Opportunity__c = o.id,
Account__c = o.AccountId,
Call_Centre__c = o.Call_Centre__c,
Who_Enrolled__c = UserInfo.getUserId()));
o.converted_2_enrolment__c = TRUE;
update o;
}
}
try {
insert createenrolment;
}
catch (Exception Ex){
system.debug(Ex);
}
}
trigger CreateEnrolment on Opportunity (after update) {
List<enrolment__c> createenrolment = new List <enrolment__c> ();
for (Opportunity o: trigger.new) {
if (o.isWon == true && o.HasOpportunityLineItem == true) {
String opptyId = o.Id;
OpportunityLineItem[] OLI = [Select id,converted_2_enrolment__c From OpportunityLineItem where OpportunityId = :opptyId];
for(OpportunityLineItem ol: OLI){
if (ol.converted_2_enrolment__c == FALSE) {
createenrolment.add(
new enrolment__c(
Campaign_Medium__c = o.Campaign_Medium__c,
Campaign_Name__c = o.Campaign_Name__c,
Product__c = o.Product__c,
Opportunity__c = o.id,
Account__c = o.AccountId,
Call_Centre__c = o.Call_Centre__c,
Who_Enrolled__c = UserInfo.getUserId()));
ol.converted_2_enrolment__c = TRUE;
}
}
if (OLI != NULL) {
update OLI;
}
}
}
try {
insert createenrolment;
}
catch (Exception Ex){
system.debug(Ex);
}
All Answers
Please use below code ,error is comming as after insert and update as the record is saved to data base so the record is always readonly .
.
Please use below code .
Choose best answer if it solves your problem .
trigger CreateEnrolment on Opportunity (after update) {
List<enrolment__c> createenrolment = new List <enrolment__c> ();
for (Opportunity o: trigger.new) {
if (o.isWon == true && o.HasOpportunityLineItem == true) {
String opptyId = o.Id;
OpportunityLineItem[] OLI = [Select id,converted_2_enrolment__c From OpportunityLineItem where OpportunityId = :opptyId];
for(OpportunityLineItem ol: OLI){
if (ol.converted_2_enrolment__c == FALSE) {
createenrolment.add(
new enrolment__c(
Campaign_Medium__c = o.Campaign_Medium__c,
Campaign_Name__c = o.Campaign_Name__c,
Product__c = o.Product__c,
Opportunity__c = o.id,
Account__c = o.AccountId,
Call_Centre__c = o.Call_Centre__c,
Who_Enrolled__c = UserInfo.getUserId()));
ol.converted_2_enrolment__c = TRUE;
}
}
if (OLI != NULL) {
update OLI;
}
}
}
try {
insert createenrolment;
}
catch (Exception Ex){
system.debug(Ex);
}