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
Deepak Singh 116Deepak Singh 116 

Need a trigger whenever a record created.

Hi,  I am very new in salesforce developement . I need a trigger which fired a mail when a customer project created under account. I have a text of group mail where , emails are inserted seprated by comma ",". so i need a trigger which fire mail to group mail of particular customer which is linked to project.
Sudipta DebSudipta Deb
Hi Deepak,

Please find the trigger code below -

Assumption:

Project Object: Project__c
Relation with Account is Mater-Detail
Custom Field on Project Object: Group_email__c
 
trigger ProjectTrigger on Project__c (after insert) {
	List<String> emailAddresses = new List<String>();
	Map<Project__c, List<String>> projectEmailAddressMap = new Map<Project__c, List<String>>();
	
	List<Messaging.SingleEmailMessage> allMessages = new List<Messaging.SingleEmailMessage>();
	
    for(Project__c singleProject : Trigger.New){
    	Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
		//Insert your subject here
    	email.setSubject( 'Test' );
    	String[] toAddresses = singleProject.Group_Email__c.split(',', 0);
    	email.setToAddresses( toAddresses );
		
		//insert your email body content here
    	email.setPlainTextBody( 'Test Body' );
    	
    	allMessages.add(email);
    }
    
    if(allMessages.size() > 0){
    	Messaging.SendEmailResult [] r =
            	Messaging.sendEmail(allMessages); 
    }
}

I would suggest you to learn Apex Trigger by going through the Trailhead module @ https://developer.salesforce.com/trailhead/module/apex_triggers

Note - Please accept my solution as Best Answer if my reply was helpful.
Deepak Singh 116Deepak Singh 116
Hi Sudipta,
Thanks for your valuable rply.but this didn't work. there is some complextion ,I will explain you :-
i have already a trigger on customer_project_c which is follows-

public with sharing class RBI_CustomerProjectTriggerHandler {
  public static boolean firstRun = true;
  public ID  CusRecType = [SELECT Id FROM RecordType WHERE DeveloperName = 'Customer'].Id;
  public ID RbiRecType = [SELECT Id FROM RecordType WHERE DeveloperName = 'RBI'].Id;
  public list<Account> Acclist=[Select Id , Weightage_Electrification_Sanitation__c,Weightage_Basement__c,
                      Weightage_Excavaton_of_Footings__c, Weightage_Finishing__c,
                      Weightage_RCC_Slab_Casting__c,RecordTypeId,Weightage_Masonary__c from Account WHERE RecordTypeId=:RbiRecType OR RecordTypeId=:CusRecType];
       
                    
  // Before Insert Trigger
  public void OnBeforeInsert(list<Customer_Projects__c> Cusprojectlist){ 
    if(Cusprojectlist!=null && Cusprojectlist.size()>0){
      for(Customer_Projects__c cusproj : Cusprojectlist){
          for(Account Acc : Acclist){
            if((cusproj.Weightage_Type__c == 'Realty BI') && (Acc.RecordTypeId==RbiRecType)) {
              cusproj.Weightage_Basement__c=Acc.Weightage_Basement__c;
                cusproj.Weightage_Electrification_Sanitation__c=Acc.Weightage_Electrification_Sanitation__c;
                cusproj.Weightage_Excavaton_of_Footings__c=Acc.Weightage_Excavaton_of_Footings__c;
                cusproj.Weightage_Finishing__c=Acc.Weightage_Finishing__c; 
                cusproj.Weightage_RCC_Slab_Casting__c=Acc.Weightage_RCC_Slab_Casting__c;
                cusproj.Weightage_Masonary__c=Acc.Weightage_Masonary__c; 
          }
                else if((cusproj.Weightage_Type__c == 'Customer') && (Acc.RecordTypeId ==CusRecType) && (Acc.Id == cusproj.AccountId__c)) { 
                  cusproj.Weightage_Basement__c=Acc.Weightage_Basement__c;
                cusproj.Weightage_Electrification_Sanitation__c=Acc.Weightage_Electrification_Sanitation__c;
                cusproj.Weightage_Excavaton_of_Footings__c=Acc.Weightage_Excavaton_of_Footings__c;
                cusproj.Weightage_Finishing__c=Acc.Weightage_Finishing__c; 
                cusproj.Weightage_RCC_Slab_Casting__c=Acc.Weightage_RCC_Slab_Casting__c;
                cusproj.Weightage_Masonary__c=Acc.Weightage_Masonary__c;
                }
            }
        }
    }
  }
  //OnBeforeUpdate trigger
  public void OnBeforeUpdate(list<Customer_Projects__c> CusprojectNew, list<Customer_Projects__c> CusprojectOld, Map<Id, Customer_Projects__c> ObjectMap){
    for(Customer_Projects__c cusprj : CusprojectNew){
      if(cusprj.Id == ObjectMap.get(cusprj.Id).Id && ObjectMap.get(cusprj.Id).Weightage_Type__c != cusprj.Weightage_Type__c){
        for(Account Acc1 : Acclist){
            if((cusprj.Weightage_Type__c == 'Customer') && (cusprj.AccountId__c==Acc1.Id) && (Acc1.RecordTypeId ==CusRecType) ) { 
              cusprj.Weightage_Basement__c=Acc1.Weightage_Basement__c;
                cusprj.Weightage_Electrification_Sanitation__c=Acc1.Weightage_Electrification_Sanitation__c;
                cusprj.Weightage_Excavaton_of_Footings__c=Acc1.Weightage_Excavaton_of_Footings__c;
                cusprj.Weightage_Finishing__c=Acc1.Weightage_Finishing__c; 
                cusprj.Weightage_RCC_Slab_Casting__c=Acc1.Weightage_RCC_Slab_Casting__c;
                cusprj.Weightage_Masonary__c=Acc1.Weightage_Masonary__c;
              }
              else if((cusprj.Weightage_Type__c == 'Realty BI') && (Acc1.RecordTypeId==RbiRecType)) {
                cusprj.Weightage_Basement__c=Acc1.Weightage_Basement__c;
                  cusprj.Weightage_Electrification_Sanitation__c=Acc1.Weightage_Electrification_Sanitation__c;
                  cusprj.Weightage_Excavaton_of_Footings__c=Acc1.Weightage_Excavaton_of_Footings__c;
                  cusprj.Weightage_Finishing__c=Acc1.Weightage_Finishing__c; 
                  cusprj.Weightage_RCC_Slab_Casting__c=Acc1.Weightage_RCC_Slab_Casting__c;
                  cusprj.Weightage_Masonary__c=Acc1.Weightage_Masonary__c;
              }
            }
      }
    }
  }
}
now i want to modify and add email trigger  functionality which fired a mail when a customer project created under account and its project_status__c='PMR' (this status is project on customer_project__c). I have a text of group mail where , emails are inserted seprated by comma ",". so i need a trigger which fire mail to group mail of particular customer which is linked to project.:-

Project Object: customer_project__c
Relation with Account is Mater-Detail
Custom Field on contacts: Group_email and a check box "Primary"   this group mail is entered where primary check box is checked.
this both contact and customer project is under accont

It urgent please help me  soon a possible.