You need to sign in to do that
Don't have an account?
jonathanbernddf20131.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
Trigger
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); } }
All Answers
Try below code(Trigger) : 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.
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.