0

I wish to update a field from one custom object (Career Path) to another object (Contact details) based on whether a checkbox (Current) is true.

Essentially, it updates the Contacts details with the job that the user has filled in on the Career Path if it is the current job title that the individual holds.

Someone assisted me in the Success Salesforce forum with an Apex Class and Trigger that worked successfully in the Sandbox. However, I am having trouble deploying it in Production as it says I need 75% code coverage - could someone assist me in creating test classes for this code so that it deploys?

Any help would be greatly appreciated as I need this functionality to work for my staff, it's a priority and SF experts haven't been much help.

Apex trigger:

trigger Update_Current_job_Title_onContact on Career_Path__c (After insert, After update) {

    if((trigger.isInsert || trigger.isUpdate) && trigger.isAfter)
    {
        Update_Current_Job_Title.WhenInsertNewCareerPathOrUpdate(trigger.new);
    }


}

Apex class:

public class Update_Current_Job_Title {


    public static void WhenInsertNewCareerPathOrUpdate(List<Career_Path__c> careerPathListNew)
    {
        try{
        List<Career_Path__c> careerPathList = new  List<Career_Path__c>();
        List<contact> contactList = new List<contact>();
         List<contact> contactListUpdated = new List<contact>();
        set<id> IdList = new set<Id>();
        careerPathList = [select id, contact__c , Job_title_2__c,Current__c from Career_Path__c where id in:careerPathListNew
                         and Job_title_2__c !=null];


            for(Career_Path__c cp :careerPathList )
        {
            IdList.add(cp.contact__c);
        }

        contactList = [select id, Current_Job_Title__c from contact where id in : IdList];

         for(Career_Path__c cpp :careerPathList )
        {
            for(contact cc :contactList )
            {
               if(cpp.Contact__c == cc.id && cpp.Current__c==true)
               {
                   cc.Current_Job_Title__c = cpp.Job_Title_2__c;
                   contactListUpdated.add(cc);
               }
            }   
        }    
        update contactListUpdated;
    }

    catch(Exception e)
    {
        system.debug('Error is -> '+ e.getMessage() + 'at Line -> '+ e.getLineNumber());
    }
        }
}
  • @VijayGanji this is not a duplicate question. I actually need someone with coding knowledge to assist me in creating a test code for this trigger and class. – Chloe Brown Oct 28 '19 at 9:01
  • 1
    Welcome to SFSE! Please take the tour and read How to Ask. This site isn't a free coding service, but rather one whose aim is to help you become a better programmer or admin by helping you overcome very specific issues. A general expectation here is that you've made an honest attempt to solve your own problem first. The question I've marked this as a duplicate of contains resources that should help you get started with writing unit tests. Trailhead is going to be a better resource than this site if you're unfamiliar with a particular concept/feature. – Derek F Oct 28 '19 at 11:20
  • Hey @ChloeBrown, you've written a pretty detailed question so let me try and provide a tailored answer. For more information on test classes in general, please do check out the links to resources on the duplicate question. Here's a commented gist that should help explain the basics: gist.github.com/DanWareing/4740e593ddfdaecbecd49d7bbdadb8f3 So, you have some code – Dan Wareing Oct 28 '19 at 12:18
  • @DanWareing - Thank you for your guidance and putting it into a simple and concise explanation - this is exactly what I needed to at least start in the right direction - trailhead was way too complex for me to actually grasp what the point of it all was. Will work on it and revert when at a further stage. – Chloe Brown Oct 29 '19 at 12:14
  • 1
    @ChloeBrown, that's awesome! Congratulations! You're officially a Salesforce developer :D We can't offer you any free hoodies or laptop stickers, but if you get a warm, fuzzy feeling every time you see somebody using something you wrote then I think that's a pretty sweet reward. Keep chasing that feeling, come back here if you get stuck (we do so daily, even after years), and don't feel bad when you look back at your old code and think it's awful - that just proves you're growing :) – Dan Wareing Nov 19 '19 at 9:04

Browse other questions tagged or ask your own question.