+ Start a Discussion
CvrKCvrK 

Need help,before trigger not working

Hi Everyone,
i have below trigger with both before insert and after insert events,Client__c (Master Obj) and Project__c(Detail Obj) with master detail relationship.
My requirement is when a ever a new client record is going to be inserted Quotation__c field has to be updated with the value that provided in the trigger,and i have an after insert event in the same trigger

After trigger is working as expected but my before trigger throwing following error
Error for the trigger

Trigger :

trigger AutoProjects on Client__c (before insert,After insert){
    
    If(trigger.isbefore)
    {
         list <Client__c>NewClnts=New List<Client__c>();
         for(Client__c Clnt:trigger.New)
         {
            Client__c c=new Client__c();
            C.Name=   //Need a value that says "the record that is going to be created with any name not a particular name"
            c.Assigned_To__c='chhakri;chetan';
            NewClnts.add(C);
         }
       Insert NewClnts;  
    }
   
   if(Trigger.isafter){
    List <Project__c> NewProjs= new List <Project__c> ();
        for(client__c Clnt:trigger.new)
        {
            Project__c p=new project__c();
            p.Name=Clnt.Name+'Project';
            p.Start_date__c=date.today();
            p.Client_Type__c='Silver';
            p.Client__c= clnt.Id;
            NewProjs.add(p);
        }
        Insert NewProjs;
    }
 }
 
ManojjenaManojjena
Hi crvk,

Try with below code as you have inserted in before insert which is not required .
 
trigger AutoProjects on Client__c (before insert,After insert){
    
    If(trigger.isbefore)
    {
         
         for(Client__c Clnt:trigger.New)
         {
            Client__c c=new Client__c();
            C.Name=   //Need a value that says "the record that is going to be created with any name not a particular name"
            c.Assigned_To__c='chhakri;chetan';
          
         }
        
    }
   
   if(Trigger.isafter){
    List <Project__c> NewProjs= new List <Project__c> ();
        for(client__c Clnt:trigger.new)
        {
            Project__c p=new project__c();
            p.Name=Clnt.Name+'Project';
            p.Start_date__c=date.today();
            p.Client_Type__c='Silver';
            p.Client__c= clnt.Id;
            NewProjs.add(p);
        }
        Insert NewProjs;
    }
 }

Let me know if it helps

Thanks
Manoj
CvrKCvrK
Hi manoj,
Trigger not firing on before insert hence no value is popping in my Assigned_To__c field(picklist).
ManojjenaManojjena
Hi CvrK
TRy with belwo code .
trigger AutoProjects on Client__c (before insert,After insert){
    
    If(trigger.isbefore)
    {
         
         for(Client__c Clnt:trigger.New)
         {
           
            Clnt.Name=   //Need a value that says "the record that is going to be created with any name not a particular name"
            Clnt.Assigned_To__c='chhakri chetan';
          
         }
        
    }
   
   if(Trigger.isafter){
    List <Project__c> NewProjs= new List <Project__c> ();
        for(client__c Clnt:trigger.new)
        {
            Project__c p=new project__c();
            p.Name=Clnt.Name+'Project';
            p.Start_date__c=date.today();
            p.Client_Type__c='Silver';
            p.Client__c= clnt.Id;
            NewProjs.add(p);
        }
        Insert NewProjs;
    }
 }

Thanks
Manoj