• Amit Karlekar 4
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 2
    Replies

Is there any way to delete or abort the specific queued scheduled apex job?

The following code aborts all the scheduled apex jobs, but, I want to abort the specific one.

List<CronTrigger> JOBIDLIST = new List<CronTrigger>();
JOBIDLIST = [select id from CronTrigger];
for(CronTrigger job:JOBIDLIST )
{
   System.abortJob(job.id);
}
/*Future Method 3: Upon Account record creation if the industry has the value as 'Media' or 'Enery' then populate the Rating as 'Hot'.

- Create Class With Fututre Method
- Call it from an anonymous window
- Create a Test Class*/

 
public class FutureMethodPopulateRatingAsHot {

    @future
    public static void populateRaingAsHot(List<Id> accIds) {
        
        List<Account> accRecords = [SELECT Id, Industry, Rating
                                   FROM Account WHERE (Industry = 'Media'
                                   OR Industry = 'Energy') AND Rating != 'Hot'];
        
        if(!accRecords.isEmpty()) {
            for(Account acc : accRecords) {
                acc.Rating = 'Hot';
            }
        }
        update accRecords;
    }
}
---------------------------------------------------------------------------------------------

For the above future method, the test class I wrote is giving only 66% code coverage.

I tried a lot, but not able to get maximum code coverage. Can somebody please help?

 
@isTest
private class FutureMethodPopulateRatingAsHotTest {
    
    @isTest
    static void populateRaingAsHotTest() {
        
        List<Account> accList = new List<Account>();
        
        for(Integer i=1; i<=100; i++) {
            Account acc = new Account();
            acc.Name= 'Test Name' + i;
            acc.Industry = 'Energy';
            acc.Rating = 'Warm';
            
            accList.add(acc);
            
            Account acc2 = new Account();
            acc2.Name = 'Test Name2 ' + i;
            acc2.Industry = 'Media';
            acc.Rating = 'Warm';
            
            accList.add(acc2);
        }
        Test.startTest();
        insert accList;
        
        List<Id> accIds = new List<Id>();
        
        for(Account acc : accList) {
            if(acc.Industry == 'Energy' || acc.Industry == 'Media') {
                acc.Rating = 'Hot';
                accIds.add(acc.Id);
            }
        }
        update accList;
        
        FutureMethodPopulateRatingAsHot.populateRaingAsHot(accIds);
        Test.stopTest();
        
        List<Account> accRecords = [SELECT Id, Industry, Rating
                                    FROM Account WHERE Id =: accList[0].Id];
        
        System.assertEquals('Hot', accRecords[0].Rating, 'Rating Not Updated');
    }
}

 

I have been trying for the following requirements but have not succeeded. Please give it a try and let me know if it works for you. I will appreciate 👍

Write a trigger and handler class for the following scenario:

Create a field called the "Contact Relationship" checkbox on the contact object and create the object called "Contact Relationship" which is a related list to the Contacts (Lookup Relationship). Create a record.

When we change the owner of the Contact Relationship(Custom Object), the new(updated) owner name will be automatically populated in the Contact Relationship Name Field.

Trigger Scenario:
Write a Trigger on the Account and Check Only Student Admin Profile Users Should Be Able to Delete an Account.

For the above scenario, I have written the following two types of handler classes.

Type 1: It Shows the Custom Error
public class PracticeHandlerClass {

 

    public static void practiceMethod(List<Account> accList) {

            for(Account acc : accList) {

                if(UserInfo.getProfileId() != 'Student Admin') {

                acc.addError('User Not Allowed To Delete The Record');

            }

        }

    }

}

-----------------------------------

Type 2: It Doesn't Show the Custom Error

public class PracticeHandlerClass {

    

    public static void practiceMethod(List<Account> accList) {

        Profile userProfile = [SELECT Id, Name FROM Profile WHERE Name = 'Student Administrator'];

        

        if(userProfile != null && UserInfo.getProfileId() != userProfile.Id) {

            for(Account acc : accList) {

                acc.addError('User Not Allowed To Delete The Record', 'CustomError');

            }

        }

    }

}

Even the 2nd type shows the error but, not the custom error message. I want to understand why?

What would be the correct code that will display the custom error message?

Trigger Scenario:
Write a Trigger on Contact Which Will Create an Account Record Whenever Contact Is Created Without an Account.

Trigger:
trigger CreateAccountOnContactCreationTrigger on Contact (before insert, after insert) {

    if(Trigger.isAfter) {
        if(Trigger.isInsert) {
            CreateAccountOnContactCreationHandler.immediatelyCreateAccount(Trigger.new);
        }
    }
}

Handler Class:
public class CreateAccountOnContactCreationHandler {
    
    public static void immediatelyCreateAccount(List<Contact> conList) {
        
        List<Account> accList = new List<Account>();
        
        for(Contact con : conList) {
            if(con.AccountId == null) {
                Account acc = new Account();
                acc.Name = 'Trigger Account 38';
                
                accList.add(acc);
                con.AccountId = acc.Id ;
            }
        }
        if(!accList.isEmpty()) {
            insert accList;
        }
    }
}

For the mentioned trigger scenario above, I have written the provided trigger and handler. Contact is getting created, but not the Account.

kindly tell me what caused the error? (I have even tried it with both Before and After Insert)

At the age of 35, I decided to go into the IT industry. Started learning Salesforce Development after the admin. But, in this process, I am finding so many difficulties, especially in the trigger, as I am from a non-IT background.

I would like to hear about the experience from the experienced folks working in Salesforce Development. Especially, people like me, who were from non-IT backgrounds, but now are working without any problem.

Would you please be kind enough to guide me on how do I improve my salesforce development coding and how to improve the logic building?

I have the following requirement of screen worflow:

" Take Custom Name(Text), Feedback Date(date) and Feedback(Picklist) as input from user. Feedback should be a picklist containing 'Happy', 'Not Happy' and 'May Be' as values. If the user selects Happy then create an opportunity, if Not Happy then create a case, if May be then is entered then display a message "We will do our best to improve the services". At the display a message saying "Thanks for providing your valuable feedback" no matter whatever the feedback the user provides. "

how do I create the customer picklist options in the picklist component? I tried but the picklist options are coming in one line.

as a begnner, I am getting so many errors. Solution would be really helpful.

Do we need to know LWC & AURA to learn wrapper class? and What is the best practice to learn wrapper class as a beginner?

I would appreciate help with the following trigger question:


Write a trigger, Whenever an Opportunity is Created, Updated, Deleted, or Undeleted then Roll-Up the Opportunity amount to Account's Annual Revenue.

I have tried it with the handler class. I have just begun learning triggers, so I make a lot of silly mistakes. Any small help/guidance you can give will be helpful.

Trigger/Handle I wrote is:

 

Trigger: trigger PopulateOppAmntToAccAmnt on Opportunity (before insert) {

    if(Trigger.isInsert) {
        if(Trigger.isAfter) {
            CopyOppAmntToAccAmnt.populateAccAmnt(Trigger.new);
        }
    }
    
    if(Trigger.isDelete) {
        if(Trigger.isAfter) {
            CopyOppAmntToAccAmnt.populateAccAmnt(Trigger.old);
        }
    }
    
    if(Trigger.isUndelete) {
        if(Trigger.isAfter) {
            CopyOppAmntToAccAmnt.populateAccAmnt(Trigger.new);
        }
    }
    
    if(Trigger.isUpdate) {
        if(Trigger.isAfter) {
            CopyOppAmntToAccAmnt.populateAccAmnt(Trigger.new);
        }
    }  
}

 

Handle Class:
public class CopyOppAmntToAccAmnt {

    public static void populateAccAmnt(List<Opportunity> oppList) {
        
        Set<Id> accId = new Set<Id>();
        List<Account> accList = new List<Account>();
        
        for(Opportunity opp : oppList) {
            if(opp.AccountId != null) {
                accId.add(opp.AccountId);
            }
        }
        
        for(Account acc : [SELECT Id, Name, AnnualRevenue,
                          (SELECT Id, Name, Amount FROM Opportunities)
                          FROM Account WHERE Id IN: accId]) {
            acc.AnnualRevenue = acc.Opportunities.Amount;
                              accList.add(acc);
        }
    }
}

/*Future Method 3: Upon Account record creation if the industry has the value as 'Media' or 'Enery' then populate the Rating as 'Hot'.

- Create Class With Fututre Method
- Call it from an anonymous window
- Create a Test Class*/

 
public class FutureMethodPopulateRatingAsHot {

    @future
    public static void populateRaingAsHot(List<Id> accIds) {
        
        List<Account> accRecords = [SELECT Id, Industry, Rating
                                   FROM Account WHERE (Industry = 'Media'
                                   OR Industry = 'Energy') AND Rating != 'Hot'];
        
        if(!accRecords.isEmpty()) {
            for(Account acc : accRecords) {
                acc.Rating = 'Hot';
            }
        }
        update accRecords;
    }
}
---------------------------------------------------------------------------------------------

For the above future method, the test class I wrote is giving only 66% code coverage.

I tried a lot, but not able to get maximum code coverage. Can somebody please help?

 
@isTest
private class FutureMethodPopulateRatingAsHotTest {
    
    @isTest
    static void populateRaingAsHotTest() {
        
        List<Account> accList = new List<Account>();
        
        for(Integer i=1; i<=100; i++) {
            Account acc = new Account();
            acc.Name= 'Test Name' + i;
            acc.Industry = 'Energy';
            acc.Rating = 'Warm';
            
            accList.add(acc);
            
            Account acc2 = new Account();
            acc2.Name = 'Test Name2 ' + i;
            acc2.Industry = 'Media';
            acc.Rating = 'Warm';
            
            accList.add(acc2);
        }
        Test.startTest();
        insert accList;
        
        List<Id> accIds = new List<Id>();
        
        for(Account acc : accList) {
            if(acc.Industry == 'Energy' || acc.Industry == 'Media') {
                acc.Rating = 'Hot';
                accIds.add(acc.Id);
            }
        }
        update accList;
        
        FutureMethodPopulateRatingAsHot.populateRaingAsHot(accIds);
        Test.stopTest();
        
        List<Account> accRecords = [SELECT Id, Industry, Rating
                                    FROM Account WHERE Id =: accList[0].Id];
        
        System.assertEquals('Hot', accRecords[0].Rating, 'Rating Not Updated');
    }
}

 
Trigger Scenario:
Write a Trigger on Contact Which Will Create an Account Record Whenever Contact Is Created Without an Account.

Trigger:
trigger CreateAccountOnContactCreationTrigger on Contact (before insert, after insert) {

    if(Trigger.isAfter) {
        if(Trigger.isInsert) {
            CreateAccountOnContactCreationHandler.immediatelyCreateAccount(Trigger.new);
        }
    }
}

Handler Class:
public class CreateAccountOnContactCreationHandler {
    
    public static void immediatelyCreateAccount(List<Contact> conList) {
        
        List<Account> accList = new List<Account>();
        
        for(Contact con : conList) {
            if(con.AccountId == null) {
                Account acc = new Account();
                acc.Name = 'Trigger Account 38';
                
                accList.add(acc);
                con.AccountId = acc.Id ;
            }
        }
        if(!accList.isEmpty()) {
            insert accList;
        }
    }
}

For the mentioned trigger scenario above, I have written the provided trigger and handler. Contact is getting created, but not the Account.

kindly tell me what caused the error? (I have even tried it with both Before and After Insert)