You need to sign in to do that
Don't have an account?
AnuSh
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
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
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
Try this now and let me know if it works!
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.
}
You are not suppose to use these > or <
So code should be cs.Related_Opportunity__c = mapOpp.get(cs.SuppliedEmail);
Thanks for the reply
however, still, it is reflecting in all opportunities.
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!
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);
}
}
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
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.
}
I forgot to mention, still same it is reflecting in all opportunities
Kedharinath is my skype id
205583209 is my team viewer id
Ping me now, im on skype