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
Craig PhoenixCraig Phoenix 

Create Case On Opportunity Field Values

I am struggling with building a trigger to create a case when Opportunity is Closed with Setup Needed = Yes (Picklist) and still trying to learn as I run with Apex.

Opportunity fields Stage=Closed Won, Type=Upgrade and Setup Needed=Yes once all 3 of these fields are set to these values I need it to create a Case with Subject containing Opportunity Name and Account Name, also link to Account that is attached to the Opportunity then dump a bunch of the Opportunity fields into the Description field for example:
Customer: {!Account.Name}
AccountNumber: {!Account.AccountNumber__c}
Sold By: {!Opportunity.OwnerFullName}

I had it setup (since minimal coding experience) where it used a workflow to send an email which looped back into SF with Email-to-Case but thats not working 100% so I believe I need to move it into SF directly.

If anyone can help with building the trigger to create the case with field data from the Opp passed into the case that would be great help!
Best Answer chosen by Craig Phoenix
DaspereraDasperera
Hi Craig,

I hope the following trigger would work for you. 
trigger CaseInvokeWithOpp on Opportunity (after insert, after update) {
	 
    List<Opportunity> oppLst = Trigger.New;
    
    //list for account Ids
    List<Id> AccIds = new List<Id>();
    //list for owner Ids
    List<Id> ownerIds = new List<Id>();
    
    //list to create new cases
    List<Case> newCases = new List<Case>();
    
    //Map to set account details related opportunity
    Map<Id, String> accountInfo = new Map<Id, String>();
	Map<Id, String> accountAdditionalInfo = new Map<Id, String>(); 
    
    //Map to set owner's first name and the last name
    Map<Id, String> ownerFirstName = new Map<Id, String>();
    Map<Id, String> ownerLastName = new Map<Id, String>();
   
    
    //Get account Id and owner Id from opportunities
    for(Opportunity opp : oppLst)
    {
        AccIds.add(opp.AccountId);
        ownerIds.add(opp.OwnerId);
    }
    
    List<Account> relatedAcc = [SELECT Name, AccountNumber FROM Account WHERE Id IN: AccIds];
    List<User> relatedUserLst = [SELECT FirstName, LastName FROM User WHERE Id IN: ownerIds];
    
    //Set account name and account number
    for(Account ac : relatedAcc){
        accountInfo.put(ac.Id, ac.Name);
        accountAdditionalInfo.put(ac.Id, ac.AccountNumber);
    }
    
    //set owner first name and the last name
    for(User usr: relatedUserLst){
        
        ownerFirstName.put(usr.Id, usr.FirstName);
    	ownerLastName.put(usr.Id, usr.LastName);
    }
        
    for(Opportunity opp : oppLst)
    {
        //Check whether the opportunity's Stage=Closed Won, Type=Upgrade and Setup Needed=Yes
        if(opp.StageName == 'Closed Won' && opp.Type == 'Upgrade' && opp.Setup_Needed__c == true){
            
            case newcase = new case();
            newcase.AccountId = opp.AccountId;
            //Case with Subject containing Opportunity Name and Account Name
            newcase.Subject = opp.Name + ' ' + accountInfo.get(opp.AccountId);
            //Customer: {!Account.Name}
            newcase.Customer__c = accountInfo.get(opp.AccountId);
            //AccountNumber: {!Account.AccountNumber__c}
            newcase.Account_Number__c = accountAdditionalInfo.get(opp.AccountId);
            //Sold By: {!Opportunity.OwnerFullName}
            newcase.Sold_By__c = ownerFirstName.get(opp.OwnerId) + ' '+ ownerLastName.get(opp.OwnerId);
            newcase.Status = 'New';
            newCases.add(newcase);
        }
        
        insert newCases;
    }
}

I also tested this scenario in one of my developer org. So once you updated the Opportunity stage as "Closed Won" of an opportunity in which type is "Upgrade" and Setup Needed is set to true. This trigger should work.
Following is the input opportunity that I have been used to test this scenario.
Opportunity used to test the scenario

Once it was updated, a case is created as follows.
Case created upon updating the opportunity

All Answers

DaspereraDasperera
Hi Craig,

I hope the following trigger would work for you. 
trigger CaseInvokeWithOpp on Opportunity (after insert, after update) {
	 
    List<Opportunity> oppLst = Trigger.New;
    
    //list for account Ids
    List<Id> AccIds = new List<Id>();
    //list for owner Ids
    List<Id> ownerIds = new List<Id>();
    
    //list to create new cases
    List<Case> newCases = new List<Case>();
    
    //Map to set account details related opportunity
    Map<Id, String> accountInfo = new Map<Id, String>();
	Map<Id, String> accountAdditionalInfo = new Map<Id, String>(); 
    
    //Map to set owner's first name and the last name
    Map<Id, String> ownerFirstName = new Map<Id, String>();
    Map<Id, String> ownerLastName = new Map<Id, String>();
   
    
    //Get account Id and owner Id from opportunities
    for(Opportunity opp : oppLst)
    {
        AccIds.add(opp.AccountId);
        ownerIds.add(opp.OwnerId);
    }
    
    List<Account> relatedAcc = [SELECT Name, AccountNumber FROM Account WHERE Id IN: AccIds];
    List<User> relatedUserLst = [SELECT FirstName, LastName FROM User WHERE Id IN: ownerIds];
    
    //Set account name and account number
    for(Account ac : relatedAcc){
        accountInfo.put(ac.Id, ac.Name);
        accountAdditionalInfo.put(ac.Id, ac.AccountNumber);
    }
    
    //set owner first name and the last name
    for(User usr: relatedUserLst){
        
        ownerFirstName.put(usr.Id, usr.FirstName);
    	ownerLastName.put(usr.Id, usr.LastName);
    }
        
    for(Opportunity opp : oppLst)
    {
        //Check whether the opportunity's Stage=Closed Won, Type=Upgrade and Setup Needed=Yes
        if(opp.StageName == 'Closed Won' && opp.Type == 'Upgrade' && opp.Setup_Needed__c == true){
            
            case newcase = new case();
            newcase.AccountId = opp.AccountId;
            //Case with Subject containing Opportunity Name and Account Name
            newcase.Subject = opp.Name + ' ' + accountInfo.get(opp.AccountId);
            //Customer: {!Account.Name}
            newcase.Customer__c = accountInfo.get(opp.AccountId);
            //AccountNumber: {!Account.AccountNumber__c}
            newcase.Account_Number__c = accountAdditionalInfo.get(opp.AccountId);
            //Sold By: {!Opportunity.OwnerFullName}
            newcase.Sold_By__c = ownerFirstName.get(opp.OwnerId) + ' '+ ownerLastName.get(opp.OwnerId);
            newcase.Status = 'New';
            newCases.add(newcase);
        }
        
        insert newCases;
    }
}

I also tested this scenario in one of my developer org. So once you updated the Opportunity stage as "Closed Won" of an opportunity in which type is "Upgrade" and Setup Needed is set to true. This trigger should work.
Following is the input opportunity that I have been used to test this scenario.
Opportunity used to test the scenario

Once it was updated, a case is created as follows.
Case created upon updating the opportunity
This was selected as the best answer
Craig PhoenixCraig Phoenix
This worked great, now I need to do code coverage so any help with code coverage I totally appreciate! so I can deploy :)