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 which fires before record inserted.

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 this and email trigger  functionality which fired a mail when a customer project created under account and its project_status__c='PMR' (this status of  project is custom field on customer_project__c). I have a textbox of group mail where , emails are inserted and 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.
sandeep reddy 37sandeep reddy 37
  public ID  CusRecType = [SELECT Id FROM RecordType WHERE DeveloperName = 'Customer'].Id
check wether it is  write limit clause here 
and  first method it only work for customer record type only that not work out beacuase you are trying validate input but you are performing using other object data  wtat ever it may you give that will validates we can not provide data from out side that is not use full you only perform dml on  Customer_Projects__c this object also other using Customer_Projects__c this obj data 
I hope it is help full for u?
 
 
Deepak Singh 116Deepak Singh 116
Is there any way to achieve this. Sent from my Windows Phone
sandeep reddy 37sandeep reddy 37
its not possible so in what  kind of situation you need then i will help you 
Deepak Singh 116Deepak Singh 116
I need this trigger when any customer create for a project. Then this customer get a email notification if there is a column with name project_status__c is PMR this column is on customer_project__c object. Sent from my Windows Phone
sandeep reddy 37sandeep reddy 37
trigger onemailnotification on customer_project__c(after insert){ for(customer_project__c c:trigger.new){ if(c.project_status__c ==PMR){ verficationcode v=new verficationcode(); } } the apex CLASS public class verficationcode { public string name{set;get;} public string[] emails{set;get;} public string email1{set;get;} public string email2{set;get;} public integer age{set;get;} public string verfication{set;get;} public list acc {set;get;} public verficationcode(){ acc=new list(); } public void check(){ emails=new string[]{'bethasrinivasareddy@gmail.com',' sandeepambati4512@gmail.com'}; messaging.SingleEmailMessage single=new messaging.SingleEmailMessage(); single.setToAddresses(emails); single.setsubject('verfication coad'); single.setPlainTextBody('dear'+name+'the verfication coad for registration is is :'+verfication); messaging.Email[] mail =new messaging.Email[]{single}; messaging.sendEmail(mail); Messaging.SingleEmailMessage[] messages = new List {single}; Messaging.SendEmailResult[] results = Messaging.sendEmail(messages); /* } public void insertacc(){ account a=new account(); a.Name=name; a.srinivasreddy__verficationcoad__c=verfication; a.srinivasreddy__email__c=email1; acc.add(a); insert acc; }*/ } There is no problem beacuase every one can not access project_status__c Right ? other wise any thing ask me ok
Deepak Singh 116Deepak Singh 116
hi,
I have written a triiger for firing a mail when a new customer added to a project.following is the apex class and apex trigger but does not work.Please help if there is any mistake in following code:-

Apex trigger:-
trigger RBI_CustomerProjectTrigger on Customer_Projects__c (before insert, after insert, before update) {
    RBI_CustomerProjectTriggerHandler handler= new RBI_CustomerProjectTriggerHandler();
     if(Trigger.isInsert && Trigger.isbefore){
        handler.OnBeforeInsert(Trigger.new);
      }
     /* After Update */
    if(Trigger.isUpdate && Trigger.isbefore){  
        handler.OnBeforeUpdate(Trigger.new , Trigger.old, Trigger.oldMap);
    }
     /* After insert */
    if(Trigger.isInsert && Trigger.isafter){
        handler.OnAfterinsert(Trigger.new);  
        } 
}

Apex Class:- 

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;
                }
            }
        }
    }
  }
  public void OnAfterinsert(list<Customer_Projects__c> Cusprojectlist){
        if (firstRun){
          List<Messaging.SingleEmailMessage> emails;
            list<Customer_Projects__c> custProject = new list<Customer_Projects__c>();
            set<Id> projSetId = new set<Id>();
            set<Id> custProjSetId = new set<Id>();
            list<Contact> con = new list<Contact>();
            map<Id, wrapEmailProject> projWrap = new map<Id, wrapEmailProject>();
          
               if(custProjSetId != null && custProjSetId.size() > 0)
                    con = [select name, Email, AccountId, Group_Email__c from Contact where AccountId IN : custProjSetId and Primary__c = true];
                for(Customer_Projects__c cusProj : custProject){ 
                if(cusProj.project_status__c=='PMR'){
                    for(Contact c : con){ // Inner for loop
                        if(c.AccountId == cusProj.AccountId__c){
                            String[] toAddresses;
                            String toAddr;
                            Customer_Projects__c custProjRec = new Customer_Projects__c();
                            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                            if(c.Group_Email__c != null){
                                toAddr = c.Group_Email__c.replace('\r\n', '');
                                toAddresses = toAddr.split(',');
                                mail.setToAddresses(toAddresses);
                                mail.setToAddresses(toAddresses);
                            }
                            
                            
                            mail.setTemplateId(System.label.RBI_TemplateIdForPRMStatusChange);
                            mail.setTargetObjectId(c.Id);
                            mail.setWhatId(cusProj.Project_Id__c);
                            emails.add(mail);
                        }
                        
                    }// end of inner for loop   
                }   
                if(emails != null && emails.size() > 0)
                  Messaging.sendEmail(emails);
            }
            }
        
        }
    
    public class wrapEmailProject{
        public Customer_Projects__c custProjrecord;
        public String emailId;
        public wrapEmailProject(Customer_Projects__c custproj){
            custProjrecord = custproj;
        }
        public wrapEmailProject(Customer_Projects__c custproj, String conEmail){
            custProjrecord = custproj;
            emailId = conEmail;
        }
    }
   
  //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;
              }
            }
      }
    }
  }
}