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
sundhar.mks1.3962649227519546E12sundhar.mks1.3962649227519546E12 

How to create new opportunity? when a Case is Closed.

Hi,
Case object have picklist field of  Stage = Closed "Stage is a picklist field"
 
Best Answer chosen by sundhar.mks1.3962649227519546E12
MithunPMithunP
Hi Sundhar,

Below is the code.
trigger oppCase on Case (after insert, after update) {
    list<opportunity> opplist = new list<opportunity>();
    for(case cs : trigger.new){
          if(cs.status == 'Closed'){
               opportunity opp = new opportunity();
               opp.accountid = cs.accountid;
               opp.name = 'New Opportunity -' + cs.casenumber;
               opp.stageName = 'Prospecting';
               opp.closedate = system.today();
               opplist.add(opp);
          }
    }
    if(opplist.size() > 0){
       insert opplist;
    }

}



 Best Regrads,
Mithun.

All Answers

MithunPMithunP
Hi Sundhar,

Below is the code.
trigger oppCase on Case (after insert, after update) {
    list<opportunity> opplist = new list<opportunity>();
    for(case cs : trigger.new){
          if(cs.status == 'Closed'){
               opportunity opp = new opportunity();
               opp.accountid = cs.accountid;
               opp.name = 'New Opportunity -' + cs.casenumber;
               opp.stageName = 'Prospecting';
               opp.closedate = system.today();
               opplist.add(opp);
          }
    }
    if(opplist.size() > 0){
       insert opplist;
    }

}



 Best Regrads,
Mithun.
This was selected as the best answer
sundhar.mks1.3962649227519546E12sundhar.mks1.3962649227519546E12
Hi Mithun,

It's working Perfect.Many Thanks..!

How to write the test class for this? Kindly help me
MithunPMithunP
Hi Sundhar,

Here is test class
@istest(seealldata = true)
public class TrgTestClass{

    public static testmethod void main(){
        account acc = new account(name = 'test acc for case');
        insert acc;
        case cs = new case (accountid = acc.id, status = 'closed', origin = 'Phone');
        insert cs;
    }
}
Mark this as Best Answer, if it solves your proble.

Best Regards,
Mithun.
sundhar.mks1.3962649227519546E12sundhar.mks1.3962649227519546E12
Sorry i did't get got some error. Kindly give the following code test class please
============================
Public class CasetriggerHandler{

    Public void OnAfterInsert(map<id,Case> CaseNewMap,map<id,Case> CaseOldMap){
        CreateNewOpp(CaseNewMap.values());
    }
    Public void OnAfterUpdate(map<id,Case> CaseNewMap,map<id,Case> CaseOldMap){
        CreateNewOpp(CaseNewMap.values());
    }
    
  Public void CreateNewOpp(List<Case> NewCaseList){
    List<Opportunity> oppList= New List<Opportunity>();
    
    For(Case Cs: NewCaseList){
    System.Debug('==========='+NewCaseList);
    
       IF(Cs.nbfg__Status__c=='Closed'){
        Opportunity opp=new Opportunity();
        System.Debug('======Op======'+Opp);
        opp.accountid = cs.accountid;
        opp.name = 'New Opportunity -' + cs.casenumber;
        opp.stageName = 'Prospecting';
        opp.nbfg__Case__c=cs.casenumber;
        opp.closedate = system.today();
        opplist.add(opp);
     
       }
    }
    if(oppList.size() > 0){
       insert opplist;
    }
  }

}
MithunPMithunP
Hi Sundhar,

Share your trigger code and error details.

Best Regards,
Mithun.
sundhar.mks1.3962649227519546E12sundhar.mks1.3962649227519546E12
Used for Test Class
Code
===============
@isTest(SeeAllData=true)
public class CasetriggerHandlerTest
{
   public static void CreateNewOpptest(){
     account acc = new account(name = 'test acc for case');
        insert acc;
        case cs = new case (accountid = acc.id, status = 'closed', origin = 'Phone');
        insert cs;
    }
}

Error:
Compile Error: Invalid constructor syntax, name=value pairs can only be used for SObjects at line 5 column 20
MithunPMithunP
Hi Sundar,

I asked for your trigger code.

Best Regards,
Mithun.
sundhar.mks1.3962649227519546E12sundhar.mks1.3962649227519546E12
Actually the trigger call to Apex class.I have used new Opportunity code to Apex class

Trigger
==============
trigger Casetrigger on Case (After insert, After update){
CasetriggerHandler handler = new CasetriggerHandler();

     if(trigger.isAfter && trigger.IsInsert){
        handler.OnAfterInsert(trigger.newmap, Trigger.OldMap);
    } 
      
    if(trigger.isAfter && trigger.IsUpdate){
        handler.OnAfterUpdate(trigger.newmap, Trigger.OldMap);
    }  
}



I need to write the test class for following class

Apex Class
====================
Public class CasetriggerHandler{

    Public void OnAfterInsert(map<id,Case> CaseNewMap,map<id,Case> CaseOldMap){
        CreateNewOpp(CaseNewMap.values());
    }
    Public void OnAfterUpdate(map<id,Case> CaseNewMap,map<id,Case> CaseOldMap){
        CreateNewOpp(CaseNewMap.values());
    }
    
  Public void CreateNewOpp(List<Case> NewCaseList){
    List<Opportunity> oppList= New List<Opportunity>();
    
    For(Case Cs: NewCaseList){
    System.Debug('==========='+NewCaseList);
    
       IF(Cs.nbfg__Status__c=='Closed'){
        Opportunity opp=new Opportunity();
        System.Debug('======Op======'+Opp);
        opp.accountid = cs.accountid;
        opp.name = 'New Opportunity -' + cs.casenumber;
        opp.stageName = 'Prospecting';
       // opp.nbfg__Case__c=cs.casenumber;
        opp.closedate = system.today();
        opplist.add(opp);
     
       }
    }
    if(oppList.size() > 0){
       insert opplist;
    }
  }

}
MithunPMithunP
Hi Sundhar,

I have tested your trigger, trigger helper class and test class (all 3) in my dev org, everything is working fine.
If want you can copy and try them in your dev org.

I think there may be some other triggers are firing on Case or Account or Opprtunity etc.. in your current org. Please check the error message properly that where it is pointing out and try to fix that trigger or class.

Best Regards,
Mithun.
sundhar.mks1.3962649227519546E12sundhar.mks1.3962649227519546E12
Hi Mithun,

How to Send all the product information to the primary contact of the opportunity,Kindly give any idea?

Thanks
sundhar.mks1.3962649227519546E12sundhar.mks1.3962649227519546E12
Hi Mithun,

I have Completed test class,

 @isTest(SeeAllData=true)
public class CasetriggerHandlerTest
{
   @isTest public static void CreateNewOpptest(){
   
   //Inserting New Case record Creation
   Case newcase = new Case();
   
   newcase.nbfg__Status__c = 'New'; 
   newcase.Status          = 'Working';
      
   insert newcase;
   
   // Updating the Case record
   List<Case> NewListCase = new List<Case>();
   Case Caselist = [Select id,nbfg__Status__c,casenumber from Case Where id=:newcase.id Limit 1];
   
   Caselist.nbfg__Status__c = 'Closed';
   
   NewListCase.add(Caselist);
   
   Update NewListCase;
   
   //Inserting New Opportunity Record
   Opportunity NewOpp = new Opportunity();
   
   NewOpp.name = 'New Opportunity -' + Caselist .casenumber;
   NewOpp.stageName = 'Prospecting';
   NewOpp.closedate = system.today();
   
   insert NewOpp;
     
  }
}

Thanks.