0% found this document useful (0 votes)
2 views19 pages

Scripting Solutions

The document contains a series of Salesforce Apex code solutions for various tasks, including creating accounts, opportunities, and products, as well as performing operations like printing Fibonacci series, reversing numbers, and calculating date differences. Each solution is presented in a structured format with a brief description of the task followed by the corresponding Apex code. The document serves as a guide for developers to implement common functionalities in Salesforce using Apex programming.

Uploaded by

Ira Sharma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views19 pages

Scripting Solutions

The document contains a series of Salesforce Apex code solutions for various tasks, including creating accounts, opportunities, and products, as well as performing operations like printing Fibonacci series, reversing numbers, and calculating date differences. Each solution is presented in a structured format with a brief description of the task followed by the corresponding Apex code. The document serves as a guide for developers to implement common functionalities in Salesforce using Apex programming.

Uploaded by

Ira Sharma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. Create 20 new Accounts Records in salesforce with at least 5 fields filled.

Soln. public static void newAccounts(){


try{
list<account> accList = new list<account>();
for(integer i=1; i<=20; i++){
account acc = new account();
[Link] = 'New Account' + i;
[Link] = 'Hot';
[Link] = 50000000;
[Link] = 'New Account Created';
acc.Active__c = 'Yes';
[Link](acc);
}
if(![Link]()){
insert accList;
}
}
catch(exception e){
[Link]([Link]());
}
}

2. Create 20 Opportunity with Closed Date, Stage and Opportyunity Name.


Soln. public static void newOpportunity(){
try{
list<opportunity> oppList = new list<opportunity>();
for(integer i=1; i<=20; i++){
opportunity opp = new opportunity();
[Link]='Prospecting';
[Link]=[Link]();
[Link]='New Opportunity'+' '+i;
[Link](opp);
}
if(![Link]()){
insert oppList;
}
}
catch(exception e){
[Link]([Link]());
}
}

3. Print the fibonacci series. 1, 1, 2, 3, 5, 8, 13..


Soln. public static List<Integer> fib(Integer num) {
List<Integer> series = new List<Integer>();

try {
if (num == null || num <= 0) {
return series;
}
Integer a = 1;
Integer b = 1;
[Link](a);
[Link](a);

if (num == 1)
return series;

[Link](b);
[Link](b);

for (Integer i = 3; i <= num; i++) {


Integer c = a + b;
[Link](c);
[Link](c);
a = b;
b = c;
}
} catch (Exception e) {
[Link]([Link]());
}
return series;
}

4. Delete all contacts belonging to Accounts Name FIELD having 'A' in them.
Soln. public static void deleteCon(){
try{
list<contact> conList = [Select id,name,[Link] from contact where
[Link] like '%A%'];
if(![Link]()){
delete conList;
}
[Link](conList);
}
catch(Exception e){
[Link]([Link]());
}
}

5. Update all Opportunity with future Closed Date by Opportunity Name=Opportunity Name +
'F'.
Soln. public static void laterOpportunity(){
try{
list<opportunity> oppList=[Select id, name from opportunity where closedate >
today];
for(opportunity opp: oppList){
[Link] = [Link] + ' ' + 'F';
}
if(![Link]()){
update oppList;
}
}
catch(exception e){
[Link]([Link]());
}
}

6. Print any Integer number in reverse order. Ex: 789234 => 432987.
Soln. public static list<integer> reverseNum(integer num){
list<integer> series = new list<integer>();
try{
if (num == null || num <= 0) {
return series;
}
[Link]('The number to be reversed is:' + num);
integer sum = 0;
integer rem = 0;
integer quo = 0;
while (num!=0){
rem = [Link](num,10);
sum = sum*10+rem;
quo=num/10;
num=quo;
}num++;
[Link]('Reverse number is' + sum);
[Link](sum);
}
catch (exception e){
[Link]([Link]());
}
return series;
}

7. Find all the Products having Description field containing 'a' in it.
Soln. public static void findProduct(){
try{
list<product2> prodList = [Select description from product2 where description like '%a
%'];
[Link](prodList);
}
catch(exception e)
{
[Link]([Link]());
}
}

8. Create 10 Products with different Product Names, Description & Family.


Soln. public static void prodChange(){
try{
list<product2> prodList = new list<product2>();
list<string> familyValues = new
list<string>{'abc','xyz','mno','opq','tuv','uvw','def','ghi','jkl','ijk'};
for(integer i=1; i<=10; i++){
product2 prod = new product2();
[Link] = 'Prod' + i;
[Link] = 'New Product' + i;
[Link] = familyValues[i-1];
[Link](prod);
}
if([Link]()){
insert prodList;
}
}
catch(Exception E){
[Link]([Link]());
}
}

9. Print the Account Names in Alphabetical ascending order.


Soln. public static void ascending(){
try{
list<account> accList = [Select name from account order by name];
for(account acc: accList){
[Link]([Link]);
}
}
catch(exception e){
[Link]([Link]());
}
}

10. Print the Account Name in reverse order Ex: Name='Company' => 'ynapmoC'
Soln. public static void revString(){
try{
list<account> accName = [Select name from account];
for(account acc: accName){
string rev = [Link]();
[Link]('Reverse string is for ' + [Link] +' is :' +rev);
}
}
catch(exception e){
[Link]([Link]);
}
}

11. Create 10 Contacts with diferent Account(LookUp) values(Relationship with no same 2


accounts) in them.
Soln. public static void createContact(){
try{
list<account> accList= new list<account>();
for(integer i = 1; i<=10; i++){
account acc = new account();
[Link] = 'Account' + i;
[Link](acc);
}
if(![Link]){
insert accList;
}
list<contact> conList = new list<contact>();
for(Account ac:accList){
Contact con = new Contact();
[Link]='newContact';
[Link]=[Link];
[Link](con);
}
if(![Link]()){
insert conList;
}
}
catch(exception e){
[Link]([Link]());
}
}

12. Create a apex class with a function that show a message "Welcome to Salesforce "
Soln. public class NewAcc {
try{
public static void display(){
[Link](' "Welcome to Salesforce" ');
}
}
catch(Exception e){
[Link]([Link]);
}
}

13. Execute the above function from Apex Class.


Soln. [Link]();

14. Display the current salesforce user details 'Name', 'Number' & 'EmailId'.
Soln. public static void displayCurrent(){
try{
User currentUser = [SELECT firstname, lastname, email,Phone FROM User WHERE Id
= :[Link]()];
String userPhone = [Link];
string userName = [Link] +' '+ [Link];
string useremail = [Link];
[Link]('Name: '+ username);
[Link]('Phone Number: ' + userPhone);
[Link]('Email: '+ useremail);
}
catch(exception e){
[Link]([Link]());
}
}

15. Create New Price book 'Algo Pricebook' and 10 Products with Prices in the Pricebook.
Soln. public static void priceProduct(){
try{
pricebook2 pb =new pricebook2();
[Link] = 'Algo Pricebook';
insert pb;
list<product2> prodList = new list<product2>();
for(integer i=1; i<=10; i++){
product2 p = new product2();
[Link] = 'Product' + i;
[Link](p);
}insert prodList;
pricebook2 pbstd = [Select id from pricebook2 where name like 'Standard Price Book'];
list<pricebookentry> lpbe = new list<pricebookentry>();
if(! [Link]()){
for(product2 p: prodlist){
pricebookentry pe = new pricebookentry();
pe.Product2Id = [Link];
pe.Pricebook2Id = [Link];
[Link] = 8000;
[Link](pe);
}
insert lpbe;
}
list<pricebookentry> lpbes = new list<pricebookentry>();
if(! [Link]()){
for(product2 p: prodlist){
pricebookentry pe = new pricebookentry();
pe.Product2Id = [Link];
pe.Pricebook2Id = [Link];
[Link] = 9000;
[Link](pe);
}
insert lpbes;
}
}
catch(exception e){
[Link]([Link]());
}
}

16. WAP to add two binary nos. '100101' & '0101101' which are in text format.
Soln. public static list<string> add(string a, string b){
list<string> fresult = new list<string>();
try{
string result = '';
integer i = [Link]() - 1;
integer j = [Link]() - 1;
integer carry = 0;
while(i>=0 || j>=0 || carry !=0){
integer sum =carry;
if(i>=0){
sum =sum + [Link]([Link](i, i+1));
i--;
}
if(j>=0){
sum = sum + [Link]([Link](j, j+1));
j--;
}
result = [Link]([Link](sum,2)) + result;
carry = sum / 2;
}
[Link]('result-->'+result);
[Link](result);
}
catch(Exception E){
[Link]([Link]());
}
return fresult;
}

17. WAP to enter a startDate='06:07:55 2-jan-2016' & endDate='07:08:55 5-feb-2016' and


display the difference between two dates in Years, months, days, hours, minutes,seconds.
Soln. public static void date(){
try{
map<string,integer> mnthmap = new
map<string,integer>{'jan'=>1,'feb'=>2,'mar'=>3,'apr'=>4,'may'=>5,'jun'=>6,
'jul'=>7,'aug'=>8,'sep'=>9,'oct'=>10,'nov'=>11,'dec'=>12};
string startDate = '06:07:55 2-jan-2016';
string endDate = '07:08:55 5-feb-2016';
integer year = [Link]([Link](15,19))-
[Link]([Link](15,19));
[Link](year);
integer startmonth = [Link]([Link](11,14));
integer endmonth = [Link]([Link](11,14));
integer month;
if(endmonth<startmonth){
month = (endmonth + 12) - startmonth;
}
else{
month = endmonth - startmonth;
}
[Link](month);
integer day = [Link]([Link](9,10))-
[Link]([Link](9,10));
if(day<0){
day = ([Link]([Link](9,10)) + 30)-
[Link]([Link](9,10));
}
[Link](day);
integer hours = [Link]([Link](0,2))-
[Link]([Link](0,2));
if(hours<0){
hours = ([Link]([Link](0,2)) + 24)-
[Link]([Link](0,2));
}
[Link](hours);
integer min = [Link]([Link](3,5))-
[Link]([Link](3,5));
if(min<0){
min = ([Link]([Link](3,5)) + 60)-
[Link]([Link](3,5));
}
[Link](min);
integer sec = [Link]([Link](6,8))-
[Link]([Link](6,8));
if(sec<0){
sec = ([Link]([Link](6,8)) + 60)-
[Link]([Link](6,8));
}
[Link](sec);
}
catch(exception e){
[Link]('Exception:'+[Link]());
}
}
18. Write a program in Salesforce to create another System admin user in your dev org with
your email. Register the user manually.
Soln. public static void createUser(){
try{
profile p = [Select id from profile where name='System Administrator' limit 1];
user nUser = new user(FirstName = 'Arvind', LastName = 'Sharma',
Email='irasharma1291@[Link]',

UserName='arvdsh@[Link]',Alias='ash',CommunityNickName='asharma',
TimeZoneSidkey='America/New_York',LocaleSidkey='en_US',
EmailEncodingKey='UTf-8',LanguageLocaleKey='en_US',ProfileId=[Link]);
insert nUser;
[Link]('User created with id:' +[Link]);
}
catch(exception e){
[Link]('Encoding:'+[Link]());
}
}

19. WAP to create 5 Case Records with all the values(Account, Contact, etc) filled in.
[Link] static void caseCreation(){
try{
list<Case> caseList = new list<Case>();
account ac = new account();
[Link] = 'New Account case';
insert ac;
contact cn = new contact();
[Link] = 'New Contact case';
insert cn;
for(integer i=1; i<=5; i++){
case c = new case();
[Link] = [Link];
[Link] = [Link];
[Link] = 'Working';
[Link] = 'Medium';
[Link] = 'Web';
[Link](c);
}
if(![Link]()){
insert caseList;
}
}
catch (exception e){
[Link]('Exception:' +[Link]());
}
}
20. Create a new multi picklist field In Lead Object "Records" [Buyer, Seller, Tenant ,
Landlord]. Write script to fetch all the Sellers and Tenant which are entered this year.
Soln. public static void leadRecords(){
try{
list<Lead> lList = [Select id,records__c from lead where records__c in
('Seller','Tenant') and createdDate = THIS_YEAR];
[Link](lList);
}
catch(Exception e){
[Link]([Link]());
}
}

[Link] an Object "Event" (Name Default field and "Event Date" Date time field). Another
Junction Object "Event Participant" with 3 fields 1. lookup Contact 2. lookup Event 3.
multipicklist ("Attendee Contact", "Presenter Contact","Organizer Contact") Write a script to
create 10 "Event Participant" with Attendee picklist, 4 with Presenter, 2 with organizer.
Soln. public static void EventParticipants(){
try{
event__c e = new event__c();
[Link] = 'New Event';
e.event_date__c = [Link]();
insert e;

contact con = [Select id from contact limit 1];

list<Event_Participants__c> epList = new list<Event_Participants__c>();


for(integer i=1;i<=16;i++){
Event_Participants__c ep= new Event_Participants__c();
[Link] = 'Participant';
ep.event__c = [Link];
ep.contact__c = [Link];
if(i<=10){
ep.Multipicklist__c = 'Attendee Contact';
}
else if(i>10 && i<=14){
ep.Multipicklist__c = 'Presenter Contact';
}
else{
ep.Multipicklist__c = 'Organiser Contact';
}
[Link](ep);
}
if(![Link]()){
insert epList;
}
}
catch(exception e){
[Link]([Link]());
}
}

22. Create several Accounts and Opportunities and products. Write a Script to get all the
Accounts having more than 2 Opportunities with Closed Won.
Soln. public static void getOpportunity(){
try{
list<aggregateResult> oppList = [SELECT AccountId, COUNT(Id)FROM Opportunity
WHERE StageName = 'Closed Won'
GROUP BY AccountId HAVING COUNT(Id) > 2];
[Link](opplist);
}
catch(Exception e){
[Link]([Link]());
}
}

23. Write a script to get all the Account having more than 10 opportunities whose status is
closed won and the close date is between one month.
Soln. public static void getAccounts(){
try{
list<opportunity> opList = new list<opportunity>();
account acc = new account();
[Link] = 'oAccount';
insert acc;

for(integer i=1;i<=10;i++){
opportunity opp = new opportunity();
[Link] = [Link](i);
[Link] = 'Closed Won';
[Link] = [Link]() +5;
[Link] = [Link];
[Link](opp);
}
if(![Link]()){
insert opList;
}
list<aggregateResult> oppList = [SELECT AccountId, COUNT(Id)FROM Opportunity
WHERE StageName = 'Closed Won' and CloseDate >= LAST_N_DAYS:30
GROUP BY AccountId HAVING COUNT(Id) >= 10];
[Link](opplist);
}
catch(Exception e){
[Link]([Link]());
}
}

24. Write a Script to get all the Contacts having same email as any Salesforce [Link]
Soln. public static void eMail1(){
try{
User currentUser = [SELECT Email FROM User WHERE Id = :[Link]()];
String userEmail = [Link];
List<Contact> matchingContacts = [SELECT Id, Name, Email FROM Contact
WHERE Email = :userEmail];
[Link](matchingContacts);

}
catch(Exception e){
[Link]('Exception' +[Link]());
}
}

25. Write a Script to get all the Accounts having more than 2 Opportunity Line Items in their
Opportunity.
Soln. public static void oliCount(){
try{
Set<Id> oppIds = new Set<Id>();
list<aggregateresult> aggRes = [SELECT OpportunityId,[Link]
AccId, COUNT(Id) FROM OpportunityLineItem GROUP BY
OpportunityId,[Link] HAVING COUNT(Id) > 2];
for(AggregateResult ar :aggres) {
[Link]((Id)[Link]('AccId'));
}
List<Account> accList = [Select id, name from account where id in:oppIds];
for (Account acc : accList) {
[Link]('Account Name: ' + [Link]);
}
}
catch(exception e){
[Link]([Link]());
}
}

26. In Account Object create a Multi picklist "Working in (ASIA, EMA, NA, SA)". Write a script
to get the total "No of employees" of all the Accounts working in ASIA and NA(North
America)
Soln. public static void total_employees(){
try{
Integer SUM = 0;
List<Account> accList = [SELECT NumberOfEmployees FROM
Account WHERE Working_in__c INCLUDES ('ASIA','NA')];
for(Account acc : accList){
SUM = SUM + [Link]([Link]);
}
[Link]('Total Number of Employees (ASIA + NA): ' + SUM);

}
catch(Exception e){
[Link]('Exception:' + e);
}
}

27. "Create a new Product "Gandhiji Chasma". Add Price as 100$ in standard price book
and any other price books as well. Create few oppportunites with Opportunity
line item "Gandhiji Chasma". Write a script to get the total Price of all the Opportunity sold
having Product "Gandhiji Chasma"
soln. public static void new1(){
try{
decimal totalAmount = 0;
List<OpportunityLineItem> oliList = [SELECT Quantity, UnitPrice FROM
OpportunityLineItem
WHERE [Link] like 'Gandhiji%'];
for (OpportunityLineItem oli : oliList) {
totalAmount = totalAmount + ([Link] * [Link]);
}
[Link]('Total Price of all Opportunities sold for Gandhiji Chashma: ' +
totalAmount);
}
catch(Exception e){
[Link]([Link]());
}
}

28. Write a script to send Email to all users which are having more than 100 Account
(Having phone number) and 30 contacts(Having email address).
Soln. public static void emailnew(){
try{
Set<Id> UserIds = new Set<Id>();
list<aggregateResult> aggRes = [SELECT OwnerId, COUNT(Id) accCount FROM
Account WHERE Phone != null GROUP BY
OwnerId HAVING COUNT(Id) > 100];
for (AggregateResult ar : aggRes) {
[Link]((Id)[Link]('OwnerId'));
}
[Link](userIds);
Set<Id> finalUsers = new Set<id>();
list<aggregateResult> faggRes = [SELECT OwnerId, COUNT(Id) conCount FROM
Contact WHERE Email != null AND OwnerId
IN :UserIds GROUP BY OwnerId HAVING COUNT(Id) > 30];
for (AggregateResult ar :faggRes ) {
[Link]((Id)[Link]('OwnerId'));
}

List<User> EmailUsers = [SELECT Id, Name, Email FROM User WHERE Id


IN :finalUsers AND Email != null];
list<string> recipientList = new list<string>();
for(user u:EmailUsers){
[Link]([Link]);
}
List<[Link]> emailList = new
List<[Link]>();
[Link] mail = new [Link]();
[Link](recipientList);
[Link]('Performance Update');
[Link]('Hello' + 'You currently own:\n' +'- More than 100 Accounts with
phone numbers\n' +
'- More than 30 Contacts with email addresses\n\n' +'Regards');
[Link](mail);

if (![Link]()) {
[Link](emailList);
}
}
catch(exception e){
[Link]([Link]());
}
}

29. Write a script to Send opportunity details to the Account's Contact's email address
whose close date is 10 days later.
Soln. public static void emailSend(){
try{
List<Opportunity> oppList = [SELECT Id, Name, Amount, CloseDate, AccountId
FROM Opportunity WHERE CloseDate >=next_n_days :10
AND AccountId != null];
List<[Link]> emailList = new
List<[Link]>();
for (Opportunity opp : oppList) {
List<Contact> contactList = [SELECT Id, Name, Email FROM Contact WHERE
AccountId = :[Link] AND Email != null];

for (Contact con : contactList) {


[Link] mail = new [Link]();
[Link](new List<String>{ [Link] });
[Link]('Opportunity Closing in 10 Days');
[Link]('Hello ' + [Link] + ',\n\n' +'Opportunity Details:\n'
+'Name: ' + [Link] + '\n' +'Amount: ' + [Link] + '\n' +
'Close Date: ' + [Link] + '\n\n' +'Regards');
[Link](mail);
}
}
if (![Link]()) {
[Link](emailList);
}
}
catch(exception e){
[Link]([Link]());
}
}

30. Create a Script to find out all the users in the systems who are having more than 20
Leads allocated[Owner] to them in month of Dec 2017
Soln. public static void leadSelection(){
try{
List<Lead> leadList = [SELECT Id, OwnerId FROM Lead WHERE CreatedDate >=
2017-12-01T00:00:00Z AND CreatedDate <= 2018-01-01T00:00:00Z];
List<Id> userIds = new List<Id>();
List<Integer> leadCounts = new List<Integer>();
for (Lead l : leadList) {
Integer index = [Link]([Link]);
if (index == -1) {
[Link]([Link]);
[Link](1);
}
else {
leadCounts[index] = leadCounts[index] + 1;
}
}
List<User> resultUsers = new List<User>();
for (Integer i = 0; i < [Link](); i++) {
if (leadCounts[i] > 20) {
[Link](new User(Id = userIds[i]));
}
}
List<User> users = [SELECT Id, Name, Email FROM User WHERE Id
IN :resultUsers];
for (User u : users) {
[Link]('User: ' + [Link]);
}
}
catch(Exception e){
[Link]([Link]());
}
}

31. Create a look up[Lead] on product so that Products come over in related list of a Lead.
Write script to create 3 Leads and 5 Products with Lead lookup field.
[Link] static void LeadProductCreation(){
try{
List<Product2> prodList = new List<Product2>();
for (Integer i = 1; i <= 5; i++) {
product2 prod = new product2();
[Link] = 'Product ' + i;
[Link] = true;
[Link](prod);
}
if(![Link]()){
insert prodList;
}
List<Lead> leadList = new List<Lead>();
for (Integer i = 0; i < 3; i++) {
lead l = new lead();
[Link] = 'Lead';
[Link] = 'User ' + (i + 1);
[Link] = 'LeadCo. ' + (i + 1);
[Link] = 'Open - Not Contacted';
l.product__c = prodList[i].Id;
[Link](l);
}
if(![Link]()){
insert leadList;
}
[Link]('Products and Leads created successfully');
}
catch(Exception e){
[Link]('Exception' + e);
}
}

32. Write a Script to get all the Accounts having Oppotunity Line Items Quantity > 100 under
their Opportunities(CLOSED WON only).
Soln. public static void oliCheck(){
try{
Set<Id> accIds = new Set<Id>();
List<OpportunityLineItem> oliList = [SELECT [Link] FROM
OpportunityLineItem
WHERE Quantity > 100 AND [Link] =
'Closed Won'];
for (OpportunityLineItem oli : oliList) {
if ([Link] != null) {
[Link]([Link]);
}
}
List<Account> accList = [SELECT Id, Name FROM Account WHERE Id IN :accIds];
for (Account acc : accList) {
[Link]('Account: ' + [Link]);
}
}
catch(Exception e){
[Link]([Link]());
}
}

[Link] a code to clone(copy ) 1 Opportunity with all Opportunity line items into a new
Opportunity with Closed Date = Old closed date + 30 days & Opportunity name =
Opportunity Name+ 'Clone'.
Soln.

34. Manually add 2 (jpg and PDF) attachments under Accounts related list. WAP to copy the
attachments to Contact associated(parent child) with Account.
Soln. public static void linkAccFiles(){
try{
List<ContentDocumentLink> accFiles = [SELECT ContentDocumentId,
LinkedEntityId FROM ContentDocumentLink
WHERE LinkedEntityId IN (SELECT Id FROM Account)];
Map<Id, List<Id>> accToDocsMap = new Map<Id, List<Id>>();
for (ContentDocumentLink cdl : accFiles) {
if (![Link]([Link])) {
[Link]([Link], new List<Id>());
}
[Link]([Link]).add([Link]);
}
List<Contact> contacts = [SELECT Id, AccountId FROM Contact WHERE AccountId
IN :[Link]()];
List<ContentDocumentLink> newLinks = new List<ContentDocumentLink>();
for (Contact con : contacts) {
List<Id> docIds = [Link]([Link]);
for (Id docId : docIds) {
ContentDocumentLink cdl = new ContentDocumentLink();
[Link] = docId;
[Link] = [Link];
[Link] = 'V';
[Link] = 'AllUsers';
[Link](cdl);
}
}
if (![Link]()) {
insert newLinks;
}
[Link]('Files copied from Account to related Contacts successfully');
}
catch(Exception e){
[Link]([Link]());
}
}

35. Write a script to get all Account those are associated with opportunity and put the
attachment from account to their opportunity. If the account does not have attachment put
opportunity close loss otherwise close won.
soln. public static void linkOpp(){
try{
List<Opportunity> oppList = [SELECT Id, StageName, AccountId FROM Opportunity
WHERE AccountId != NULL];
Set<Id> accountIds = new Set<Id>();
for (Opportunity opp : oppList) {
[Link]([Link]);
}
List<ContentDocumentLink> accFileLinks = [SELECT ContentDocumentId,
LinkedEntityId FROM ContentDocumentLink
WHERE LinkedEntityId IN :accountIds];
Map<Id, List<Id>> accToFileMap = new Map<Id, List<Id>>();
for (ContentDocumentLink cdl : accFileLinks) {
if (![Link]([Link])) {
[Link]([Link], new List<Id>());
}
[Link]([Link]).add([Link]);
}
List<ContentDocumentLink> oppFileLinks = new List<ContentDocumentLink>();
List<Opportunity> oppsToUpdate = new List<Opportunity>();
for (Opportunity opp : oppList) {
if ([Link]([Link])) {
for (Id docId : [Link]([Link])) {
ContentDocumentLink cdl = new ContentDocumentLink();
[Link] = docId;
[Link] = [Link];
[Link] = 'V';
[Link] = 'AllUsers';
[Link](cdl);
}
[Link] = 'Closed Won';
[Link](opp);
}
else {
[Link] = 'Closed Lost';
[Link](opp);
}
}
if (![Link]()) {
insert oppFileLinks;
}
if (![Link]()) {
update oppsToUpdate;
}
[Link]('Process completed successfully');
}
catch(Exception e){
[Link]([Link]());
}
}

You might also like