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
- Within a specific day range: Last 14 days/Last month
- 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:
- User who updates the Opportunity, i.e., Data Load User
- Last Modified date of the Opportunity record
- If there is no Contact Role existed on the Primary Contact.