Skip to main content
Anush G ha fatto una domanda in #Apex
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
13 risposte
  1. 27 feb 2017, 23:07

    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
0/9000