Skip to content

Instantly share code, notes, and snippets.

@durveshshah
Created January 31, 2024 01:02
Show Gist options
  • Save durveshshah/c646f3ccc8003778a5474583b97731fa to your computer and use it in GitHub Desktop.
Save durveshshah/c646f3ccc8003778a5474583b97731fa to your computer and use it in GitHub Desktop.
public with sharing class getOpportunities {
@AuraEnabled
public static List<OpportunityWrapper> getAllOpportunities() {
List<OpportunityWrapper> opportunityWrapperList = new List<OpportunityWrapper>();
List<Opportunity> opportunityList = [SELECT Id,StageName,Name,Owner.Name, AccountId FROM Opportunity LIMIT 5];
for (Opportunity thisOpp : opportunityList) {
OpportunityWrapper wrapper = new OpportunityWrapper();
wrapper.accountId = thisOpp.AccountId;
wrapper.oppOwnerName = thisOpp.Owner.Name;
wrapper.oppName = thisOpp.Name;
wrapper.StageName = thisOpp.StageName;
wrapper.opportunityUrl = '/' + thisOpp.Id;
opportunityWrapperList.add(wrapper);
}
return opportunityWrapperList;
}
@AuraEnabled
public static void copyOwnerNameToAccount(List<OpportunityWrapper> selectedOpportunities) {
Set<Id> accountIds = new Set<Id>();
Map<Id, String> ownerIdToAccountMap = new Map<Id, String>();
// Collect Account Ids and corresponding Owner Names
for (OpportunityWrapper oppWrapper : selectedOpportunities) {
accountIds.add(oppWrapper.accountId);
ownerIdToAccountMap.put(oppWrapper.accountId, oppWrapper.oppOwnerName);
}
// Query and update Accounts
List<Account> accountsToUpdate = [SELECT Id, Opportuntiy_owner_Name__c FROM Account WHERE Id IN :accountIds];
for (Account acc : accountsToUpdate) {
acc.Opportuntiy_owner_Name__c = ownerIdToAccountMap.get(acc.Id);
}
// Update Accounts
if (!accountsToUpdate.isEmpty()) {
update accountsToUpdate;
}
}
public class OpportunityWrapper {
@AuraEnabled
public Id accountId { get; set; }
@AuraEnabled
public String oppOwnerName { get; set; }
@AuraEnabled
public String oppName { get; set; }
@AuraEnabled
public String StageName { get; set; }
@AuraEnabled
public String opportunityUrl { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment