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
jonathanbernddf20131.387825590468351E12jonathanbernddf20131.387825590468351E12 

After insert,update trigger problem - cannot add 'if' conditions (e.g. field=true or trigger.old != trigger.new)

As a Newbie, I'm still learning, so the answer to there is probably a very simple issue with my code to remedy my problem.

Here is my use case:
When a checkbox is true on a custom master object a new child record is created with 1) a concatenation of values in the 'name' field, 2) Now() in a published date field and 3) obviously the master.id in the child_master__c field.

I have my class (enclosed below) which appears to work with a trigger that has no conditions.
I have my trigger with no conditions below.

I want to add the following conditions to my trigger:
If(Trigger.isInsert && am.Published__c = true) {execute class}  if(Trigger.isUpdate && am.Published__c = true && Trigger.old.am.Published__c != Trigger.new.am.Published__c){execute class}

I was wondering about putting if(am.Published__c = true) into the class, but was concerned that I'd then have too many nested if statements.

Even with my 'working' simple trigger and class, it does not appear possible to delete a record (hoping that will change when I get my conditions).

Class
Public with sharing class InsertVersionNumber {
    
    //Initialize Static Variable
    Public Static Boolean firstcall=True;
           
    Public static void setVersionNumber(List<App_Master__c> appMasts) {
    
    if(!firstcall) {return;}
    else{firstcall = false;}
    
    //Make lists for update
    List<App_Published_Version__c> appPVs = new List<App_Published_Version__c>();

    for(App_Master__c am :appMasts) {

        App_Published_Version__c apv = new App_Published_Version__c();
         if(am.Number_of_Versions__c >= 10) {
         apv.Name = am.Name + '-' + (am.Number_of_Versions__c + 1);
         } else {
         apv.Name = am.Name + '-0' + (am.Number_of_Versions__c + 1);
         }
        apv.Published_Date__c = datetime.Now();
        apv.App_Master__c = am.Id;
        appPVs.add(apv);

    }

    insert appPVs;
   }  
                    
}

Trigger
trigger publishApp on App_Master__c (after insert, after update) {

        for(App_Master__c am :Trigger.New) {
        
               InsertVersionNumber.setVersionNumber(Trigger.New);
          }

}

 
Best Answer chosen by jonathanbernddf20131.387825590468351E12
jonathanbernddf20131.387825590468351E12jonathanbernddf20131.387825590468351E12
Think I solved my own problem thanks

All Answers

Balaji BondarBalaji Bondar
Hi jonathan,

Try below code(Trigger) :
trigger publishApp on App_Master__c (after insert, after update) {
List<App_Master__c> AppMasterList	= new List<App_Master__c>();

	if(Trigger.isInsert){
		for(App_Master__c am :Trigger.New) {
			if(am.Published__c = true)
				AppMasterList.add(am);
		}
	}
	
	if(Trigger.isUpdate){
		for(App_Master__c am :Trigger.New) {
			if(am.Published__c = true && (am.Published__c != Trigger.old.get(am.Id).Published__c))
				AppMasterList.add(am);
		}
	}
	InsertVersionNumber.setVersionNumber(AppMasterList);
}
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
jonathanbernddf20131.387825590468351E12jonathanbernddf20131.387825590468351E12
Hi Bilaji
Unfortunately this doesn't work. Could it be that one issue is that am.Number_of_Versions__c is a roll-up summary? When I tried numerous ways of doing this previously I'd quite often get a 'readonly' exception.
jonathanbernddf20131.387825590468351E12jonathanbernddf20131.387825590468351E12
I solved my first problem by putting the condition for 'Published__c' = true in my class instead of in the trigger.
jonathanbernddf20131.387825590468351E12jonathanbernddf20131.387825590468351E12
Think I solved my own problem thanks
This was selected as the best answer