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

Salesforce Certification Course
Basics of Cloud Computing: Overview of each of three building blocks in cloud applications SAAS PAAS IAAS Different Cloud Service providers Salesforce Architecture: Introduction...

Difference Between Insert/Update And Database.Insert/ Database.Update
insert/update Database.insert/Database.update Assume that you are inserting 100 records. If any one of the record fail due to error then entire operation will fail. None of the records will be saved...

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...

Any one can become a Salesforce Admin or Salesforce Developer
There are many opportunities in Salesforce as every organization is moving towards cloud computing nowadays. Salesforce has introduced new features like Salesforce Lightining, Salesforce Wave Analytics,Salesforce...

Java To Salesforce Connection
Java to Salesforce Connection Step 1: Generate or Obtain the Web Service WSDL To generate the WSDL file for your organization: Log in to your Enterprise, Unlimited, or Developer Edition Salesforce...
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