• Sergio AV
  • NEWBIE
  • 55 Points
  • Member since 2017

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

The apex code below is resolving the following requirement (specified as an exercises for practice):

When an opportunity is inserted, automatically add the Oppty's Owner's Manager as a Team Member (of the Opportunity being inserted), with the role "Sales Manager".

The solution provided by the exercise's author is below:
trigger OwnerManager on Opportunity (after insert) {
    for(Opportunity opp : Trigger.new){
        //Get opp owner manager info
        Opportunity oppWithManagerInfo = [SELECT Id,
                                                 Owner.ManagerId
                                            FROM Opportunity
                                           WHERE Id = :opp.Id];
        //Create Opportunity Team Role
        if (oppWithManagerInfo.Owner.ManagerId != null) {
            OpportunityTeamMember otm = new OpportunityTeamMember();
            otm.UserId                = oppWithManagerInfo.Owner.ManagerId;
            otm.OpportunityId         = opp.Id;
            otm.TeamMemberRole        = 'Sales Manager';
            insert otm;
        }
    }
}
My question is regarding Line 7 --> WHERE Id = :opp.Id

The question is: since the trigger is an "after" trigger, from what I have learnt regarding "after" trigger, you would need to first INSERT the value contained in the variable opp (insert opp;) in order for Salesforce to generate an Id for that record. So, I would have thought that Line 7 would only work after opp has been inserted in the data base.

But, the above code seems to work fine, that is why I am perplexed and would like to understand how can the system know the opp.Id before the opp record has even been inserted by the apex code.

Thank you very much.
Hi All,

I have created batch apex class to create multiple customers if account has no customers. Below is the code which creates one customer after running batch apex. 
But my requirement is to create 7 customers from Date given in Account record. 
Example: If Date__c in Account is 25-10-2017, then 7 customers. 1st customer should be created with Date__c = 25-10-2017, 2nd customer should be created with Date__c = 26-10-2017 till 7 days.


Below batch class creates a single customer for Account which has no customer:

global class batchContactCreate implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
       return Database.getQueryLocator([
                select Name, Date__c from Account where ID NOT IN (SELECT Account__c from S1_Customer__c)]);  
    }
    
   
    global void execute(Database.BatchableContext BC, List<Account> scope) {
         List<S1_Customer__c> cList = new List<S1_Customer__c>();
         for(Account a : scope)
         {
            S1_Customer__c cust = new S1_Customer__c(Name =a.Name, Account__c = a.Id, Phone__c = '1212121232');
            cList.add(cust); 
                   
         }
         insert cList;
    }   
    
    global void finish(Database.BatchableContext BC) {
    }
}

Thanks in advance
Hello,

The apex code below is resolving the following requirement (specified as an exercises for practice):

When an opportunity is inserted, automatically add the Oppty's Owner's Manager as a Team Member (of the Opportunity being inserted), with the role "Sales Manager".

The solution provided by the exercise's author is below:
trigger OwnerManager on Opportunity (after insert) {
    for(Opportunity opp : Trigger.new){
        //Get opp owner manager info
        Opportunity oppWithManagerInfo = [SELECT Id,
                                                 Owner.ManagerId
                                            FROM Opportunity
                                           WHERE Id = :opp.Id];
        //Create Opportunity Team Role
        if (oppWithManagerInfo.Owner.ManagerId != null) {
            OpportunityTeamMember otm = new OpportunityTeamMember();
            otm.UserId                = oppWithManagerInfo.Owner.ManagerId;
            otm.OpportunityId         = opp.Id;
            otm.TeamMemberRole        = 'Sales Manager';
            insert otm;
        }
    }
}
My question is regarding Line 7 --> WHERE Id = :opp.Id

The question is: since the trigger is an "after" trigger, from what I have learnt regarding "after" trigger, you would need to first INSERT the value contained in the variable opp (insert opp;) in order for Salesforce to generate an Id for that record. So, I would have thought that Line 7 would only work after opp has been inserted in the data base.

But, the above code seems to work fine, that is why I am perplexed and would like to understand how can the system know the opp.Id before the opp record has even been inserted by the apex code.

Thank you very much.
Hi All,

I have created batch apex class to create multiple customers if account has no customers. Below is the code which creates one customer after running batch apex. 
But my requirement is to create 7 customers from Date given in Account record. 
Example: If Date__c in Account is 25-10-2017, then 7 customers. 1st customer should be created with Date__c = 25-10-2017, 2nd customer should be created with Date__c = 26-10-2017 till 7 days.


Below batch class creates a single customer for Account which has no customer:

global class batchContactCreate implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
       return Database.getQueryLocator([
                select Name, Date__c from Account where ID NOT IN (SELECT Account__c from S1_Customer__c)]);  
    }
    
   
    global void execute(Database.BatchableContext BC, List<Account> scope) {
         List<S1_Customer__c> cList = new List<S1_Customer__c>();
         for(Account a : scope)
         {
            S1_Customer__c cust = new S1_Customer__c(Name =a.Name, Account__c = a.Id, Phone__c = '1212121232');
            cList.add(cust); 
                   
         }
         insert cList;
    }   
    
    global void finish(Database.BatchableContext BC) {
    }
}

Thanks in advance