UrbanPro
true

Learn Salesforce Developer from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Trigger Sceniors - session 3

S
Suresh Kumar Tavva
15/12/2016 0 0

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;

   }

 

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Difference Between Data Loader And Import Wizard In Salesforce
Difference between Dataloader and Import Wizard in Salesforce: Dataloader Import Wizard It supports more than 50000 records. It supports only up to 50000. It supports...

What Is SOQL And SOSL?
1. SOQL: Salesforce Object Query Language SOQL: Salesforce Object Query Language SOQL Purpose: To fetch info. from an object and related objects. We can write query on one object while querying...

What is Mixed-DML-Operation Error And How To Avoid?
If we perform DML operation on standard/custom object and global objects(User, UserRole, Group, GroupMember, Permission Set, etc...) in same transaction this error will come. To avoid this error, we should...

Trigger Best Practices
Triggers - Best Practices in Salesforce 1. Future methods, SOQL and DML: Avoid writing Future methods, SOQL and DML inside the "For" loop.2. Bulkify the trigger: Start developing the logic...
R

Rajiv Kumar Singh

0 1
0

What Is Batch Apex And Its Methods ?
1. We can call the apex code by creating object for the class (or) if the variables or methods are static then we can call with class name. Apex Code in the trigger will execute automatically for...
X

Looking for Salesforce Developer Classes?

The best tutors for Salesforce Developer Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Salesforce Developer with the Best Tutors

The best Tutors for Salesforce Developer Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more