Hi Tejas,Try below code snippet.
Please mark as Best Answer if above information was helpful.Thanks,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;
}
1 risposta