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

Trigger - date.daysBetween method not working after API insert but works fine after update
We're having an issue here. Here is my code. Everything works fine "after update" but the portion portion where it involves daysBetween calculation is not working "after insert" (API).
trigger LeadReAssigment on Opportunity (after insert, after update) { // reference to static class to prevent from trigger firing once List<Opportunity> ops= [SELECT Id, StageName, OwnerId, Date_Begun__c, AccountId FROM Opportunity WHERE Id = :Trigger.New[0].Id]; //list<Opportunity> ops= trigger.new; integer DaysOld = 0; for(Opportunity o: ops) { string OriginalOwnerId = o.OwnerId; Date StartDate = System.Today(); Date EndDate = o.Date_Begun__c; DaysOld = EndDate.daysBetween(StartDate); System.debug('Days Diff:'+DaysOld); //System.debug(o.StageName+' Records:'+ops.size()+' OWNER ID'+o.OwnerId+' Id: '+ o.Id); if(o.StageName == 'Funded'){ o.OwnerId = 'bbb'; }else if(o.StageName == 'Rejected'){ o.OwnerId = 'ccc'; }else if(o.StageName == 'Closed'){ o.OwnerId = 'yyy'; }else if(o.StageName == 'Pending' && DaysOld > 60){ o.OwnerId = 'xxx'; }else if(o.StageName == 'Opened' && DaysOld > 60){ o.OwnerId = 'vvv'; } System.debug('AccountId: '+o.AccountId+' Original Owner'+OriginalOwnerId+' New Owner:'+o.OwnerId); if(OriginalOwnerId != o.OwnerId){ //update Account and Contact List<Account> acc= [SELECT Id FROM Account WHERE Id = :o.AccountId]; for(Account o3: acc) { o3.OwnerId = o.OwnerId; } if(acc.size() > 0){ update acc; } List<Contact> cont= [SELECT Id FROM Contact WHERE AccountId = :o.AccountId]; for(Contact o4: cont) { o4.OwnerId = o.OwnerId; } if(cont.size() > 0){ update cont; } } } if(counter > 0){ System.debug('UPDATE DONE'); update ops; } //} }
May I know why you think the after insert is not working however the after update is working? Is there some error you hit when you try to insert the record?
I just think that this line is abit confusing at line 17 Because this will always return the nagetive value. May I suggest to change to: