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
BennettTranBennettTran 

Trigger to create case based off of opportunity fields and opportunity products

Hello,

 

I am trying to write a trigger that will create a specific case type when an opportunity meets the following criteria:

 

The opportunity field type = adjustment or initial sale

The opportunity stage = pending setup or closed won

One of the following products is on the opportunity:

  • Product A
  • Product B
  • Product C

I currently have another trigger that is creating a case based off of opportunity fields but am not sure how to associate opportunity products to create this new trigger.  See Below:

 

trigger ImplementationBillingCaseCreation on Opportunity (after insert,after update) {
    string recordtype = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Implementation new').getRecordTypeId();
    List<Case> cases = new List<Case>();
    for (Opportunity opp: Trigger.New)
    { case newcase= new case();
         if((trigger.isInsert && (opp.StageName.toLowerCase().equals('pending setup'))&&(opp.Type.toLowerCase().equals('initial sales')))||(trigger.isInsert && (opp.StageName.toLowerCase().equals('closed won'))&&(opp.Type.toLowerCase().equals('billing sales')))||(trigger.isUpdate &&(opp.StageName !=  Trigger.oldMap.get(opp.Id).StageName && opp.StageName.toLowerCase().equals('pending setup'))&&((opp.Type.toLowerCase().equals('initial sales'))) && !opp.On_Boarding_Case_Created__c)||(trigger.isUpdate &&(opp.StageName !=  Trigger.oldMap.get(opp.Id).StageName && opp.StageName.toLowerCase().equals('closed won'))&&((opp.Type.toLowerCase().equals('billing sales')))))
         {
            system.debug('----------opp.AccountId---->'+opp.AccountId);
            newcase.AccountId=opp.AccountId;
            newcase.Opportunity__c=opp.Id;
            newcase.Subject='New Implementation case for '+opp.Account_Name__c;
            newcase.Status='New';
            newcase.Origin='Sign Up Form';
            newcase.Priority='Medium';
            newcase.RecordTypeId=recordtype;
            cases.add(newcase);
         }
         
    }
    if(cases.size()!=0 && cases.size()!=null)
    {
        system.debug('----------cases.size()---->'+cases.size());
        insert cases;
    }

 

Thank you in advance!

Best Answer chosen by BennettTran
Vinita_SFDCVinita_SFDC

Hello,

 

If you are looking for products test1 and test2, then you can query like:

 

List<OpportunityLineItem> oppProds = [SELECT Id, PricebookEntry.Product2.Name FROM OpportunityLineItem WHERE OpportunityId IN :Trigger.new AND (PricebookEntry.Product2.Name = 'Test1' OR PricebookEntry.Product2.Name = 'Test2') ];

All Answers

Vinita_SFDCVinita_SFDC

Hello,

 

You can retrieve list of products related to an opportunity in a query as follows:

 

List<OpportunityLineItem> oppProds = [SELECT Id, PricebookEntry.Product2.Name FROM OpportunityLineItem WHERE OpportunityId IN :Trigger.new];

 

BennettTranBennettTran

After I run the query to retrive a list of all products on an opportunity, how do I see if it is the product I am looking for?

Vinita_SFDCVinita_SFDC

Hello,

 

If you are looking for products test1 and test2, then you can query like:

 

List<OpportunityLineItem> oppProds = [SELECT Id, PricebookEntry.Product2.Name FROM OpportunityLineItem WHERE OpportunityId IN :Trigger.new AND (PricebookEntry.Product2.Name = 'Test1' OR PricebookEntry.Product2.Name = 'Test2') ];

This was selected as the best answer