function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VARALAXMI JANGAYVARALAXMI JANGAY 

Trigger to send an Email in Salesforce

Hi,
I have a below code, I am getting a null values rather than getting a mail to the mailId mentioned in the code.
Here is my Code:
Code
------------------------------------------------------------------

trigger SHiftmangment on Shift_Management__c (before insert, after insert) {
    List < Messaging.SingleEmailMessage > mailList = new List < Messaging.SingleEmailMessage > ();
    list<Shift_Management__c> sm = [select id,Contact__c,From_Date__c,To_Date__c,Shift__C,contact__r.Email from Shift_Management__c];
    for(Shift_Management__c c : trigger.new){
    

    
        for(Shift_Management__c sf : sm){
            
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                message.setTemplateId('00X6F000001BxUA');
                message.setTargetObjectId(c.contact__c);
                List<String> toAddresses = new List<String>();
                toAddresses.add(sf.contact__r.Email);
                message.setToAddresses(toAddresses);
                mailList.add(message);
               // Messaging.sendEmail(new Messaging.Email[] {message});
            if(c.Contact__c == sf.Contact__c && c.From_Date__c == sf.From_Date__c && c.Shift__c == sf.Shift__c){
                
                c.Contact__c.addError('Shift already assgined with this date');  
                c.Shift__c.addError('Shift already assgined');
                c.From_Date__c.addError('Shift already assgined with this date');
            }
            else if((c.From_Date__c >=sf.From_Date__c && c.From_Date__c <=sf.To_Date__c) && c.Contact__c == sf.Contact__c){
                c.addError('Shift already assgined with in this date');
            }
            
        }
    }
    Messaging.sendEmail(mailList);
}

Please can anyone help me to solve that problem.

Thanks,
Varalaxmi
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Varalaxmi,

 

Can you please explain your actual requirement so that accordingly I can do changes in code

VARALAXMI JANGAYVARALAXMI JANGAY
I have two objects Contact and Shift_managment__c, they have lookup relation. Now when i create a new record in Shift_managment__c, an email has to be sent to the contact's Email.
Deepak Maheshwari 7Deepak Maheshwari 7

Please use below trigger:

 

trigger SHiftmangment on Shift_Management__c (before insert, after insert) {
    List < Messaging.SingleEmailMessage > mailList = new List < Messaging.SingleEmailMessage > ();
    list<Shift_Management__c> sm = [select id,Contact__c,From_Date__c,To_Date__c,Shift__C,contact__r.Email from Shift_Management__c];
    if(trigger.isbefore)
    {
    for(Shift_Management__c c : trigger.new){
        for(Shift_Management__c sf : sm)
        {
            if(c.Contact__c == sf.Contact__c && c.From_Date__c == sf.From_Date__c && c.Shift__c == sf.Shift__c){
                
                c.Contact__c.addError('Shift already assgined with this date');  
                c.Shift__c.addError('Shift already assgined');
                c.From_Date__c.addError('Shift already assgined with this date');
            }
            else if((c.From_Date__c >=sf.From_Date__c && c.From_Date__c <=sf.To_Date__c) && c.Contact__c == sf.Contact__c){
                c.addError('Shift already assgined with in this date');
            }
    }
    }
    }
    if(trigger.isafter)
    {
        for(Shift_Management__c c : trigger.new){
            
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                message.setTemplateId('00X6F000001BxUA');
                message.setTargetObjectId(c.contact__c);
                //List<String> toAddresses = new List<String>();
                //toAddresses.add(c.contact__r.Email);
                //message.setToAddresses(toAddresses);
                mailList.add(message);
               // Messaging.sendEmail(new Messaging.Email[] {message});
            
            
        }
    
    Messaging.sendEmail(mailList);
    }
}

Deepak Maheshwari 7Deepak Maheshwari 7

Hi,

 

Did you try the above trigger?

Hope this will worked for you!

 

If not please let me know.

 

Thanks