You need to sign in to do that
Don't have an account?
Arief 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; } }
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.
Mark as best answer, If it resloves !! Mark as resloved if it helps :) :)
Thanks,
Bhanu Prakash
visit ForceLearn.com (https://www.forcelearn.com)