• Faizan Ali 24
  • NEWBIE
  • 30 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 8
    Replies
I have browsed forums for this issue and sadly it isn't helping much due to my basic knowledge of Apex.

I keep getting: List has no rows for assignment to SObject
 
public class SendEmailButtonController {
    
    private ApexPages.StandardController standardController;
    
    public SendEmailButtonController(ApexPages.StandardController acon){
        this.standardController = standardController;
    }
    
    public static void sendEmail(){
        
        List<String> toAddresses = new List<String>();
        
        Class_Register__c cont = [SELECT Id, Contact__r.Email, Contact__r.Id, Class__r.Name, Class__r.Event_Sub_Type__c FROM Class_Register__c];

        Class__c contClass     = [SELECT Id, Name, Contact__c, Contact__r.Id, Event_Sub_Type__c FROM Class__c];

        EmailTemplate[] et     = [SELECT Id, DeveloperName, FolderId, Body, IsActive FROM EmailTemplate WHERE FolderId = '00D3L0000008l4sUAA' LIMIT 1];

        Contact contacts       = [SELECT Id, FirstName, LastName, Name FROM Contact WHERE Id=: contClass.Id];
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        toAddresses.add(cont.Contact__r.Email);
                
        mail.setUseSignature(false);
        mail.setSaveAsActivity(false);
        
        String subType = contClass.Event_Sub_Type__c;
        
        switch on subType {
            when 'Webinar - Tools & Techniques' {
                mail.setTemplateId('00X3L000000MPbIUAW');
                mail.setTargetObjectId(contacts.Id);
                mail.setWhatId(cont.Id);
            }
            when else {
                System.debug('no value match');
            }
        }
        mail.toAddresses = new String[]{cont.Contact__r.Email};
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});        
    }
}

 
Hi All,

I am still learning apex so I am sorry if there are any rookie mistakes. Learning as much as possible!

I have a controller that sends an email when a button is clicked. But the issue is that whenever the recipient receives the email the merge fields are not working at all. I have tried to follow documentation as much as possible but no luck.
 
public static void sendEmail(){
        
        Class_Register__c cont = [SELECT Id, Contact__c, Contact__r.Email, Contact__r.Id, Class__r.Name, Class__r.Event_Sub_Type__c FROM Class_Register__c];
        List<Class_Register__c> addCont = new List<Class_Register__c>();
        addCont.add(cont);
        
        List<String> toAddresses = new List<String>();
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        toAddresses.add(cont.Contact__r.Email);
        mail.setTargetObjectId(cont.Contact__r.Id);
        mail.setUseSignature(false);
        mail.setSaveAsActivity(false);
                
        EmailTemplate et = [SELECT Id, DeveloperName , Body, IsActive FROM EmailTemplate WHERE DeveloperName = 'Tools_Techniques_Webinar'];
             
        mail.setTemplateId(et.Id);
        mail.setPlainTextBody(et.Body);
        mail.setWhatId(cont.Contact__r.Id);
        mail.toAddresses = new String[]{cont.Contact__r.Email};
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
        
        
    }

 
Hi all,

I am trying to automate the creation of records every 6 months however it is not working and I don't know how to debug it.

My scheduled class:
global class ScheduleRosterAutomation implements Schedulable{
    
    global void execute(SchedulableContext SC) {
        AutomateRosterCreationHandler.createRosters();
        
     }
}



I have a class that creates rosters every 6 months:
public with sharing class AutomateRosterCreationHandler{

    public static List<Employee_Roster__c> createRosters()
    {
        // Get all contacts with standard hours.
        List<Contact> contacts = getContactsWithStdHrs();
        // Create an empty list of Employee Roster objects.
        // This will be used to insert the records into the database.
        List<Employee_Roster__c> empRostList = new List<Employee_Roster__c>();
        // Start Date for each week.
        // uses setMondayStartDate to change the date each week.
        Date startDate = Date.Today().addDays(1);
        //For 26 weeks out of 52 we want to create a roster for each contact each week.
        for (Integer i =0; i < 26; i++)
        {
            // For each contact fetch the standard hours sObject and field values.
            // Assign the values to be input for the Employee Roster.
            // Note: The field used in the UI is Time and the Employee Roster fields are Date/Time.
            // Values will need to be formatted and concatenated for Roster creation.
            for (Contact con : contacts)
            {
                // Get the Standard Hours from the contact
                // Assign the values from the fields to local variables
                Standard_Hours__c standardHours = getStandardHours(con);
                Time monStart;
                Time monEnd;
                Time tueStart;
                Time tueEnd;
                Time wedStart;
                Time wedEnd;
                Time thuStart;
                Time thuEnd;
                Time friStart = standardHours.Friday_Start__c;
                Time friEnd = standardHours.Friday_End__c;
                Time satStart = standardHours.Saturday_Start__c;
                Time satEnd = standardHours.Saturday_End__c;
                Time sunStart = standardHours.Sunday_Start__c;
                Time sunEnd = standardHours.Sunday_End__c;
                
                //Check if each weekday has a start and end time value
                //if it has no value place an empty Time value in.
                if (standardHours.Monday_Start__c != null)
                {
                    monStart = standardHours.Monday_Start__c;
                }else{
                    monStart = Time.newInstance(0, 0, 0, 0);
                }
                
                if (standardHours.Monday_End__c != null)
                {
                    monEnd = standardHours.Monday_End__c;
                }else{
                    monEnd = Time.newInstance(0, 0, 0, 0);
                }
                
                if (standardHours.Tuesday_Start__c != null)
                {
                    tueStart = standardHours.Tuesday_Start__c;
                }else{
                    tueStart = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Tuesday_End__c != null)
                {
                    tueEnd = standardHours.Tuesday_End__c;
                }else{
                    tueEnd = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Wednesday_Start__c != null)
                {
                    wedStart = standardHours.Wednesday_Start__c;
                }else{
                    wedStart = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Wednesday_End__c != null)
                {
                    wedEnd = standardHours.Wednesday_End__c;
                }else{
                    wedEnd = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Thursday_Start__c != null)
                {
                    thuStart = standardHours.Thursday_Start__c;
                }else{
                    thuStart = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Thursday_End__c != null)
                {
                    thuEnd = standardHours.Thursday_End__c;
                }else{
                    thuEnd = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Friday_Start__c != null)
                {
                    friStart = standardHours.Friday_Start__c;
                }else{
                    friStart = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Friday_End__c != null)
                {
                    friEnd = standardHours.Friday_End__c;
                }else{
                    friEnd = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Saturday_Start__c != null)
                {
                    satStart = standardHours.Saturday_Start__c;
                }else{
                    satStart = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Saturday_End__c != null)
                {
                    satEnd = standardHours.Saturday_End__c;
                }else{
                    satEnd = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Sunday_Start__c != null)
                {
                    sunStart = standardHours.Sunday_Start__c;
                }else{
                    sunStart = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Sunday_End__c != null)
                {
                    sunEnd = standardHours.Sunday_End__c;
                }else{
                    sunEnd = Time.newInstance(0,0,0,0);
                }
                
                Contact cont = con;
                // Create new empty Employee Roster
                Employee_Roster__c empRost = new Employee_Roster__c();
                
                
                // Assign the values for the Employee Roster
                // Format the date/time fields accordingly using the values from the local variables.
                
                emprost.Week_Beginning__c = startDate;
                emprost.Contact__c = con.Id;
                emprost.Monday_Start__c = DateTime.newInstance(startDate, monStart);        
                emprost.Monday_End__c = DateTime.newInstance(startDate, monEnd);
                emprost.Tuesday_Start__c = DateTime.newInstance(startDate.addDays(1), tueStart);
                emprost.Tuesday_End__c = DateTime.newInstance(startDate.addDays(1), tueEnd);
                emprost.Wednesday_Start__c = DateTime.newInstance(startDate.addDays(2), wedStart);
                emprost.Wednesday_End__c = DateTime.newInstance(startDate.addDays(2), wedEnd);
                emprost.Thursday_Start__c = DateTime.newInstance(startDate.addDays(3), ThuStart);
                emprost.Thursday_End__c = DateTime.newInstance(startDate.addDays(3), thuEnd);
                emprost.Friday_Start__c = DateTime.newInstance(startDate.addDays(4), friStart);
                emprost.Friday_End__c = DateTime.newInstance(startDate.addDays(4), friEnd);
                emprost.Saturday_Start__c = DateTime.newInstance(startDate.addDays(6), satStart);
                emprost.Saturday_End__c = DateTime.newInstance(startDate.addDays(6), satEnd);
                emprost.Sunday_Start__c = DateTime.newInstance(startDate.addDays(7), sunStart);
                emprost.Sunday_End__c = DateTime.newInstance(startDate.addDays(7), sunEnd);
                
                // add the roster to the list of rosters
                empRostList.add(emprost);
            }
            startDate = setMondayStartDate(startDate);
        }
        
        // Persist the list of rosters.
        insert empRostList;
        return empRostList;
    }
    
    // Add 7 days to the last start date.
    public static Date setMondayStartDate(Date lastStartDate)
    {
        Date newDate = lastStartDate.addDays(7);
        return newDate;
    }

    // Get a list of all contacts with Standard Hours
    public static List<Contact> getContactsWithStdHrs()
    {
        List<Contact> conts = [SELECT Id, Name, Has_Standard_Hours__c FROM Contact WHERE RecordType.Name IN ('Team Contact') AND Contact.Has_Standard_Hours__c = true];
        return conts;
    }

    // Get standard hours from a specific contact.
    public static Standard_Hours__c getStandardHours(Contact con)
    {
        Standard_Hours__c  stdHrs;
        // Check if contact has standard hours
        if (con.Has_Standard_Hours__c == True)
        {
            // populate a Standard Hours sObject with values based on the contact
            stdHrs = [SELECT Id, Name, Monday_Start__c, Monday_End__c, Tuesday_Start__c, Tuesday_End__c, Wednesday_Start__c, Wednesday_End__c, Thursday_Start__c, Thursday_End__c, Friday_Start__c, Friday_End__c, Saturday_Start__c, Saturday_End__c, Sunday_Start__c, Sunday_End__c, Contact__c FROM Standard_Hours__c where Contact__c =: con.Id LIMIT 1];
            return stdHrs;
        }
        else{
            return stdHrs;
        }
    }
    
    public static void AutomateRosterCreationHandler()
    {
        createRosters();
    }
}

 
Hi All,

I want to change the format of my date. Currently, the format is in yyyy-MM-dd but I want it to be like 12th December 2020.

My apex code is currently:

currentDateString = string.valueOf(Date.today());

And in my visualforce page it is:

<apex:outputLabel id="currentDateString" style="color:white;font-size:26px;font-weight:bold">{!currentDateString}</apex:outputLabel>
Hi All,

I am getting a very rookie error. I am not sure why I am getting it however could someone please help?

I am getting the below errors:

User-added image

Code:

User-added image


 
Hi All,

I am trying to create a striped effect however when I implement the CSS code it comes out as so:

User-added image

I want the lines to be perfect and straight.

var cssBooked = "repeating-linear-gradient(45deg,#606dbc,#606dbc 10px,#465298 10px,#465298 20px)";
I am trying to fire this trigger:

trigger Autofill on Event (before insert, before update) {  
    
        for (Event e : Trigger.new) 
        {
            boolean flag = (e.Appointment_with__c==null)?true:false;
            
            if(flag){
                          
            
            String [] str = e.Appointment_with__c.split(' ');
            
            
            List<Contact> cont = [SELECT Id FROM Contact WHERE FirstName =:str[0] AND LastName =:str[1]];              
            
            
            e.Staff__c = cont[0].Id;
            }                  

        }


But I keep getting this error and I don't know how to fix it at all:

Autofill: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.Autofill: line 10, column 1
Hi All,

I have a trigger that autofills a lookup field based on a picklist value. It will look for the Contact name when a person is selected from the picklist.

However, whenever a person is not selected from picklist it should not fire the trigger. When I do try to I get this error:

Autofill: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.Autofill: line 6, column 1



trigger Autofill on Event (before insert, before update) {   
    
        for (Event e : Trigger.new) 
        {              
            String [] str = e.Appointment_with__c.split(' ');
            
            
            List<Contact> cont = [SELECT Id FROM Contact WHERE FirstName =:str[0] AND LastName =:str[1]];              
            
            
            e.Staff__c = cont[0].Id;
        }
}
Hi all,

I am new to Salesforce and would appreciate if someone could help me with the following.

I want to move today's date forward (on click of button) whenever a certain number is selected from my picklist.

User-added image

So If I select 1 week and click the forward button it should go forward 1 week. If I click the back button it should go back 1 week. This applies to all the picklist values.

VF page code:

<apex:page showHeader="false" controller="increment">
    <apex:form>    
    
    <apex:outputText value="{0, date, dd/MM/yy}">
        <apex:param value="{!NOW()}" />
    </apex:outputText>
    
        <apex:commandButton value = "<"/>
        <apex:commandButton value = ">"/>
        
        <apex:selectList value="{!weeks}" size = "1">
            <apex:selectOptions value="{!items}">
            </apex:selectOptions>            
        </apex:selectList>
    
    </apex:form>   
</apex:page>


Apex class increment:

public class increment {
    String[] weeks = new String[]{};

    public PageReference test() {
        return null;
    }

    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('1 week','1 week'));
        options.add(new SelectOption('2 weeks','2 weeks'));
        options.add(new SelectOption('3 weeks','3 weeks'));
        options.add(new SelectOption('4 weeks','4 weeks'));

        return options;
    }

    public String[] getWeeks() {
        return weeks;
    }

    public void setWeeks(String[] weeks) {
        this.weeks = weeks;
    }
}

 
I am new to Salesforce and I am struggling to write a simple test class. I haven't written one before though I have been doing a lot of trailhead on it but can't seem to wrap my head around this test class for a trigger.

I have this trigger that Autofills a lookup field called Staff whenever a value is selected from the Appointment_with__c picklist field. It only auto-fills whenever the event is saved:

trigger Autofill on Event (before insert, before update) {
    { 
        for (Event e : Trigger.new) 
        {
            
            String [] str = e.Appointment_with__c.split(' ');
            
            
            List<Contact> cont = [SELECT Id FROM Contact WHERE FirstName =:str[0] AND LastName =:str[1]];               
            
            
            e.Staff__c = cont[0].Id;
        }
    }
}

Any ideas on how to create a test class for this? 

Thank you in advance for your help
 
I have browsed forums for this issue and sadly it isn't helping much due to my basic knowledge of Apex.

I keep getting: List has no rows for assignment to SObject
 
public class SendEmailButtonController {
    
    private ApexPages.StandardController standardController;
    
    public SendEmailButtonController(ApexPages.StandardController acon){
        this.standardController = standardController;
    }
    
    public static void sendEmail(){
        
        List<String> toAddresses = new List<String>();
        
        Class_Register__c cont = [SELECT Id, Contact__r.Email, Contact__r.Id, Class__r.Name, Class__r.Event_Sub_Type__c FROM Class_Register__c];

        Class__c contClass     = [SELECT Id, Name, Contact__c, Contact__r.Id, Event_Sub_Type__c FROM Class__c];

        EmailTemplate[] et     = [SELECT Id, DeveloperName, FolderId, Body, IsActive FROM EmailTemplate WHERE FolderId = '00D3L0000008l4sUAA' LIMIT 1];

        Contact contacts       = [SELECT Id, FirstName, LastName, Name FROM Contact WHERE Id=: contClass.Id];
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        toAddresses.add(cont.Contact__r.Email);
                
        mail.setUseSignature(false);
        mail.setSaveAsActivity(false);
        
        String subType = contClass.Event_Sub_Type__c;
        
        switch on subType {
            when 'Webinar - Tools & Techniques' {
                mail.setTemplateId('00X3L000000MPbIUAW');
                mail.setTargetObjectId(contacts.Id);
                mail.setWhatId(cont.Id);
            }
            when else {
                System.debug('no value match');
            }
        }
        mail.toAddresses = new String[]{cont.Contact__r.Email};
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});        
    }
}

 
Hi All,

I am getting a very rookie error. I am not sure why I am getting it however could someone please help?

I am getting the below errors:

User-added image

Code:

User-added image


 
Hi All,

I have a trigger that autofills a lookup field based on a picklist value. It will look for the Contact name when a person is selected from the picklist.

However, whenever a person is not selected from picklist it should not fire the trigger. When I do try to I get this error:

Autofill: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.Autofill: line 6, column 1



trigger Autofill on Event (before insert, before update) {   
    
        for (Event e : Trigger.new) 
        {              
            String [] str = e.Appointment_with__c.split(' ');
            
            
            List<Contact> cont = [SELECT Id FROM Contact WHERE FirstName =:str[0] AND LastName =:str[1]];              
            
            
            e.Staff__c = cont[0].Id;
        }
}
I am new to Salesforce and I am struggling to write a simple test class. I haven't written one before though I have been doing a lot of trailhead on it but can't seem to wrap my head around this test class for a trigger.

I have this trigger that Autofills a lookup field called Staff whenever a value is selected from the Appointment_with__c picklist field. It only auto-fills whenever the event is saved:

trigger Autofill on Event (before insert, before update) {
    { 
        for (Event e : Trigger.new) 
        {
            
            String [] str = e.Appointment_with__c.split(' ');
            
            
            List<Contact> cont = [SELECT Id FROM Contact WHERE FirstName =:str[0] AND LastName =:str[1]];               
            
            
            e.Staff__c = cont[0].Id;
        }
    }
}

Any ideas on how to create a test class for this? 

Thank you in advance for your help