• Raj Pandey 29
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
create a checkbox on contact field 

condition 1: If contact is not associated with any account then create an account

conditiopn 2: If contact is associated with an account then check weather a opportunity is related if not create an opportunity.
trigger onlyoneaccountopportunitycreate on Opportunity (before insert) {

    Set<id> accId = new Set<id>(); // Always try to use set
    for(opportunity opp : trigger.new)
    {
        if(opp.AccountId != null)
        {
            accId.add(opp.AccountId);    
        }
        }
    

    Map<Id,Account> mapAccount = new Map<Id,Account> ( [select id,(select id from opportunities) from account where id IN : accId ]) ;
    for(opportunity opp:trigger.new)
    {    
        
        if( opp.AccountId != null && mapAccount.containsKey(opp.AccountId) )
        {
            Account acc = mapAccount.get(opp.AccountId);
            if(acc.opportunities.size() > 0 )
            {
                opp.addError('you can not add more then once opportunity for this account'); 
            }
        }
    }

}

Please see the above code & help me to write a trigger that allow only one opportunity associated with an account in a day.
trigger onlyoneaccountopportunitycreate on Opportunity (before insert) {

    Set<id> accId = new Set<id>(); // Always try to use set
    for(opportunity opp : trigger.new)
    {
        if(opp.AccountId != null)
        {
            accId.add(opp.AccountId);    
        }
        }
    

    Map<Id,Account> mapAccount = new Map<Id,Account> ( [select id,(select id from opportunities) from account where id IN : accId ]) ;
    for(opportunity opp:trigger.new)
    {    
        
        if( opp.AccountId != null && mapAccount.containsKey(opp.AccountId) )
        {
            Account acc = mapAccount.get(opp.AccountId);
            if(acc.opportunities.size() > 0 )
            {
                opp.addError('you can not add more then once opportunity for this account'); 
            }
        }
    }

}

Please see the above code & help me to write a trigger that allow only one opportunity associated with an account in a day.