I want the following trigger to compare opportunity close date month with the custom date field month (Monthly Sales Target and Year). How can I achieve it? Basically I have created a custom object which has a lookup on the opportunity and I want the lookup field to be auto-updated in the opportunity and Custom object should pull out all related opportunities based on the criteria Closed date month. Opportunity = month in the Custom Object. Can you please guide me.
trigger TriggeronTarget on Monthly_Sales_Target__c (before insert, before update) {
Set<id>ids=new set<id>();
list<opportunity>oplst=new list<opportunity>();
For(Monthly_Sales_Target__c trg:trigger.new){
if(trg.Opportunities__c!=Null){
ids.add(trg.Opportunities__c);
}
if(!ids.Isempty())
{
list<opportunity >opp =[select id,name,closeDate from opportunity where Budget_Month__c= Null AND id in:ids ];
for(opportunity op:opp){
for(Monthly_Sales_Target__c trg1: trigger.new){
op.Budget_Month__c =trg1.id;
oplst.add(op);
}
}
update oplst;
}
}