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
RossGRossG 

question on list syntax

Anyone know how I can add a soql statement to this:

 

public void createEngagementWhenNeeded1(List<Opportunity> opportunities)

 

Basically that's a list of opps.  Is it possible to adda soql statement to that, like this:

 


List<Opportunity> opps = [SELECT Id,Services_to_be_Delivered_by__c,Probability,Threshold__c,Account.Id,Account.Name,Region__c,
CreatedById,Account.Region__r.OwnerId,Account.Region__r.RSA_Manager__r.Id,Professional_Services__c,
Name From Opportunity];

 

Not sure how to get that to work.

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

You dont seem to have a where clause

 

public class OpportunityTriggerHelper{

    public void createEngagementWhenNeeded1(List<Opportunity> opportunities)
    {

        List<Opportunity> opps = [SELECT Id,Services_to_be_Delivered_by__c,Probability,Threshold__c,Account.Id,Account.Name,Region__c,
                                                       CreatedById,Account.Region__r.OwnerId,Account.Region__r.RSA_Manager__r.Id,Professional_Services__c, 
                                                       Name From Opportunity WHERE Id IN:ooportunities];
        List< SFL5_Projects__c> engagementsToCreate = new List<SFL5_Projects__c>();

        for(Opportunity opp: opps)

        {
       
            if((opp.Probability>=75 && opp.Services_to_be_Delivered_by__c.contains('Training Team')) || (opp.Probability>=50 && 
                (opp.Services_to_be_Delivered_by__c.contains('Global Client Services')||opp.Services_to_be_Delivered_by__c.contains('Partner') ||  
                 opp.Services_to_be_Delivered_by__c.contains('Regional personnel')   )))         
            {
               SFL5_Projects__c engagementToAdd = new SFL5_Projects__c();
             if(opp.Account.Region__r.RSA_Manager__r.Id!=null){
              engagementToAdd.OwnerId = opp.Account.Region__r.RSA_Manager__r.Id;
           } else{
                 engagementToAdd.OwnerId = opp.OwnerId;
            }
                 engagementToAdd.Name = 'Services Engagement for: '+ opp.Account.Name;
                 engagementToAdd.Opportunity__c = opp.Id;
                 engagementToAdd.Client__c = opp.AccountId;
                 engagementToAdd.Region__c = opp.Region__c;
                 engagementToAdd.Project_Status__c = 'Active';
                 engagementToAdd.Services_Amount__c = opp.Professional_Services__c;
                 engagementsToCreate.add(engagementToAdd);
             }
        }
           insert engagementsToCreate;        
    }
   
    public void createEngagementWhenNeeded2(List<Opportunity> opportunities, Map<Id, Opportunity> oldMap)
    {
        List<Opportunity> opps = [SELECT Id, Threshold__c,Account.Id,Account.Name,Region__c,CreatedById,Account.Region__r.OwnerId,
                                                       Account.Region__r.RSA_Manager__r.Id,Professional_Services__c, Name From Opportunity WHERE id IN:Opportunities];
        List< SFL5_Projects__c > engagementsToCreate = new List< SFL5_Projects__c >();

        for(Opportunity opp: opportunities)
        {
            Opportunity beforeUpdate = oldMap.get(opp.Id);
            if(opp.Threshold__c=='1' && beforeUpdate.Threshold__c!='1')
           {
               SFL5_Projects__c engagementToAdd = new SFL5_Projects__c();
               
               if(opp.Account.Region__r.RSA_Manager__r.Id!=null)
               {
                  engagementToAdd.OwnerId = opp.Account.Region__r.RSA_Manager__r.Id;
                } else{
                   engagementToAdd.OwnerId = opp.OwnerId;
                }
                   engagementToAdd.Name = 'Services Engagement for: '+ opp.Account.Name;
                   engagementToAdd.Opportunity__c = opp.Id;
                   engagementToAdd.Client__c = opp.AccountId;
                   engagementToAdd.Region__c = opp.Region__c;
                   engagementToAdd.Project_Status__c = 'Active';
                   engagementToAdd.Services_Amount__c = opp.Professional_Services__c;
                   engagementsToCreate.add(engagementToAdd);
               }
         }

           insert engagementsToCreate; 
    }
}

All Answers

Avidev9Avidev9

Well not sure about the question!

 

do you want to pass a value to this method ?

public void createEngagementWhenNeeded1(List<Opportunity> opportunities)

 It should be something like

 

createEngagementWhenNeeded1( [SELECT Id,Services_to_be_Delivered_by__c,Probability,Threshold__c,Account.Id,Account.Name,Region__c,
CreatedById,Account.Region__r.OwnerId,Account.Region__r.RSA_Manager__r.Id,Professional_Services__c,
Name From Opportunity]);

 

or

 

 

List<Opportunity> opps = [SELECT Id,Services_to_be_Delivered_by__c,Probability,Threshold__c,Account.Id,Account.Name,Region__c,
CreatedById,Account.Region__r.OwnerId,Account.Region__r.RSA_Manager__r.Id,Professional_Services__c,
Name From Opportunity];
createEngagementWhenNeeded1(opps);

 

 

 

RossGRossG

Thanks a lot.

 

Probably did a bad job explaining what I'm trying to do so sorry about that.

 

Basically I've got this need: Create 1 custom object record, when an opportunity is insertred or updated to meet  a couple basic criteria.

 

So I've got a trigger that calls a class to do this.

 

Here's my trigger, followed by the class:

 

trigger TESTOpportunityTrigger on Opportunity (after insert, after update) {
/// BEGIN Services Engagement Auto-create Processing for Opps
OpportunityTriggerHelper helper = new OpportunityTriggerHelper();
if(Trigger.isInsert && Trigger.isAfter)
{
helper.createEngagementWhenNeeded1(Trigger.new);
}
OpportunityTriggerHelper helper2 = new OpportunityTriggerHelper();
if(Trigger.isUpdate && Trigger.isAfter)
{
helper2.createEngagementWhenNeeded2(Trigger.new, Trigger.oldMap);
}

/// END Services Engagement Auto-create Processing
}

 Class:

 

public class OpportunityTriggerHelper{

    public void createEngagementWhenNeeded1(List<Opportunity> opportunities)
    {

        List<Opportunity> opps = [SELECT Id,Services_to_be_Delivered_by__c,Probability,Threshold__c,Account.Id,Account.Name,Region__c,
                                                       CreatedById,Account.Region__r.OwnerId,Account.Region__r.RSA_Manager__r.Id,Professional_Services__c, 
                                                       Name From Opportunity];
        List< SFL5_Projects__c> engagementsToCreate = new List<SFL5_Projects__c>();

        for(Opportunity opp: opps)

        {
       
            if((opp.Probability>=75 && opp.Services_to_be_Delivered_by__c.contains('Training Team')) || (opp.Probability>=50 && 
                (opp.Services_to_be_Delivered_by__c.contains('Global Client Services')||opp.Services_to_be_Delivered_by__c.contains('Partner') ||  
                 opp.Services_to_be_Delivered_by__c.contains('Regional personnel')   )))         
            {
               SFL5_Projects__c engagementToAdd = new SFL5_Projects__c();
             if(opp.Account.Region__r.RSA_Manager__r.Id!=null){
              engagementToAdd.OwnerId = opp.Account.Region__r.RSA_Manager__r.Id;
           } else{
                 engagementToAdd.OwnerId = opp.OwnerId;
            }
                 engagementToAdd.Name = 'Services Engagement for: '+ opp.Account.Name;
                 engagementToAdd.Opportunity__c = opp.Id;
                 engagementToAdd.Client__c = opp.AccountId;
                 engagementToAdd.Region__c = opp.Region__c;
                 engagementToAdd.Project_Status__c = 'Active';
                 engagementToAdd.Services_Amount__c = opp.Professional_Services__c;
                 engagementsToCreate.add(engagementToAdd);
             }
        }
           insert engagementsToCreate;        
    }
   
    public void createEngagementWhenNeeded2(List<Opportunity> opportunities, Map<Id, Opportunity> oldMap)
    {
        List<Opportunity> opps = [SELECT Id, Threshold__c,Account.Id,Account.Name,Region__c,CreatedById,Account.Region__r.OwnerId,
                                                       Account.Region__r.RSA_Manager__r.Id,Professional_Services__c, Name From Opportunity];
        List< SFL5_Projects__c > engagementsToCreate = new List< SFL5_Projects__c >();

        for(Opportunity opp: opportunities)
        {
            Opportunity beforeUpdate = oldMap.get(opp.Id);
            if(opp.Threshold__c=='1' && beforeUpdate.Threshold__c!='1')
           {
               SFL5_Projects__c engagementToAdd = new SFL5_Projects__c();
               
               if(opp.Account.Region__r.RSA_Manager__r.Id!=null)
               {
                  engagementToAdd.OwnerId = opp.Account.Region__r.RSA_Manager__r.Id;
                } else{
                   engagementToAdd.OwnerId = opp.OwnerId;
                }
                   engagementToAdd.Name = 'Services Engagement for: '+ opp.Account.Name;
                   engagementToAdd.Opportunity__c = opp.Id;
                   engagementToAdd.Client__c = opp.AccountId;
                   engagementToAdd.Region__c = opp.Region__c;
                   engagementToAdd.Project_Status__c = 'Active';
                   engagementToAdd.Services_Amount__c = opp.Professional_Services__c;
                   engagementsToCreate.add(engagementToAdd);
               }
         }

           insert engagementsToCreate; 
    }
}

 

 

This thing works in the UI.  However it's not working when I test by inserting more than 200 opps that fire the trigger.  Nothing fails, but what happens is 2 records get created for every inserted opp, instead of just one.  

 

I THINK my problem is in the class.  Actually I'm almost positive that's the problem.  Any idea what I'm doing wrong there that would cause this thing to crete 2 records instead of 1 for every inserted opp?

 

Thanks so much for your help!

 

 

Avidev9Avidev9

You dont seem to have a where clause

 

public class OpportunityTriggerHelper{

    public void createEngagementWhenNeeded1(List<Opportunity> opportunities)
    {

        List<Opportunity> opps = [SELECT Id,Services_to_be_Delivered_by__c,Probability,Threshold__c,Account.Id,Account.Name,Region__c,
                                                       CreatedById,Account.Region__r.OwnerId,Account.Region__r.RSA_Manager__r.Id,Professional_Services__c, 
                                                       Name From Opportunity WHERE Id IN:ooportunities];
        List< SFL5_Projects__c> engagementsToCreate = new List<SFL5_Projects__c>();

        for(Opportunity opp: opps)

        {
       
            if((opp.Probability>=75 && opp.Services_to_be_Delivered_by__c.contains('Training Team')) || (opp.Probability>=50 && 
                (opp.Services_to_be_Delivered_by__c.contains('Global Client Services')||opp.Services_to_be_Delivered_by__c.contains('Partner') ||  
                 opp.Services_to_be_Delivered_by__c.contains('Regional personnel')   )))         
            {
               SFL5_Projects__c engagementToAdd = new SFL5_Projects__c();
             if(opp.Account.Region__r.RSA_Manager__r.Id!=null){
              engagementToAdd.OwnerId = opp.Account.Region__r.RSA_Manager__r.Id;
           } else{
                 engagementToAdd.OwnerId = opp.OwnerId;
            }
                 engagementToAdd.Name = 'Services Engagement for: '+ opp.Account.Name;
                 engagementToAdd.Opportunity__c = opp.Id;
                 engagementToAdd.Client__c = opp.AccountId;
                 engagementToAdd.Region__c = opp.Region__c;
                 engagementToAdd.Project_Status__c = 'Active';
                 engagementToAdd.Services_Amount__c = opp.Professional_Services__c;
                 engagementsToCreate.add(engagementToAdd);
             }
        }
           insert engagementsToCreate;        
    }
   
    public void createEngagementWhenNeeded2(List<Opportunity> opportunities, Map<Id, Opportunity> oldMap)
    {
        List<Opportunity> opps = [SELECT Id, Threshold__c,Account.Id,Account.Name,Region__c,CreatedById,Account.Region__r.OwnerId,
                                                       Account.Region__r.RSA_Manager__r.Id,Professional_Services__c, Name From Opportunity WHERE id IN:Opportunities];
        List< SFL5_Projects__c > engagementsToCreate = new List< SFL5_Projects__c >();

        for(Opportunity opp: opportunities)
        {
            Opportunity beforeUpdate = oldMap.get(opp.Id);
            if(opp.Threshold__c=='1' && beforeUpdate.Threshold__c!='1')
           {
               SFL5_Projects__c engagementToAdd = new SFL5_Projects__c();
               
               if(opp.Account.Region__r.RSA_Manager__r.Id!=null)
               {
                  engagementToAdd.OwnerId = opp.Account.Region__r.RSA_Manager__r.Id;
                } else{
                   engagementToAdd.OwnerId = opp.OwnerId;
                }
                   engagementToAdd.Name = 'Services Engagement for: '+ opp.Account.Name;
                   engagementToAdd.Opportunity__c = opp.Id;
                   engagementToAdd.Client__c = opp.AccountId;
                   engagementToAdd.Region__c = opp.Region__c;
                   engagementToAdd.Project_Status__c = 'Active';
                   engagementToAdd.Services_Amount__c = opp.Professional_Services__c;
                   engagementsToCreate.add(engagementToAdd);
               }
         }

           insert engagementsToCreate; 
    }
}
This was selected as the best answer
RossGRossG

Bingo.  Thanks