+ Start a Discussion
Sepur AnilSepur Anil 

I need LastActivity < Last_N_Days:90   Soql Query can any one help me.

I have Standard Object and Custom Object 
Standard Object fields are Id, LastactivityDate. Object name is Leads
Custom Object fields are Id, Lead__c. Object Name Email_Lead_Sync__c
I need LastActivity < Last_N_Days:90   Soql Query can any one help me.
Best Answer chosen by Sepur Anil
Ajay K DubediAjay K Dubedi
Hi Sepur,

If you want to do SOQL query using LAST_N_DAYS:90 functinality in a simple way then you can write this:
//It will return all the list of leads having LastActivityDate < LAST_N_DAYS:90
List<Lead> leadList = [Select Id,LastActivityDate  from Lead WHERE LastActivityDate < LAST_N_DAYS:90 Limit 10000];

And if you want to use custom Object(Email_Lead_Sync__c) and standared object(Lead). If Custom object(Email_Lead_Sync__c) have the lookup(relation) with Lead(standared object) in the Lead__c field then you can try this:

List<Lead> leadList = [Select Id,LastActivityDate  from Lead WHERE LastActivityDate < LAST_N_DAYS:90 Limit 10000];
if(leadList.size()>0) {
    Set<Id> leadIdSet = new Set<Id>();
    for (Lead temp : leadList) {
        leadIdSet.add(temp.Id);
    }
//'emailLeadList'- contains all the list of 'email Lead List' having the relation with Lead and LastActivityDate < LAST_N_DAYS:90
    List<Email_Lead_Sync__c> emailLeadList = new List<Email_Lead_Sync__c>();
    emailLeadList = [SELECT Id, Lead__c FROM Email_Lead_Sync__c WHERE Lead__c IN: leadIdSet Limit 10000];
}

If your requirement is anything else then please provide me whole requirement.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

All Answers

Shanu Kumar 18Shanu Kumar 18
Hi Sepur,
I hope this query will solve your problem.
SELECT Id,LastactivityDate From Lead where LastactivityDate <  Last_N_Days:90 LIMIT 50000
SELECT Id, Lead__c From Email_Lead_Sync__c where LastactivityDate <  Last_N_Days:90

Thanks
 
Sepur AnilSepur Anil
Please check with this query.

This is the first query
Select Id, Name, lastActivityDate from Lead where IsConverted = False and LastActivityDate < LAST_90_DAYS

You will get the list of LeadIds from the above query.
You have to pass that LeadIds in below query [where Lead__c in ()].

This is the second qry
Select lead__c,lastmodifieddate,createddate from Email_Lead_Sync__c where Lead__c in () and lastmodifieddate = LAST_90_DAYS

Now you will get the list of LeadIds from the above query

Final list of LeadIds [for update] = First query LeadIds - Second query LeadsId.

For this I need batch class
Ajay K DubediAjay K Dubedi
Hi Sepur,

If you want to do SOQL query using LAST_N_DAYS:90 functinality in a simple way then you can write this:
//It will return all the list of leads having LastActivityDate < LAST_N_DAYS:90
List<Lead> leadList = [Select Id,LastActivityDate  from Lead WHERE LastActivityDate < LAST_N_DAYS:90 Limit 10000];

And if you want to use custom Object(Email_Lead_Sync__c) and standared object(Lead). If Custom object(Email_Lead_Sync__c) have the lookup(relation) with Lead(standared object) in the Lead__c field then you can try this:

List<Lead> leadList = [Select Id,LastActivityDate  from Lead WHERE LastActivityDate < LAST_N_DAYS:90 Limit 10000];
if(leadList.size()>0) {
    Set<Id> leadIdSet = new Set<Id>();
    for (Lead temp : leadList) {
        leadIdSet.add(temp.Id);
    }
//'emailLeadList'- contains all the list of 'email Lead List' having the relation with Lead and LastActivityDate < LAST_N_DAYS:90
    List<Email_Lead_Sync__c> emailLeadList = new List<Email_Lead_Sync__c>();
    emailLeadList = [SELECT Id, Lead__c FROM Email_Lead_Sync__c WHERE Lead__c IN: leadIdSet Limit 10000];
}

If your requirement is anything else then please provide me whole requirement.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer
Sepur AnilSepur Anil
For this I need batch apex 
Sepur AnilSepur Anil
To change ownerid to admin if there is no activity 
and 
Status to remarketing