I need to compare two dates in Months in Salesforce

Opportunity opp Target targ

If (Month(Opp.closeDate)== Month(targ.Target Date)


Add the target Id in Map


And the Id will be Queried in SOQL.This is a before Insert trigger on Opp.

Errors : Method does not exist or Incorrect signature :Void Month(Date) /Method does not exist or incorrect signature : void add(Id)from type map.

Can someone please guide me on how to compare date of Opp with Target.

Many Thanks

Update 1

trigger Updatetraget on Opportunity (after insert,after update) {

Map <Id,Opportunity> mappedopp = new Map <Id,Opportunity>();              {
for (Opportunity Opp : Trigger.new)  

List <Monthly_Sales_Target__c> msales = [SELECT Id, Name,Target_Month_and_Year__c FROM Monthly_Sales_Target__c where Id in :mappedopp.Keyset()];

    for (Opportunity Opp : Trigger.new)  {

    for (Monthly_Sales_Target__c monthsales : msales)
        if(Opp.StageName == 'Closed Won/Completed'&& Opp.CloseDate.Month()==monthsales.Target_Month_and_Year__c.month())
            Opp.Monthly_Sales_Target__c = monthsales.Id;
            update Opp;

} }

1 Answer 1


You should use:

If (Opp.closeDate.Month()== targ.Month()){

  • Thank u so much for the help. Jun 28, 2020 at 10:33
  • Hi @Liron, Can you please tell me what is wrong in below code..There is no error in the code but it still doesnt update the auto lookup field Jun 29, 2020 at 8:00
  • Check the data you are using. Without viewing the full code/component/data it is not possible for me to know. Use debug log to see if the code reach to those lines.
    – Liron C
    Jun 29, 2020 at 10:18

You must log in to answer this question.

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