• Erin_AP_Admin
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

Hi, I am just learning how to read/write Apex, so I'm sure this is a really basic problem, but I would really appreciate any help!

 

I created a trigger that will create a new Case record when an Opportunity meets the following criteria (Type = 'Maintenance Renewal' & Probability = 100).  I'm running into two issues with the below code:

1) the trigger is working, except it is creating 2 identical Case records for each Opp which meets the above criteria

2) I can't figure out how to populate the 2 necessary lookup fields on the Case: Owner (which needs to be the Maintenance Renewal Queue) & Contact (which needs to be the Opportunity Owner)

 

Thank you in advance to anyone who can help!!

 

Here is the trigger:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
trigger createNewCase on Opportunity (after update) 
{
    List<Case> listCase = new List<Case>();
    for(Opportunity o : trigger.new)
    {
        if(o.Type == 'Maintenance Renewal' && o.Probability == 100)
        {
          //here Opportunity__c is lookup to Opportunity record for which new record is created
          listCase.add(new Case(Opportunity__c = o.id, 
          Subject = o.Name,
          Origin = 'Email-Renewals',
          Priority = 'Medium',
          Status = 'New'));
          
        }
    }
    
    if(listCase.size() > 0)
    {
        insert listCase;
    }
}

Hi, I am just learning how to read/write Apex, so I'm sure this is a really basic problem, but I would really appreciate any help!

 

I created a trigger that will create a new Case record when an Opportunity meets the following criteria (Type = 'Maintenance Renewal' & Probability = 100).  I'm running into two issues with the below code:

1) the trigger is working, except it is creating 2 identical Case records for each Opp which meets the above criteria

2) I can't figure out how to populate the 2 necessary lookup fields on the Case: Owner (which needs to be the Maintenance Renewal Queue) & Contact (which needs to be the Opportunity Owner)

 

Thank you in advance to anyone who can help!!

 

Here is the trigger:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
trigger createNewCase on Opportunity (after update) 
{
    List<Case> listCase = new List<Case>();
    for(Opportunity o : trigger.new)
    {
        if(o.Type == 'Maintenance Renewal' && o.Probability == 100)
        {
          //here Opportunity__c is lookup to Opportunity record for which new record is created
          listCase.add(new Case(Opportunity__c = o.id, 
          Subject = o.Name,
          Origin = 'Email-Renewals',
          Priority = 'Medium',
          Status = 'New'));
          
        }
    }
    
    if(listCase.size() > 0)
    {
        insert listCase;
    }
}