Skip to main content
added 3 characters in body
Source Link
gNerb
  • 9.6k
  • 5
  • 27
  • 58

My best guess is that you want to make sure that no account has more than 4 opportunities that were created via leads that came from the web.

Disclaimer: the following is intentionally not copy/paste code and meant as a guide to get you started. Use it as a guide and if you run into any specific issues you can ask another question for more assistance.

trigger oppTriggerTest on Opportunity (before update, before insert) {
    // FIrst step is to get account ids
    Set<id> accountIds = new Set<Id>();

    for (Opportunity opp : trigger.new) {
        if (opp.leadSource = 'Web') { // Make sure we only look at opps that we care about
            accountIds.add(opp.Id);
        }
    }

    // Let's make the database do as much of the work as possible
    List<AggregateResult> ars = [
        SELECT Count(Id) numOfWeb, AccountId
        FROM Opportunity
        WHERE AccountId IN :accountIds
            And LeadSource = 'Web'
        GROUP BY AccountId
        HAVING Count(Id) >= 4
    ];

    // Lets make a map that's easier to work with
    Map<Id, Integer> accountToCount = new Map<id, Integer>();

    for (AggregateResult ar : ars) {
        accountToCount.put(ar.get('AccountId'), ar.get('numOfWeb'));
    }

    // Now, lets add an error to opps that violate the rule
    for (Opportunity opp : trigger.new) {
        if (accountToCount.getcontainsKey(opp.accountId) >= 4) {
            opp.addError('Too many web opps for the given account');
        }
    }
}

My best guess is that you want to make sure that no account has more than 4 opportunities that were created via leads that came from the web.

Disclaimer: the following is intentionally not copy/paste code and meant as a guide to get you started. Use it as a guide and if you run into any specific issues you can ask another question for more assistance.

trigger oppTriggerTest on Opportunity (before update, before insert) {
    // FIrst step is to get account ids
    Set<id> accountIds = new Set<Id>();

    for (Opportunity opp : trigger.new) {
        if (opp.leadSource = 'Web') { // Make sure we only look at opps that we care about
            accountIds.add(opp.Id);
        }
    }

    // Let's make the database do as much of the work as possible
    List<AggregateResult> ars = [
        SELECT Count(Id) numOfWeb, AccountId
        FROM Opportunity
        WHERE AccountId IN :accountIds
            And LeadSource = 'Web'
        GROUP BY AccountId
        HAVING Count(Id) >= 4
    ];

    // Lets make a map that's easier to work with
    Map<Id, Integer> accountToCount = new Map<id, Integer>();

    for (AggregateResult ar : ars) {
        accountToCount.put(ar.get('AccountId'), ar.get('numOfWeb'));
    }

    // Now, lets add an error to opps that violate the rule
    for (Opportunity opp : trigger.new) {
        if (accountToCount.get(opp.accountId) >= 4) {
            opp.addError('Too many web opps for the given account');
        }
    }
}

My best guess is that you want to make sure that no account has more than 4 opportunities that were created via leads that came from the web.

Disclaimer: the following is intentionally not copy/paste code and meant as a guide to get you started. Use it as a guide and if you run into any specific issues you can ask another question for more assistance.

trigger oppTriggerTest on Opportunity (before update, before insert) {
    // FIrst step is to get account ids
    Set<id> accountIds = new Set<Id>();

    for (Opportunity opp : trigger.new) {
        if (opp.leadSource = 'Web') { // Make sure we only look at opps that we care about
            accountIds.add(opp.Id);
        }
    }

    // Let's make the database do as much of the work as possible
    List<AggregateResult> ars = [
        SELECT Count(Id) numOfWeb, AccountId
        FROM Opportunity
        WHERE AccountId IN :accountIds
            And LeadSource = 'Web'
        GROUP BY AccountId
        HAVING Count(Id) >= 4
    ];

    // Lets make a map that's easier to work with
    Map<Id, Integer> accountToCount = new Map<id, Integer>();

    for (AggregateResult ar : ars) {
        accountToCount.put(ar.get('AccountId'), ar.get('numOfWeb'));
    }

    // Now, lets add an error to opps that violate the rule
    for (Opportunity opp : trigger.new) {
        if (accountToCount.containsKey(opp.accountId)) {
            opp.addError('Too many web opps for the given account');
        }
    }
}
Source Link
gNerb
  • 9.6k
  • 5
  • 27
  • 58

My best guess is that you want to make sure that no account has more than 4 opportunities that were created via leads that came from the web.

Disclaimer: the following is intentionally not copy/paste code and meant as a guide to get you started. Use it as a guide and if you run into any specific issues you can ask another question for more assistance.

trigger oppTriggerTest on Opportunity (before update, before insert) {
    // FIrst step is to get account ids
    Set<id> accountIds = new Set<Id>();

    for (Opportunity opp : trigger.new) {
        if (opp.leadSource = 'Web') { // Make sure we only look at opps that we care about
            accountIds.add(opp.Id);
        }
    }

    // Let's make the database do as much of the work as possible
    List<AggregateResult> ars = [
        SELECT Count(Id) numOfWeb, AccountId
        FROM Opportunity
        WHERE AccountId IN :accountIds
            And LeadSource = 'Web'
        GROUP BY AccountId
        HAVING Count(Id) >= 4
    ];

    // Lets make a map that's easier to work with
    Map<Id, Integer> accountToCount = new Map<id, Integer>();

    for (AggregateResult ar : ars) {
        accountToCount.put(ar.get('AccountId'), ar.get('numOfWeb'));
    }

    // Now, lets add an error to opps that violate the rule
    for (Opportunity opp : trigger.new) {
        if (accountToCount.get(opp.accountId) >= 4) {
            opp.addError('Too many web opps for the given account');
        }
    }
}