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
Thomas CailletThomas Caillet 

Create multiple case for each opportunity product when opportunity is won (trigger)

Hi everybody,

I'm trying to make a trigger that create one case for each opportunity product when opportunity is won.
I know how to create case but I don't know how to browse all opportunity products.
I'm new on apex so I need your help please.

Thanks to you 
Best Answer chosen by Thomas Caillet
GauravGargGauravGarg
Thomas,

Yes, please try with below code, I tried it on my org  and it works:

trigger OpportuntiyTrigger_AT on Opportunity(after insert, after update) {
    if ((Trigger.isAfter && Trigger.isInsert) || (Trigger.isAfter && Trigger.isUpdate)) {
        List < id > ListOpportunityLineItem = new List < id > ();
        Map < Id, Opportunity > opptyMap = new Map < id, Opportunity > ();
        for (Opportunity o: Trigger.new) {
            if (o.StageName == 'Closed won')
                ListOpportunityLineItem.add(o.id);
            opptyMap.put(o.id, o);
        }
        List < OpportunityLineItem > opptyLineItemList = new List < OpportunityLineItem > ([select id, name, Description, UnitPrice from opportunityLineItem where OpportunityId IN: ListOpportunityLineItem]);
        List <
            case >caseListToInsert = new List < Case > ();
        for (OpportunityLineItem o: opptyLineItemList) {
            Case c = new Case(Subject = O.name, Description = o.description, Origin = 'web', AccountId = opptyMap.get(o.OpportunityId).Accountid);
            caseListToInsert.add(c);
        }
        if (caseListToInsert != null && caseListToInsert.size() > 0)
            insert caseListToInsert;
    }
}

Thanks,
Gaurav

All Answers

GauravGargGauravGarg
Hi Thomas,

In SFDC Opportunity Product is named as Opportunity Line Item, please find below code to create new case:

trigger OpportuntiyTriggerHandler on Opportunity (after insert, after update){
    set<id> ListOpportunityLineItem = new set<id>();
    for(Opportunity o: trigger.new){
        if(o.StageName == 'won')
          ListOpportunityLineItem.add(o.id);
    } 
    List<OpportunityLineItem> opptyLineItemList = new List<OpportunityLineItem>([select id, name, Description from opportunityLineItem where OpportunityId IN :ListOpportunityLineItem]);
    List<case> caseListToInsert = new List<Case>();
    for(OpportunityLineItem opl: opptyLineItemList){
        Case c = new Case(description = opl.description, status = opl.name);
        caseListToInsert.add(c);
    }
    if(caseListToInsert != null && caseListToInsert.size > 0)
        insert caseListToInsert;
}


Please let me know if you still face any issues, or contact me on email: gauravgarg.nmims@gmail.com, skype: gaurav62990

Thanks,
Gaurav
Thomas CailletThomas Caillet
Thanks Gaurav to your answer.
I didn't test your code because you create cases according to OLI and me I'd like to do according to opportunity with OLI information.
I find something that works. See the code below :

trigger createCase on Opportunity (after update)
{
                if(trigger.new[0].StageName=='closed won'&&(trigger.old[0].StageName<>'closed won'))
                {
                                
                                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,OwnerId='XXX', AccountId = trigger.new[0].AccountId);
                                        ll.add(c1);
                                     }
                 insert ll;   
                }
}
GauravGargGauravGarg

Hi Thomas,

Above correct code is correct, if you want to use Standard Product and Pricebook object, but Opportunity Product are referred as OpportunityLineItem.

Also, above code will need to update as per your requirement. This code will only work for single (first) record. 

Thanks,

Gaurav

Thomas CailletThomas Caillet
Gaurav,

You're sure because I tested it and it works for all OLI and it create as many cases as OLI
GauravGargGauravGarg
Thomas,
In case of MassUpdate it will fail. If you manually update the Opportunity then the trigger will work as expected. 

Thanks,
Gaurav
Thomas CailletThomas Caillet
Ok thank you for informations, so could you explain me how to put OLI AND opportunity informations on case with your code because I've test Case c = new Case(status = o.name); and I doesn't work.
Thanks
GauravGargGauravGarg
Thomas,

Please replace below line:
  Case c = new Case(description = opl.description, status = opl.name); // Replace this line
  Case c = new Case(description = opl.description, subject = opl.name);

Thanks,
Gaurav
Thomas CailletThomas Caillet
Yes you're right but I talk about opportunity informations like :
Case c = new Case(AccountId = o.AccountId); 
Doesn't work :/
GauravGargGauravGarg

Thomas,

For creating new case, Origin is a requried standard field. Please look below Code.

Case c = new Case(Subject = O.name, Description = o.description, Origin = web, AccountId = O.Accountid);
insert c;

I have tried this in my org. It works.

let me know if you still face some issue, or contact me on: email: gauravgarg.nmim@gmail.com, skype: gaurav62990
Thanks,

Gaurav

Thomas CailletThomas Caillet
Error: Compile Error: Variable does not exist: O.name at line 10 column 37
Thomas
GauravGargGauravGarg

Thomas,
can you please post the updated code, you are trying to insert.

Thanks,
Gaurav

Thomas CailletThomas Caillet
I tried your code : 

trigger OpportuntiyTriggerHandler on Opportunity (after insert, after update){
    set<id> ListOpportunityLineItem = new set<id>();
    for(Opportunity o: trigger.new){
        if(o.StageName == 'Closed won')
          ListOpportunityLineItem.add(o.id);
    } 
    List<OpportunityLineItem> opptyLineItemList = new List<OpportunityLineItem>([select id, name, Description, UnitPrice from opportunityLineItem where OpportunityId IN :ListOpportunityLineItem]);
    List<case> caseListToInsert = new List<Case>();
    for(OpportunityLineItem opl: opptyLineItemList){
        Case c = new Case(Subject = O.name, Description = o.description, Origin = web, AccountId = O.Accountid);
insert c;
    }
    if(caseListToInsert != null && caseListToInsert.size() > 0)
        insert caseListToInsert;
}
GauravGargGauravGarg
Thomas,

Yes, please try with below code, I tried it on my org  and it works:

trigger OpportuntiyTrigger_AT on Opportunity(after insert, after update) {
    if ((Trigger.isAfter && Trigger.isInsert) || (Trigger.isAfter && Trigger.isUpdate)) {
        List < id > ListOpportunityLineItem = new List < id > ();
        Map < Id, Opportunity > opptyMap = new Map < id, Opportunity > ();
        for (Opportunity o: Trigger.new) {
            if (o.StageName == 'Closed won')
                ListOpportunityLineItem.add(o.id);
            opptyMap.put(o.id, o);
        }
        List < OpportunityLineItem > opptyLineItemList = new List < OpportunityLineItem > ([select id, name, Description, UnitPrice from opportunityLineItem where OpportunityId IN: ListOpportunityLineItem]);
        List <
            case >caseListToInsert = new List < Case > ();
        for (OpportunityLineItem o: opptyLineItemList) {
            Case c = new Case(Subject = O.name, Description = o.description, Origin = 'web', AccountId = opptyMap.get(o.OpportunityId).Accountid);
            caseListToInsert.add(c);
        }
        if (caseListToInsert != null && caseListToInsert.size() > 0)
            insert caseListToInsert;
    }
}

Thanks,
Gaurav
This was selected as the best answer
Thomas CailletThomas Caillet
Thanks to you, I mark your answer as best answer.
Have a nice day :)
Thomas
GauravGargGauravGarg
thanks Thomas. 
Have a nice day you too.