Scenario 7:-
//Checking the model number with the existence values while inserting or updating
before insert,before update
//Variable Declaration
List<Product__c> allProducts = new List<Product__c>();
map<string,id> nameMap = new map<string,id>();
set<String> productNameSet = new Set<String>();
for(Product__c prdEach:trigger.new){
productNameSet.add(prdEach.name);
}
//Retrieving all the product records
try{
allProducts=[select Name,id from Product__c where name in:productNameSet limit 50000];
}Catch(Exception exep){
System.debug('****Exception*****'+exep);
}
for(product__c prd:allProducts){
nameMap.put(prd.name.toUppercase(),prd.id);
}
//Iterating The list of all products and Identifying the Duplicates. If the Duplicates are existed Display the error message.
for(Product__c newprd:trigger.new){
if(nameMap.containsKey(newprd.name.toUppercase()) && (trigger.isinsert||(trigger.isupdate && nameMap.get(newprd.name.toUppercase())!=null&&nameMap.get(newprd.name.toUppercase())!=newprd.id))){
newprd.name.adderror('The model is Already existed');
}
}
Scenario 8:-
//QtValueAfterCust
After update
if (!RecurciveTrigger.hasAlreadyCreatedFollowUpTasks()) {
set<id> qid=new set<id>();
for(quote q:trigger.new){
if(q.Free_of_cost_Approved__c==true && q.Record_Type_Name__c == 'Jewellery'){
qid.add(q.ID);
}
}
List<QuoteLineItem> updatelineItem =new List<QuoteLineItem>();
if(!qid.isempty()){
for(QuoteLineItem qtl1:[select id,Sales_Price_c__c,QuoteId,TotalPrice from QuoteLineItem where QuoteId=:qid and Extra_Charges__c ='true' ]){
qtl1.Sales_Price_c__c=0;
qtl1.UnitPrice=0;
updatelineItem.add(qtl1);
}
}
RecurciveTrigger.setAlreadyCreatedFollowUpTasks();
if(!updatelineItem.isempty())
update updatelineItem;
}
Scenario 8:-
// Can not delete an approved Quote
before delete
List<profile> pp = [Select Name From Profile where Id = :UserInfo.getProfileId()];
for(Quote q:trigger.old)
{
if(q.Opportunity.Record_Type_Name__c != 'Jewellery' && q.Opportunity.Record_Type_Name__c != 'Lifestyle' && q.Opportunity.Record_Type_Name__c != 'Watches')
{
if(q.Approval_Status__c == 'Approved' && pp[0].name!='System Administrator'){
q.addError('Can not delete an approved Quote');
}
}
Scenario 9:-
//QuoteLineItemBeforeInsetUpdate
after insert,after update,after delete
public static boolean flag = true;
if(flag == true)
{
//added
flag = false;
Set<id> quotId = new Set<Id>();
Decimal Sum = 0;
Decimal optionalSum = 0;
if(trigger.isInsert || trigger.isUpdate){
for(Quote_Items__c q: trigger.new){
quotId.add(q.Quote__c);
}
}
if(trigger.isDelete){
for(Quote_Items__c q: trigger.old){
quotId.add(q.Quote__c);
}
}
List<Quote_Items__c> LineItem =new List<Quote_Items__c>();
List<Quote> QoutlIst =new List<Quote>();
if(!Test.isRunningTest()){
LineItem = [Select id,Total_Value__c,Optional__c from Quote_Items__c Where Quote__c IN: quotId ];
QoutlIst = [Select id,Total_Value_PECSA_new__c,Opportunity.Amount,opportunityId from Quote where id In: quotId ];
}
list<opportunity> opplist = new list<opportunity> ();
for(Quote_Items__c q : LineItem){
if(q.Optional__c == true){
optionalSum = optionalSum + q.Total_Value__c;
system.debug('+++++++++++++++optionalSum : '+optionalSum );
}
sum = sum + q.Total_Value__c;
system.debug('+++++++++++++++sum : '+sum );
}
for(Quote q : QoutlIst){
if(optionalSum != 0){
q.Total_Value_PECSA_new__c = optionalSum;
q.Opportunity.amount =q.Total_Value_PECSA_new__c;
system.debug('+++++++++++++++q.Opportunity.amount : '+q.Opportunity.amount);
opportunity obj = new opportunity(id =q.opportunityId,Amount =q.Opportunity.amount );
opplist.add(obj);
}
else{
q.Total_Value_PECSA_new__c = Sum;
q.Opportunity.Amount =q.Total_Value_PECSA_new__c;
opportunity obj = new opportunity(id =q.opportunityId,Amount =q.Opportunity.amount );
opplist.add(obj);
}
}
Update QoutlIst;
system.debug('+++++++++++++++opplist: '+opplist);
if(opplist.size()>0){
update opplist;
}
//added
//flag = true;
}