function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AnuShAnuSh 

Convert Email to case related to recent opportunity

Subject: I would like to convert Email to case related to recent opportunity ( i have written the code email to case related to opportunity but I would like to assign recent opportunity)

For your reference, i have attached the code

Drawback: My cases assigned to  All related opportunities 

Code:
trigger CasetoAccountInsert on Case (before insert) {

    for(Case record: Trigger.new)
    {
        List<Opportunity> OpportunityList = [SELECT AccountId, Name FROM Opportunity WHERE Contact_Email__c = :record.SuppliedEmail] ;
        if (OpportunityList.size() > 0)
        {
            record.AccountId  = OpportunityList[0].AccountId;                            
        }
    } 
    
}



Thanks in Advance
AnuSh
Deepak GulianDeepak Gulian
Try to use this SOQL
[SELECT AccountId, Name FROM Opportunity WHERE Contact_Email__c = :record.SuppliedEmail  ORDER BY CreatedDate DESC Limit 1]

 
AnuShAnuSh
Hi Deepak,

I was trying this code, it was updated in all opportunities, is there any way that i can add case to recent opportunity instead of all opportunities.

Regards
Anush
Deepak GulianDeepak Gulian
trigger CasetoAccountInsert on Case (before insert) {

     set<String> emailSet = new set<String>();
     map<String,Id> mapOpp = new map<String,Id>();
     map<String,Id> mapAcc = new map<String,Id>();
     
     for(Case record: Trigger.new) {
         emailSet.add(record.SuppliedEmail);
     } 
    
     for(Opportunity opp : [SELECT Id, AccountId, Contact_Email__c FROM Opportunity WHERE Contact_Email__c IN:emailSet ORDER BY CreatedDate DESC]){
        if(mapOpp.get(opp.Contact_Email__c) == null){
            mapOpp.put(opp.Contact_Email__c, opp.Id); 
            mapAcc.put(opp.Contact_Email__c, opp.AccountId); 
        }
    }
   
   for(Case cs : Trigger.new){
       cs.<OpportunityLookUpCustomField> = mapOpp.get(cs.SuppliedEmail);
       cs.AccountId = mapAcc.get(cs.SuppliedEmail);
   }

  //Please replace <OpportunityLookUpCustomField> with this custom Opportunity Lookup Field API Name to link case with the Opportunity.

}

Try this now and let me know if it works!
AnuShAnuSh
Hi Deepak,
I'm getting error message 

Here is the errorr message show below :

Line :              cs.<Related_Opportunity__c> = mapOpp.get(cs.SuppliedEmail);
problem :  Unexpected token :'='



For your reference code and case object screen shot was attached.

--------------
trigger Email_to_case on Case (before insert) {

       set<String> emailSet = new set<String>();

     map<String,Id> mapOpp = new map<String,Id>();

     map<String,Id> mapAcc = new map<String,Id>();
    
     for(Case record: Trigger.new) {

         emailSet.add(record.SuppliedEmail);
     }
     for(Opportunity opp : [SELECT Id, AccountId, Contact_Email__c FROM Opportunity WHERE Contact_Email__c IN:emailSet ORDER BY closedate DESC])
     {
        if(mapOpp.get(opp.Contact_Email__c) == null){

            mapOpp.put(opp.Contact_Email__c, opp.Id);

            mapAcc.put(opp.Contact_Email__c, opp.AccountId);

        }
    }
    for(Case cs : Trigger.new){

             cs.<Related_Opportunity__c> = mapOpp.get(cs.SuppliedEmail);

       cs.AccountId = mapAcc.get(cs.SuppliedEmail);
   }
  //Please replace <OpportunityLookUpCustomField> with this custom Opportunity Lookup Field API Name to link case with the Opportunity.
}



Case object
Deepak GulianDeepak Gulian

You are not suppose to use these > or < 

So code should be  cs.Related_Opportunity__c = mapOpp.get(cs.SuppliedEmail);

AnuShAnuSh
Hi Deepak, 
Thanks for the reply

however, still, it is reflecting in all opportunities. 

 
Deepak GulianDeepak Gulian

You changed the query to Order By close date so change it back to the original ORDER BY CreatedDate

So Query should be [SELECT Id, AccountId, Contact_Email__c FROM Opportunity WHERE Contact_Email__c IN:emailSet ORDER BY CreatedDate DESC]

Test it and let me know!

AnuShAnuSh
Hi Deepak,
Still Same


Here is the code
trigger Email_to_case on Case (before insert) {
    
       set<String> emailSet = new set<String>();

     map<String,Id> mapOpp = new map<String,Id>();

     map<String,Id> mapAcc = new map<String,Id>();
    
     for(Case record: Trigger.new) {

         emailSet.add(record.SuppliedEmail);
     }
     for(Opportunity opp : [SELECT Id, AccountId, Contact_Email__c FROM Opportunity WHERE Contact_Email__c IN:emailSet ORDER BY createddate DESC Limit 1])
     {
        if(mapOpp.get(opp.Contact_Email__c) == null){

            mapOpp.put(opp.Contact_Email__c, opp.Id);

            mapAcc.put(opp.Contact_Email__c, opp.AccountId);

        }
    }
    for(Case cs : Trigger.new){

             cs.Related_Opportunity__c = mapOpp.get(cs.SuppliedEmail);

       cs.AccountId = mapAcc.get(cs.SuppliedEmail);
   }


}
Deepak GulianDeepak Gulian
trigger CasetoAccountInsert on Case (before insert) {

     set<String> emailSet = new set<String>();
     map<String,Id> mapOpp = new map<String,Id>();
     map<String,Id> mapAcc = new map<String,Id>();
     
     for(Case record: Trigger.new) {
         emailSet.add(record.SuppliedEmail);
     } 
    
     for(Opportunity opp : [SELECT Id, AccountId, Contact_Email__c FROM Opportunity WHERE Contact_Email__c IN:emailSet ORDER BY CreatedDate DESC]){
        if(mapOpp.get(opp.Contact_Email__c) == null){
            mapOpp.put(opp.Contact_Email__c, opp.Id); 
            mapAcc.put(opp.Contact_Email__c, opp.AccountId); 
        }
    }
   
   for(Case cs : Trigger.new){
       cs.Related_Opportunity__c = mapOpp.get(cs.SuppliedEmail);
       cs.AccountId = mapAcc.get(cs.SuppliedEmail);
   }

}
I notice that you added Limit 1 in query.
Please don't modify any piece of code above, else it will not work and just paste this code and try. If it still not work let me know
AnuShAnuSh
Hi Deepak,

Now i was removed limit 1


This is the actual code:


trigger Email_to_case on Case (before insert) {
    
       set<String> emailSet = new set<String>();

     map<String,Id> mapOpp = new map<String,Id>();

     map<String,Id> mapAcc = new map<String,Id>();
    
     for(Case record: Trigger.new) {

         emailSet.add(record.SuppliedEmail);
     }
     for(Opportunity opp : [SELECT Id, AccountId, Contact_Email__c FROM Opportunity WHERE Contact_Email__c IN:emailSet ORDER BY createddate DESC])
     {
        if(mapOpp.get(opp.Contact_Email__c) == null){

            mapOpp.put(opp.Contact_Email__c, opp.Id);

            mapAcc.put(opp.Contact_Email__c, opp.AccountId);

        }
    }
    for(Case cs : Trigger.new){

             cs.Related_Opportunity__c = mapOpp.get(cs.SuppliedEmail);

       cs.AccountId = mapAcc.get(cs.SuppliedEmail);
   }

  //Please replace <OpportunityLookUpCustomField> with this custom Opportunity Lookup Field API Name to link case with the Opportunity.
}

 
AnuShAnuSh
Hi Deepak,

I forgot to mention, still same it is reflecting in all opportunities 
Deepak GulianDeepak Gulian
I'm running the same code at my end and its working fine. Will it be possible for you to connect through skype and let's figure it out how exactly its working at your end.
AnuShAnuSh
Hi deepak,

Kedharinath is my skype id
 205583209 is my team viewer id

Ping me now, im on skype