You need to sign in to do that
Don't have an account?

AND and OR in one 'if' Statement
I have a trigger which is working absolutely fine. It tries to update an opportunity field (Stage) when the Resigned Date is changed and the Fund date is deleted. It is as below:
trigger OpportunityUpdResigned on Account ( after update) {
set<Id> setAccountId = new set<Id>();
for(Account a : Trigger.new){
if(a.Resigned_Date__c != trigger.oldMap.get(a.Id).Resigned_Date__c && a.Fund_Date__c == null)
{
setAccountId.add(a.Id);
}
}
List<Opportunity> lstOpportunity = [SELECT Id, StageName FROM Opportunity WHERE AccountId IN: setAccountId];
for(Opportunity objOpp: lstOpportunity)
{
if(objOpp.StageName == 'Closed Lost'){
objOpp.StageName = 'Installed';
objOpp.Closed_Lost_Reason__c = null;
}
}
update lstOpportunity;
}
Now, there is one more condition to be added. It is to check if the Assets Under Management field is also blank. So, when I am trying to change the if condition like this:
for(Account a : Trigger.new){
if(a.Resigned_Date__c != trigger.oldMap.get(a.Id).Resigned_Date__c && (a.Fund_Date__c == null ||a.Assets_Under_Management__c))
{
setAccountId.add(a.Id);
}
}
it's not helping to achieve the business result.
trigger OpportunityUpdResigned on Account ( after update) {
set<Id> setAccountId = new set<Id>();
for(Account a : Trigger.new){
if(a.Resigned_Date__c != trigger.oldMap.get(a.Id).Resigned_Date__c && a.Fund_Date__c == null)
{
setAccountId.add(a.Id);
}
}
List<Opportunity> lstOpportunity = [SELECT Id, StageName FROM Opportunity WHERE AccountId IN: setAccountId];
for(Opportunity objOpp: lstOpportunity)
{
if(objOpp.StageName == 'Closed Lost'){
objOpp.StageName = 'Installed';
objOpp.Closed_Lost_Reason__c = null;
}
}
update lstOpportunity;
}
Now, there is one more condition to be added. It is to check if the Assets Under Management field is also blank. So, when I am trying to change the if condition like this:
for(Account a : Trigger.new){
if(a.Resigned_Date__c != trigger.oldMap.get(a.Id).Resigned_Date__c && (a.Fund_Date__c == null ||a.Assets_Under_Management__c))
{
setAccountId.add(a.Id);
}
}
it's not helping to achieve the business result.
you appear to have missed the test for null on the Assets under management field
Regards
Andrew
Only suggestion for the minute is to break the test into two or three and test each element individually to determine which is not allowing the whole to execute properly - running debug logs in the back end and then
because looking at it, it appears fine. My only thought is that one of the individual tests (perhaps the new one) is not evaluating correctly.
Regards
Andrew
trigger OpportunityUpdResigned on Account ( after update) {
set<Id> setAccountId = new set<Id>();
for(Account a : Trigger.new){
if(a.Resigned_Date__c != trigger.oldMap.get(a.Id).Resigned_Date__c )
{
if (a.Fund_Date__c == null ||a.Assets_Under_Management__c ==null){
setAccountId.add(a.Id);
}
}
}
List<Opportunity> lstOpportunity = [SELECT Id, StageName FROM Opportunity WHERE AccountId IN: setAccountId];
for(Opportunity objOpp: lstOpportunity)
{
if(objOpp.StageName == 'Closed Lost'){
objOpp.StageName = 'Installed';
objOpp.Closed_Lost_Reason__c = null;
}
}
update lstOpportunity;