Trigger Scenarios:- (Prepared By Suresh Kumar tvs)
Scenario one:- (All Opportunity Triggers)
//Trigger to display error message when stage name is change from 'Order closed' to other values
Part one 1 (While trigger.old) :-
if(Trigger.isupdate && Trigger.isBefore){
for(Opportunity oldOpportunity:trigger.old){
if(oldOpportunity.StageName == 'Order Closed'){
trigger.new[0].addError('An Opportunity cannot be edited once the opportunity stage is Order Closed');
}
}
Part 2(While Trigger.new):-
if(Trigger.isupdate && Trigger.isBefore){
set<id> opplistid=new set<id>();
for(Opportunity newopportunity:Trigger.new){
opplistid.add(newopportunity.id);
}
list<Quote> Qlist = [select OpportunityId,Customization_Payment_Criteria__c,Approval_Status_Quote__c from Quote where OpportunityId=:opplistid];
for(Opportunity newopportunity: Trigger.new){
Opportunity old=trigger.oldmap.get(newopportunity.Id);
if(newopportunity.Record_Type_Name__c=='Watches' || newopportunity.Record_Type_Name__c=='Jewellery'||newopportunity.Record_Type_Name__c=='Lifestyle'){
if((old.StageName != newopportunity.StageName)){
for(Quote qq:Qlist){
if(qq.OpportunityId==newopportunity.id && qq.Customization_Payment_Criteria__c =='Free Of Cost' && qq.Approval_Status_Quote__c!='Approved'){
newopportunity.addError('cannot change Stage as Quote Customization Payment Criteria is Free of cost and quote is not approved');
}
}
}
}
}
}
Scenario Two:-
//Trigger on checking attachment size and displaying error for different Record type
if(Trigger.isAfter && Trigger.isupdate){
set<id> oppId=new set<id>();
for(Opportunity newopportunity:Trigger.new){
oppId.add(newopportunity.id);
}
Integer Vattachmentcount = 0;
Integer Invoicecount = 0;
List<Attachment> ca = [Select id,Name from Attachment where ParentId=:oppId limit 99];
for(Integer i=0;i<ca.size();i++){
if(ca[i].Name.indexOfIgnoreCase('validated')>=0 ){
Vattachmentcount++;
}
if(ca[i].Name.indexOfIgnoreCase('invoice')>=0 ){
Invoicecount++;
}
}
for(Opportunity newOpportunity:Trigger.new){
if(newOpportunity.Record_Type_Name__c == 'Jewellery' && newOppor
tunity.Customization_Status__c == 'Validated' && (newOpportunity.StageName == 'Proforma Invoice'||newOpportunity.StageName == 'Received PO/Order Won' ||newOpportunity.StageName == 'Payment/Delivery' ||newOpportunity.StageName == 'Order Closed' )&& Vattachmentcount == 0){
newOpportunity.addError('Please upload the appropriate file with the keyword "validated" included within the file name');
}
if((newOpportunity.Record_Type_Name__c == 'Lifestyle' || newOpportunity.Record_Type_Name__c == 'Watches') && newOpportunity.Customization_Status__c == 'Validated' && (newOpportunity.StageName == 'Proforma Invoice'||newOpportunity.StageName == 'Received PO/Order Won' ||newOpportunity.StageName == 'Payment/Delivery' ||newOpportunity.StageName == 'Order Closed' ) && Vattachmentcount == 0){
newOpportunity.addError('Please upload the appropriate file with the keyword "validated" included within the file name');
}
if(newOpportunity.Delivery_Status__c == 'Completed' && newOpportunity.StageName == 'Payment/Delivery' && Invoicecount == 0){
newOpportunity.addError('Please upload the appropriate file with the keyword "Invoice" included within the file name');
}
}
}
}