Skip to main content
1 risposta
  1. 24 dic 2022, 17:29
    Hi Tejas,

    Try below code snippet.

     

    trigger oneopp on Account (after update) {

    Opportunity[] newOpportunities = new Opportunity[0];

    Account[] potentialAccounts = new Account[0];

    for(Account record: Trigger.new) {

    if(Trigger.oldMap.get(record.Id).AnnualRevenue != record.AnnualRevenue) {

    potentialAccounts.add(record);

    }

    }

    if(potentialAccounts.isEmpty()) {

    return;

    }

    Set<Id> accountIdsWithOpps = new Map<Id, AggregateResult>([

    SELECT COUNT(Id), AccountId Id

    FROM Opportunity

    WHERE AccountId = :potentialAccounts

    GROUP BY AccountId

    ]).keySet();

    for(Account record: potentialAccounts) {

    if(!accountIdsWithOpps.contains(record.Id)) {

    newOpportunities.add(

    new Opportunity(Name='dummyopp '+record.Name, AccountId = record.Id, CloseDate=Date.Today(), StageName='Closed Lost', Amount=record.AnnualRevenue)

    );

    }

    }

    insert newOpportunities;

    }

    Please mark as Best Answer if above information was helpful.

    Thanks,
0/9000