0

Requirement: Here I have Contacts, each of them is may/may not self-related to another Contact. Terminology: Primary Contact and Secondary Contact. Here Primary Contact is a self lookup field on the Contact object.

I am trying to push(using Upsert) some Opportunity records using data loader. For the newly created/updated Opportunity, if there is an OpportunityContactRole with a Secondary Contact(one which has a lookup to another Contact), check if there an OpportunityContactRole for the Primary Contact in regards to that Opportunity. If there is none, Create a new OpportunityContactRole for the Primary Contact.

Question: Things I am trying to achieve:

Opportunity records that are

  1. Within a specific day range: Last 14 days/Last month
  2. Last Modified/Created by a specific User(through out all dev orgs including Prod).

This is the query I had so far:

 Map<Id, Contact> relContIdsMap = new Map<Id, Contact>(secondaryCons);
 List<Opportunity> relatedOpps = [Select (Select OpportunityId From OpportunityContactRoles), Name 
                                From Opportunity Where Id IN 
                                (Select OpportunityId From OpportunityContactRole 
                                Where ContactId =: relContIdsMap.keySet())
                                AND (Opportunity.LastModifiedBy.LastName =: 'Data Load User' OR Opportunity.CreatedBy.LastName =: 'Data Load User'  )];
  System.debug('newOpps: ' +relatedOpps); //Returns the Opportunities of the Secondary contacts. 

How can I get the day range requirement full-filled? Any suggestions are welcome.

Background: I am trying to implement a Batch class which inserts new Contact Roles on the Opportunity based on:

  1. User who updates the Opportunity, i.e., Data Load User
  2. Last Modified date of the Opportunity record
  3. If there is no Contact Role existed on the Primary Contact.

1 Answer 1

1

To fulfill the date range of Last 15 days or last month, use Date Literals LAST_N_DAYS:n or LAST_MONTH

Map<Id, Contact> relContIdsMap = new Map<Id, Contact>(secondaryCons);
 List<Opportunity> relatedOpps = [Select (Select OpportunityId From OpportunityContactRoles), Name 
                                From Opportunity Where Id IN 
                                (Select OpportunityId From OpportunityContactRole 
                                Where ContactId =: relContIdsMap.keySet())
                                AND (Opportunity.LastModifiedBy.LastName =: 'Data Load User' 
                                OR Opportunity.CreatedBy.LastName =: 'Data Load User' )
                                AND CreatedDate >= LAST_N_DAYS:15];

Also note that, this approach is good if you are running the batch with either 15 days or monthly interval respectively, so that same records will not be pulled twice.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .