You need to sign in to do that
Don't have an account?

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!
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
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];
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?
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') ];