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
pavan kumar 177pavan kumar 177 

Unable to calculate days in apex

I need to calculate days between two fields.

So I use daysBetween to complete when I try to do with two dates it's working fine.

But, when I try with list(or) map  it throws an error like:

Method does not exist or incorrect signature: [Date].daysBetween(List)

For reference my code:

public date closedates;
Public Long details;
List<Case> c=new List<case>();
List<Integer> Values=new List<Integer>();
//Integer Value=0;
List<Date> Churndate=new List<Date>();
List<Date> closedatesd=new List<Date>();
c=[Select Id,Date_Order_Received__c,accountId from case where RecordTypeId = '01290000000sF3L'];
System.debug('Data in Case '+c);
List<Id> accountIds= new List<Id>();
List<Account> accounts=new List<Account>();
List<Id> opps=new List<Id>();
List<Date> Oppsdate= new List<Date>();
for(Case cl:c) {
accountIds.add(cl.accountId);
//Churndate=cl.Date_Order_Received__c;
Churndate.add(cl.Date_Order_Received__c);
}
System.debug('Date value in '+churndate);
for(Account ao:[Select Id,name,(select Id,name,CloseDate from opportunities) from account where Id in:accountIds]) {
for(Opportunity opp : ao.opportunities) {
opps.add(opp.Id);
//details=closedates.daysBetween(Churndate);
**Values.add(opp.CloseDate.daysBetween(Churndate));**
//Other Operation you want to perform with every opportunity
}
}
JeffreyStevensJeffreyStevens
Churndate is a list of dates. The daysBetween method has to operate on a single date. 
pavan kumar 177pavan kumar 177
Hey you are right.But my requirement is to calculate a date field in case with close date in opportunity.But for a account lot of opportunities are avilable.So,Please can you tell me solution to resolve this issue. @JeffreyStevens 
JeffreyStevensJeffreyStevens
So, what starts this process?  A specific case?  Or - are you doing this for all cases, and all accounts on those cases, and all opportunities on those acocunts?  (That's about what your code is doing now).
pavan kumar 177pavan kumar 177
I wrote this code in anonymous block for testing puropose.Actually my requirement is to wrote a trigger. And my requirement is case with record type customer order have a date called Date_Order_Received__c.I want to know the differentiate between this date and closedate in opportunity.So,from cases i get account Id-->opportunityId(Closedate)-->.I want to know number.If difference is 180 days i want to update a field in that particular opportunity.So,for account multiple opportunities are there.So i stucked in this place to handle bulk.Please help me @JeffreyStevens.If you want to know anything
JeffreyStevensJeffreyStevens
Okay - so you've got a bit of a architectural problem.  In order to do it correctly - you would need a one-to-one relationship on the Case to Opportunity - Correct?  But you don't have relationship - right?  So  you've got some choices...
1) Create (and somehow maintain) the relationship from the Case to the Opportunity
2) Get to AN Opportunity through the account.

 
JeffreyStevensJeffreyStevens
If you chose #2 - you have to choose - the first opportunity associated with the account, ALL opportunities, the LAST opportunity - or another type of criteria. 

You might try some code close to this.  (I didn't do your selects on the exact case, etc) But this should give you a concept of how to do it.
 
// Loop through Cases & get Account IDs, building map
map<id,Case> mAccountIdCase = new map<id,Case>();
For(Case c :[SELECT id,Date_Order_Received__c,AccountId FROM Case  ]) {
    mAccountIdCase.put(c.AccountId,c);
}

// Get Account & opportunites
list<Account> accounts = new list<Account>([SELECT id,
                                            		(SELECT id,CloseDate FROM Opportunities) 
                                            FROM Account 
                                            WHERE id IN :mAccountIdCase.keyset()]);

// Build Opportunites to update if needed
list<Opportunity> opportunitiesToUpdate = new list<Opportunity>();
For(Account a :Accounts) {
    for(Opportunity o :a.opportunities) {
        if(mAccountIdCase.containsKey(a.id)) {
            integer checkDays = 0;
            checkDays = o.closeDate.daysBetween(mAccountIdCase.get(a.id).Date_Order_Received__c);
            if(checkDays > 180) {
                Opportunity iterOppty = new Opportunity();
                iterOppty = o;
                iterOppty.Past180Flag__c = true;
                opportunitesToUpdate.add(iterOppty);
            }
        }
    }
}

if(opportunitesToUpdate.size()>0) {
    update oportunitesToUpdate;
}

 
pavan kumar 177pavan kumar 177
I have newly joined in my organisation.Till now there is no relationship between cases & opportunity.So, i had already proposed the lookup relationship to opportunity.But they didn't accept the solution.So i started writing apex coding i drill down up to opportunities.But i stuck in a place calculating difference in date.So,I tried so many solutions to complete.@JeffreyStevens
  
pavan kumar 177pavan kumar 177
Thanks atleast you showed me a concept to handle it.I will work on this if i face any issues i will let you know.once again thanks for sharing knowledge begineers like me.@JeffreyStevens
pavan kumar 177pavan kumar 177
hey please i have to create any boolean variable Past180Flag__c in opportunity
@JeffreyStevens