You need to sign in to do that
Don't have an account?
pvande
Need help with IF clause on trigger
Please help me add a line of code to my trigger. I don't want the trigger to run if Date_Pushed_to_Production__c is populated. This field resides on the custom object Project__c. Thank you for looking at my question.
Here is the trigger:
Trigger MoveToProduction on Opportunity (before insert, before update){
List<ID> ProjIds = New List<ID>();
for(Opportunity o : Trigger.new){
if(o.Move_to_Production__c == true && o.RecordTypeId == '012a0000001FqTn'){
ProjIds.add(o.Project__c);
}
List<Project__c> ProjList = [SELECT id, Move_to_ProductionP__c, Date_Pushed_to_Production__c FROM Project__c WHERE id in :ProjIds];
for(integer i = 0 ; i < ProjList.size(); i++){
If(ProjList[i].Date_Pushed_to_Production__c == null)
ProjList[i].Move_to_ProductionP__c = true;
ProjList[i]. Date_Pushed_to_Production__c = system.today();
update ProjList;
Here is the trigger:
Trigger MoveToProduction on Opportunity (before insert, before update){
List<ID> ProjIds = New List<ID>();
for(Opportunity o : Trigger.new){
if(o.Move_to_Production__c == true && o.RecordTypeId == '012a0000001FqTn'){
ProjIds.add(o.Project__c);
}
List<Project__c> ProjList = [SELECT id, Move_to_ProductionP__c, Date_Pushed_to_Production__c FROM Project__c WHERE id in :ProjIds];
for(integer i = 0 ; i < ProjList.size(); i++){
If(ProjList[i].Date_Pushed_to_Production__c == null)
ProjList[i].Move_to_ProductionP__c = true;
ProjList[i]. Date_Pushed_to_Production__c = system.today();
update ProjList;
<pre>
// first, move to after insert, after update, since we are not modifying the Opportunity object
trigger MoveToProduction on Opportunity (after insert, after update)
{
// query record type so we don't have to hard-code RecordType IDs
RecordType oppRT =
[ SELECT Id
FROM RecordType
WHERE sObjectType = 'Opportunity' AND DeveloperName = 'Desired_Record_Type'
];
// use a Set because multiple Opportunities could look up to the same Project
Set<Id> projIds = new Set<Id>();
for ( Opportunity opp : Trigger.new )
{
// get the old version of the record - null if insert
Opportunity oldOpp = Trigger.isUpdate ? Trigger.oldMap.get( opp.Id ) : null;
if ( opp.Move_to_Production__c // it's boolean - no need to compare to true
&& (oldOpp == null || !oldOpp.Move_to_Production__c)
&& opp.RecordTypeId == oppRt.Id
)
{
projIds.add( opp.Project__c );
}
}
if ( !prjIds.isEmpty() )
{
// exclude Projects with non-null Date_Pushed_to_Production__c
// since we're not going to update those
List<Project__c> projList =
[ SELECT Id, Move_to_ProductionP__c, Date_Pushed_to_Production__c
FROM Project__c
WHERE Id in :projIds AND Date_Pushed_to_Production__c = null
];
for ( Project__c proj : projList )
{
proj.Move_to_ProductionP__c = true;
proj.Date_Pushed_to_Production__c = System.today();
}
update projList;
}
}
</pre>
All Answers
You are already accounting for it to not run at the end on the
" If(ProjList[i].Date_Pushed_to_Production__c == null)",
If you don't want any of it to run, it would require a little bit more changing of the total code as seen below.
What I am doing here is checking if the opporutnitys "Move_To_Production__c" went from false to true on the update, instead of always causing it to add to the list if it was ever true.
Then I only execute the bottom update onto the Project__c if the list is not empty.
<pre>
// first, move to after insert, after update, since we are not modifying the Opportunity object
trigger MoveToProduction on Opportunity (after insert, after update)
{
// query record type so we don't have to hard-code RecordType IDs
RecordType oppRT =
[ SELECT Id
FROM RecordType
WHERE sObjectType = 'Opportunity' AND DeveloperName = 'Desired_Record_Type'
];
// use a Set because multiple Opportunities could look up to the same Project
Set<Id> projIds = new Set<Id>();
for ( Opportunity opp : Trigger.new )
{
// get the old version of the record - null if insert
Opportunity oldOpp = Trigger.isUpdate ? Trigger.oldMap.get( opp.Id ) : null;
if ( opp.Move_to_Production__c // it's boolean - no need to compare to true
&& (oldOpp == null || !oldOpp.Move_to_Production__c)
&& opp.RecordTypeId == oppRt.Id
)
{
projIds.add( opp.Project__c );
}
}
if ( !prjIds.isEmpty() )
{
// exclude Projects with non-null Date_Pushed_to_Production__c
// since we're not going to update those
List<Project__c> projList =
[ SELECT Id, Move_to_ProductionP__c, Date_Pushed_to_Production__c
FROM Project__c
WHERE Id in :projIds AND Date_Pushed_to_Production__c = null
];
for ( Project__c proj : projList )
{
proj.Move_to_ProductionP__c = true;
proj.Date_Pushed_to_Production__c = System.today();
}
update projList;
}
}
</pre>