You need to sign in to do that
Don't have an account?
Zoren Domingo
Apex Trigger - Create a record under opportunity, based on the OpportunityLineItem custom field.
Hi, can anyone can help me, I have little experience with apex trigger, I need to create a record base on the OpportunityLineItem custom field.
Multiple Records to create:
Campaign_Scorecard__c - custom object - Look-up relationship with Opportunity.
trigger OpptLineITmesTrigger on OpportunityLineItem ( after insert,after update) {
if(trigger.isAfter) {
List<Campaign_Scorecard__c> insertCampns = new list<Campaign_Scorecard__c>();
for (OpportunityLineItem oli: trigger.new){
if(<Your Conditions>) {
Campaign_Scorecard__c campRec = new Campaign_Scorecard__c(OpportunityId=oli.opportunityId,...........<Other your fields to popualte>)
insertCampns.add(campRec);
}
}
if(!insertCampns.isEmpty()) {
try {
insert insertCampns;
}catch(exception e){
OpportunityLineItem ol = trigger.new[0];
ol.add.Error('your error');
}
}
}
}
Hi, thank you for your help, but I have a problem - the record created is 2 records, I only need to create one record on each condition.
Here's my current code.
trigger AdworksTriggerForCampaignScoreCard on OpportunityLineItem (after insert, after update) {
List<Campaign_Scorecard__c> scorecardToInsert = new list<Campaign_Scorecard__c>();
if(trigger.isAfter) {
for (OpportunityLineItem oli: trigger.new){
if(oli.Reporting_Length__c == 'Closed Loop Analysis (Client Data) Acxiom 60 Days') {
Campaign_Scorecard__c cs = new Campaign_Scorecard__c(Opportunity__c=oli.opportunityId);
cs.Vendor_Measurement__c = 'Closed Loop Analysis (Client Data) Acxiom';
cs.Days__c = '60';
scorecardToInsert.add(cs);
}
}
}
try {
insert scorecardToInsert;
} catch (system.Dmlexception e) {
system.debug (e);
}
}
On next save you can check if that fiag i.e. custom field is set, so record should not be created.
Also try to use before trigger on line item and @future for creating records, so that there will not be a problem of limits.
Hope this works.
Thanks