function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arief GunawanArief Gunawan 

Help apex trigger, nothing happen

so there`s custom object named term_of_payment__c, has a look up to opportunity.. I want to make a trigger that will automatically insert term of payment on opportunity.. this is what i`ve tried so far.. sry im new on salesforce need some teaching
 
trigger autoAddTOP on Opportunity (after insert, after update) {
   
        for (Opportunity opp : Trigger.new) {
        //get opportunity data
        List<Opportunity> oppList = [SELECT Id, Interval__c, Amount, Quantity__c FROM Opportunity WHERE Id = :opp.Id];
        List<Term_of_Payment__c> terms = [SELECT Id FROM Term_of_Payment__c WHERE Opportunity__c = :opp.Id];        
        //get all products from opportunity
        List<OpportunityLineitem> products = [SELECT Id, Quantity, TotalPrice, Name, UnitPrice
                                              FROM OpportunityLineitem
                                              WHERE OpportunityId = :opp.Id];
        
        List<Term_of_Payment__c> termsToInsert = new List<Term_of_Payment__c>();
        Integer seq = 1;
          
        //loop the term of payment based on frequency each product
        for (Opportunity oppty : oppList) {
            Decimal freq = 0;
            if (oppty.Interval__c != null) {
                freq = Decimal.ValueOf(oppty.Interval__c);
            }

            if (freq > 0) {
                Decimal amountTod = (oppty.Amount / freq).setScale(2);
                
                for (Integer i=1; i<=freq; i++) {
                    if (i == 1 && seq == 1) {
                    Term_of_Payment__c newTOP1 = new Term_of_Payment__c();
                    newTOP1.Sequence__c = 1;
                    newTOP1.Amount__c = amountTOD;
                    newTOP1.Document_Date__c = date.parse('02/05/2018') + 30;
                    termsToInsert.add(newTOP1);
                    
                    }
                    seq++;
                    
                }
            } 
        } insert termsToInsert;
    }
 }

 
Ravi Dutt SharmaRavi Dutt Sharma
Line 05 - query on opportunity is not required as you are inside the opportunity trigger itself. You can access the opportunity information using Trigger.new
Line 06 - The terms list that you queried is not used anywhere?

Both the above queries are inside for loop, avoid that. You will face governor limits when the trigger runs in bulk mode.
 
Arief GunawanArief Gunawan
there`s no error but nothing happened.. no term of payment record added on opportunity..
bhanu_prakashbhanu_prakash
Hi Arief Gunawan,
Mark as best answer, If it resloves !!​​
trigger autoTermFromOpp on Opportunity (after insert, after update) {  
		Set<Id> opptIds = new Set<Id>{};
        Map<Id, term_of_payment__c> termap = new Map<Id, term_of_payment__c>{};
        for(Opportunity opp : trigger.new){
              opptIds.add(  opp.id);
              termap.put( opp.id, opp);

        }

       for ( term_of_payment__c term : [select id, term_of_payment__c_Type__c, OpportunityId from term_of_payment__c where OpportunityId in  :opptIds and term_of_payment__c_Type__c = 'General']){


        Opportunity opp = termap.get(term);
        term_of_payment__c c = new term_of_payment__c(LastName = opp.name,
								  OpportunityId=opp.id,			
								  Sequence__c = 1,
								  Amount__c = amountTOD,
								  Document_Date__c = date.parse('02/05/2018') + 30);


        termsload.add(c);

 }
Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com ​ (https://www.forcelearn.com