+ Start a Discussion
matt.bylermatt.byler 

Creating a related list itam based on opportunity products

I currently have a trigger that when an opportunity gets marked closed won it creates a record in our custom object and inserts a case under the custom object. We would like the cases to be inserted based on what products are on the opportunity. Example: If the opportunity has one product the trigger will create one case. However if it has more than one product it would insert one case for each product. Is this possible to do?

Best Answer chosen by Admin (Salesforce Developers) 
matt.bylermatt.byler

I have resolved the issue by creating the following code

 

trigger createworkorder on Opportunity (after update)
{
                if(trigger.new[0].StageName=='closed won'&&(trigger.old[0].StageName<>'closed won'))
                {
                                Work_Order__c obj=new Work_Order__c(Opportunity__c=trigger.new[0].id,Stage__c='New',Account__c=trigger.new[0].AccountId);
                                insert obj;
                                List<Case> ll=new List<Case>();
                                for(OpportunityLineItem la : [SELECT PricebookEntry.Product2.Name from OpportunityLineItem where opportunityId =: Trigger.new])
                                     {
                                        String ss= String.ValueOf(la.PricebookEntry.Product2.Name);
                                        case c1=new case(Status='new',Subject='Fulfillment for'+' '+ss,Work_Order__c=obj.id);
                                        ll.add(c1);
                                     }
                 insert ll;   
                }
}

 

I want to thank everyone who has assisted me on this code.

All Answers

MandyKoolMandyKool

Yes definately.

 

Once the opportunity is "Closed Won" you should query all the OpportunityLineItems, count them and create case for each one of them.

 

 

matt.bylermatt.byler

Can you give me an example of how I would fit this into my existing trigger code that I have made?

 

Code:

 

trigger createworkorder on Opportunity (after update)
{
if(trigger.new[0].StageName=='closed won'&&(trigger.old[0].StageName<>'closed won'))
{
Work_Order__c obj=new Work_Order__c(Opportunity__c=trigger.new[0].id);
insert obj;
case c1=new case(Status='new',Subject='Initial case for Fulfillment',Work_Order__c=obj.id);
insert c1;
}
}

matt.bylermatt.byler

I have resolved the issue by creating the following code

 

trigger createworkorder on Opportunity (after update)
{
                if(trigger.new[0].StageName=='closed won'&&(trigger.old[0].StageName<>'closed won'))
                {
                                Work_Order__c obj=new Work_Order__c(Opportunity__c=trigger.new[0].id,Stage__c='New',Account__c=trigger.new[0].AccountId);
                                insert obj;
                                List<Case> ll=new List<Case>();
                                for(OpportunityLineItem la : [SELECT PricebookEntry.Product2.Name from OpportunityLineItem where opportunityId =: Trigger.new])
                                     {
                                        String ss= String.ValueOf(la.PricebookEntry.Product2.Name);
                                        case c1=new case(Status='new',Subject='Fulfillment for'+' '+ss,Work_Order__c=obj.id);
                                        ll.add(c1);
                                     }
                 insert ll;   
                }
}

 

I want to thank everyone who has assisted me on this code.

This was selected as the best answer