Below is my trigger to create a new record(with next quarter) when Hold and freeze are set to true.

But this works for new but not update.

below is my trigger and .

trigger AddingNewQuarter on Flag__c (after insert,after update) 
if(trigger.isInsert && trigger.isAfter)
AddingNewQuarter.createRecord(trigger.new, trigger.newMap);

Handler class

public class AddingNewQuarter{
public static void createRecord(List<Flag__c> oclist,Map<Id, Flag__c> newMap){
    oclist= [SELECT Id, Name FROM Flag__c WHERE Id IN :newMap.keySet()];
    for(Flag__c mdmocc:oclist){
      string  name   =   [select Name from Flag__c order by createddate desc limit 1][0].Name;
        boolean hold   =   [select Hold__c from Flag__c order by createddate desc limit 1][0].Hold__c;
        boolean freeze =   [select Freeze__C from Flag__c order by createddate desc limit 1][0].Freeze__C;
        integer quarter = 0;
        integer Year = 0;   if(integer.valueof(name.substring(5,6)) == 4)
quarter = 1;
Year = integer.valueof(name.substring(0,4)) + 1; }  else{
  quarter = integer.valueof(name.substring(5,6)) + 1;
  Year= integer.valueof(name.substring(0,4));}string nextquarter =  Year + 'Q' + quarter;
if([select Name from Flag__c where Name = :nextquarter] == null)
    if(hold && freeze == true)
        Flag__c mof = new Flag__c ();     
        mof.Name = nextquarter; 
        mof.Hold__c = false ;
        mof.Freeze__c  = false ;
            insert mof;
  • 4
    This code needs more help than "doesn't work." You need to fix a lot of problems here. – sfdcfox Jun 19 '18 at 15:32
  • @sfdcfox can you please elaborate?? – Doondi Jun 20 '18 at 5:29
  • Queries inside loops, unnecessary queries, code formatting issues, an inverted if statement (why not just say != null instead of using else?), what looks like maybe a DML statement in a loop? Your code is not maintainable as written. – sfdcfox Jun 20 '18 at 12:00
  • @sfdcfox Thanks a ton!! for your feedback.I was grown up by seeing your answers, I felt happy to see your answer on my question. Once again thanks for educating with patience. – Doondi Jun 20 '18 at 12:25

Just by checking the code on the trigger, it works only on after insert: (if(trigger.isInsert && trigger.isAfter)).

If you want to work for both cases (on after insert and after update), you have to make just:

       AddingNewQuarter.createRecord(Trigger.new, Trigger.newMap);
  • why Trigger.isInsert is not required? – Doondi Jun 20 '18 at 5:58
  • @Doondi if you want the trigger to work for both cases, on after insert and after update, that's all needed because your trigger has only insert and update events, as your code is: trigger AddingNewQuarter on Flag__c (after insert,after update) – Enrik Hysko Jun 20 '18 at 8:15
  • @Doondi you're welcome. Please check the correct answer and/or update the question accordingly to benefit as a future reference for others. Thanks. – Enrik Hysko Jun 20 '18 at 12:39
  • it's solving the timely purpose, but it doesn't stand strong enough to get code coverage. that's why I have left it as it is. – Doondi Jun 22 '18 at 9:00
  • @Doondi your question doesn't imply that. You've got an answer for what you asked your problem was. – Enrik Hysko Jun 22 '18 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.