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
Vikram Singh 157Vikram Singh 157 

Hi ! All , add the related opportunity for each new or updated account if no opportunity is already associated with the account.(Name fOr opportunity 1,2,3,4 and stage name prospecting and close date should be one month ahead of current date.

This will work fine but unable to add opp by name 1, 2, 3, 4...

trigger CreateRelatedRec on Account (after insert, after update) {
           List<Opportunity> opplist = new List<Opportunity>();
            List<Account> Acc = [Select ID , Name from Account where Id in : trigger.new AND Id NOT  in
             (select accountid from Opportunity)];
             
            for(account a : Acc){
            
 opplist.add(New Opportunity(Name=a.Name +'opportunity',StageName='Prospecting',
 CloseDate=Date.today()+30,AccountId=a.Id));
        
         }
            If(oppList.Size()>0){
                upsert opplist;
            }
                }
Best Answer chosen by Vikram Singh 157
HARSHIL U PARIKHHARSHIL U PARIKH
Here we go Vikram Singh,

I have tested this trigger and it is working fine. Below is the output it would give. Trigger would only work as AFTER INSERT and not after update etc..

Final Output:

User-added image

Trigger Code:
 
Trigger CreatingAnAutoOpp On Account(After Insert){
    
    List<Opportunity> oppFinalListToInsert = New List<Opportunity>();
    
    If(Trigger.IsInsert)
    {
        For(Account act : Trigger.New)
        {
            
            For(Integer I = 1; I < 5; I++)
            {
                Opportunity Opp = New Opportunity();
                
                Opp.AccountId = act.Id;
                Opp.Name      = 'Opp' + ' ' + I;
                Opp.CloseDate = Date.Today() + 30;
                Opp.StageName = 'Prospecting';
                
                oppFinalListToInsert.add(Opp);
            }    
            
        }
    }
    
    try{
        If(!oppFinalListToInsert.IsEmpty()){
            insert oppFinalListToInsert;
        }
    }
    Catch(Exception e){
        System.debug('Thrown Exception for CreatingAnAutoOpp Trigger Is::'  + e.getMessage());
    }
}

Hope it helps and if it solves the query then please mark it as best answer!
 

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
Hi Vikram,

I think I can help you here but I am not understanding the requirement.
Vikram Singh 157Vikram Singh 157
Hi  the Requirement is i want to add "related opportunity for each new or updated account if no opportunity is already associated with that account.
and name should be start for opportunity with 1,2,3,4 .. like this and stage name should be 'prospecting'and close date should be one month ahead of current date. 
Everything was working fine but i am unable to create opprtunity with the name 1,2,3..
 
HARSHIL U PARIKHHARSHIL U PARIKH
Ok so let's say you enter a new Accunt into your system named "Acme, Inc." then you save the record. At this point this there should be AUTO CREATION of FOUR new opportunity named Opp-1, Opp-2, Opp-3, Opp-4 right under the Account Acme, Inc. And at the same time all of these opportunities would have Stage = "Prospecting" and ClosedDate = A month from created Date.

Is this the requirement?

If yes, then what happens when user updates the same Account (Acme, INC.) next day? should it create a 4 more opportunities? OR this auto creation should happen only at the first time.

Thank You!
Vikram Singh 157Vikram Singh 157
Hi, Harshil ,
This auto creation should be happen only first time .
HARSHIL U PARIKHHARSHIL U PARIKH
Here we go Vikram Singh,

I have tested this trigger and it is working fine. Below is the output it would give. Trigger would only work as AFTER INSERT and not after update etc..

Final Output:

User-added image

Trigger Code:
 
Trigger CreatingAnAutoOpp On Account(After Insert){
    
    List<Opportunity> oppFinalListToInsert = New List<Opportunity>();
    
    If(Trigger.IsInsert)
    {
        For(Account act : Trigger.New)
        {
            
            For(Integer I = 1; I < 5; I++)
            {
                Opportunity Opp = New Opportunity();
                
                Opp.AccountId = act.Id;
                Opp.Name      = 'Opp' + ' ' + I;
                Opp.CloseDate = Date.Today() + 30;
                Opp.StageName = 'Prospecting';
                
                oppFinalListToInsert.add(Opp);
            }    
            
        }
    }
    
    try{
        If(!oppFinalListToInsert.IsEmpty()){
            insert oppFinalListToInsert;
        }
    }
    Catch(Exception e){
        System.debug('Thrown Exception for CreatingAnAutoOpp Trigger Is::'  + e.getMessage());
    }
}

Hope it helps and if it solves the query then please mark it as best answer!
 
This was selected as the best answer
Vikram Singh 157Vikram Singh 157
Thanks a lot Mr.Harshil !!!!!!!!!!!