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
CKRCKR 

Need help on the following trigger

Hi EveryOne,

I am a newbee, trying to cover the triggers topic, i am facing a prolem to finishing off writting the following trigger,Could someone help me with this,
I have a master Object Client__c and its child project__c,
1) On after Insert event a child record has to be created and i am successfull till here,
Now the problem is i have similar fields in both master and child objects Start_Date__c and End_Date__c (Date fields), i am unable to pop the same values of master record fields in the child record on after insert event.
2) On after Update The requirement is similar to after insert but the only difference is this has to be happend only on update event 
 Please refere to the If statement in the code,i am trying to make both events of this trigger work only on that particular condition.
 
trigger AutoProjects on Client__c (After insert,After update){
 
    
   if(Trigger.isafter){
   if (trigger.Isinsert){
    List <Project__c> NewProjs= new List <Project__c> ();
        for(client__c Clnt:trigger.new)
        { if (Clnt.High_Priority__c == true){
            Project__c p=new project__c();
            p.Name=Clnt.Name;
       //     p.Start_date__c='Clnt.Start_date__c' ;   // Field value has to be same as Start_date__c of master record
    //    p.End_Date__c = 'Clnt.End_Date__c' ;     // Field value has to be same as End_Date__c of master record
            p.Client_Type__c='Silver';
            p.Location__c = Clnt.Location__c;
            p.Client__c= clnt.Id;                  //Master-detail relationship name
            NewProjs.add(p);
        }
        }
        if (NewProjs.size()>0){
        Insert NewProjs;    
        }
       
      }  
    }
    
    if(Trigger.isafter){
   if (trigger.IsUpdate){
    List <Project__c> NewProjs= new List <Project__c> ();
        for(client__c Clnt:trigger.new)
        { if (Clnt.High_Priority__c == true){
            Project__c p=new project__c();
            p.Name=Clnt.Name;
       //     p.Start_date__c='Clnt.Start_date__c' ;   // Field value has to be same as Start_date__c of master record
    //    p.End_Date__c = 'Clnt.End_Date__c' ;     // Field value has to be same as End_Date__c of master record
            p.Client_Type__c='Silver';
            p.Location__c = Clnt.Location__c;
            p.Client__c= clnt.Id;                  //Master-detail relationship name
            NewProjs.add(p);
        }
        }
        if (NewProjs.size()>0){
        Update NewProjs;    
        }
       
      }  
    }
}

The Error Message that i am seeing on after update even:
User-added image
 
Best Answer chosen by CKR
Amit Chaudhary 8Amit Chaudhary 8
trigger AutoProjects on Client__c (After insert,After update)
{
    
    if(Trigger.isafter)
    {
	    if (trigger.Isinsert)
	    {
			List <Project__c> NewProjs= new List <Project__c> ();
			for(client__c Clnt:trigger.new)
			{ 
				if (Clnt.High_Priority__c == true)
				{
					Project__c p=new project__c();
					p.Name=Clnt.Name;
					p.Start_date__c=Clnt.Start_date__c ;  
					p.End_Date__c = Clnt.End_Date__c ;    
					p.Client_Type__c='Silver';
					p.Location__c = Clnt.Location__c;
					p.Client__c= clnt.Id;                  //Master-detail relationship name
					NewProjs.add(p);
				}
			}
			if (NewProjs.size()>0)
			{
				Insert NewProjs;    
			}
		   
		}
		
		if (trigger.IsUpdate)
		{
			Set<Id> setClientId = new Set<Id>();
			for(client__c Clnt:trigger.new)
			{ 
				if (Clnt.High_Priority__c == true)
				{
					setClientId.add(Clnt.id);
				}
			}

			if(setClientId.size() > 0 )
			{
				List<Project__c> lstPRoject = [select id,Name,Client_Type__c,Location__c,Client__c,Start_date__c,End_Date__c from Project__c where Client__c in : setClientId ];
				Map<ID,Project__c> mapCliendWiseProject = new Map<ID,Project__c>();
				for( Project__c  p : lstPRoject)
				{
					mapCliendWiseProject.put(p.Client__c, p);
				}
				
				List <Project__c> NewProjs= new List <Project__c> ();
				
				for(client__c Clnt : trigger.new ) 
				{ 
					if (Clnt.High_Priority__c == true)
					{
						if(mapCliendWiseProject.containsKey(Clnt.id))
						{
							Project__c p= mapCliendWiseProject.get(Clnt.id);
							p.Name=Clnt.Name;
							p.Start_date__c=Clnt.Start_date__c ;  
							p.End_Date__c = Clnt.End_Date__c ;    
							p.Client_Type__c='Silver';
							p.Location__c = Clnt.Location__c;
							//p.Client__c= clnt.Id;                  //Master-detail relationship name
							NewProjs.add(p);
						}	
					}
				}
				if (NewProjs.size()>0)
				{
					update NewProjs;    
				}
				
			}	
		}  
    }
}

Let us know if this will help you. else please post your error

Thanks
Amit Chaudhary

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
trigger AutoProjects on Client__c (After insert,After update)
{
    
    if(Trigger.isafter)
    {
	    if (trigger.Isinsert)
	    {
			List <Project__c> NewProjs= new List <Project__c> ();
			for(client__c Clnt:trigger.new)
			{ 
				if (Clnt.High_Priority__c == true)
				{
					Project__c p=new project__c();
					p.Name=Clnt.Name;
					//    p.Start_date__c='Clnt.Start_date__c' ;   // Field value has to be same as Start_date__c of master record
					//    p.End_Date__c = 'Clnt.End_Date__c' ;     // Field value has to be same as End_Date__c of master record
					p.Client_Type__c='Silver';
					p.Location__c = Clnt.Location__c;
					p.Client__c= clnt.Id;                  //Master-detail relationship name
					NewProjs.add(p);
				}
			}
			if (NewProjs.size()>0)
			{
				Insert NewProjs;    
			}
		   
		}
		
		if (trigger.IsUpdate)
		{
			Set<Id> setClientId = new Set<Id>();
			for(client__c Clnt:trigger.new)
			{ 
				if (Clnt.High_Priority__c == true)
				{
					setClientId.add(Clnt.id);
				}
			}

			if(setClientId.size() > 0 )
			{
				List<Project__c> lstPRoject = [select id,Name,Client_Type__c,Location__c,Client__c from Project__c where Client__c in : setClientId ];
				Map<ID,Project__c> mapCliendWiseProject = new Map<ID,Project__c>();
				for( Project__c  p : lstPRoject)
				{
					mapCliendWiseProject.put(p.Client__c, p);
				}
				
				List <Project__c> NewProjs= new List <Project__c> ();
				
				for(client__c Clnt : trigger.new ) 
				{ 
					if (Clnt.High_Priority__c == true)
					{
						if(mapCliendWiseProject.containsKey(Clnt.id))
						{
							Project__c p= mapCliendWiseProject.get(Clnt.id);
							p.Name=Clnt.Name;
							//     p.Start_date__c='Clnt.Start_date__c' ;   // Field value has to be same as Start_date__c of master record
							//     p.End_Date__c = 'Clnt.End_Date__c' ;     // Field value has to be same as End_Date__c of master record
							p.Client_Type__c='Silver';
							p.Location__c = Clnt.Location__c;
							p.Client__c= clnt.Id;                  //Master-detail relationship name
							NewProjs.add(p);
						}	
					}
				}
				if (NewProjs.size()>0)
				{
					update NewProjs;    
				}
				
			}	
		}  
    }
}
Please let us know if this will help you

Thanks,
Amit Chaudhary
Jerome RussJerome Russ
You are attempting to update newProjs even thought you are creating them on line 9 with Project__c p=new project__c(); Since you are creating the Project records, you need to perform an insert on line 20, not an update. 
Amit Chaudhary 8Amit Chaudhary 8
trigger AutoProjects on Client__c (After insert,After update)
{
    
    if(Trigger.isafter)
    {
	    if (trigger.Isinsert)
	    {
			List <Project__c> NewProjs= new List <Project__c> ();
			for(client__c Clnt:trigger.new)
			{ 
				if (Clnt.High_Priority__c == true)
				{
					Project__c p=new project__c();
					p.Name=Clnt.Name;
					p.Start_date__c=Clnt.Start_date__c ;  
					p.End_Date__c = Clnt.End_Date__c ;    
					p.Client_Type__c='Silver';
					p.Location__c = Clnt.Location__c;
					p.Client__c= clnt.Id;                  //Master-detail relationship name
					NewProjs.add(p);
				}
			}
			if (NewProjs.size()>0)
			{
				Insert NewProjs;    
			}
		   
		}
		
		if (trigger.IsUpdate)
		{
			Set<Id> setClientId = new Set<Id>();
			for(client__c Clnt:trigger.new)
			{ 
				if (Clnt.High_Priority__c == true)
				{
					setClientId.add(Clnt.id);
				}
			}

			if(setClientId.size() > 0 )
			{
				List<Project__c> lstPRoject = [select id,Name,Client_Type__c,Location__c,Client__c,Start_date__c,End_Date__c from Project__c where Client__c in : setClientId ];
				Map<ID,Project__c> mapCliendWiseProject = new Map<ID,Project__c>();
				for( Project__c  p : lstPRoject)
				{
					mapCliendWiseProject.put(p.Client__c, p);
				}
				
				List <Project__c> NewProjs= new List <Project__c> ();
				
				for(client__c Clnt : trigger.new ) 
				{ 
					if (Clnt.High_Priority__c == true)
					{
						if(mapCliendWiseProject.containsKey(Clnt.id))
						{
							Project__c p= mapCliendWiseProject.get(Clnt.id);
							p.Name=Clnt.Name;
							p.Start_date__c=Clnt.Start_date__c ;  
							p.End_Date__c = Clnt.End_Date__c ;    
							p.Client_Type__c='Silver';
							p.Location__c = Clnt.Location__c;
							//p.Client__c= clnt.Id;                  //Master-detail relationship name
							NewProjs.add(p);
						}	
					}
				}
				if (NewProjs.size()>0)
				{
					update NewProjs;    
				}
				
			}	
		}  
    }
}

Let us know if this will help you. else please post your error

Thanks
Amit Chaudhary
This was selected as the best answer