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
Prentiss Jones 3Prentiss Jones 3 

Trigger To Create Records

User-added image

Hello, I'm new to Apex. But I think what I'm trying to do is simple. Would Like to creat a record (Goal Split) for each Goal Q Field (there are 4 of these fields) And carry user names to that record (Client Owner, Agency Owner, Addional Owner.) Goal Split.

On the the parent if a user changes (Client Owner, Agency Owner, Addional Owner) Then update the child (Goal Split) record. 

Not all Goal Q fields have to be filled out. Example. Rep may have a goal for Goal Q1,Goal Q3,Goal Q4....skipping Goal Q2. In this case only create 3 records. 
Best Answer chosen by Prentiss Jones 3
Neetu_BansalNeetu_Bansal
Hi Prentiss,

I have written the trigger code. Just replace the object and field API Names, if these are different from what I have used.
trigger GoalTrg on Goal__c( after insert, after update )
{
	if( trigger.isInsert )
	{
		List<Goal_Split__c> goalSplits = new List<Goal_Split__c>();
		
		for( Goal__c goal : trigger.new )
		{
			if( goal.Goal_Q1__c != null )
			{
				Goal_Split__c goalSplit = new Goal_Split__c( Name = goal.Goal_Q1__c, Client_Owner__c = goal.Client_Owner__c, Additional_Owner__c = goal.Additional_Owner__c, Agency_Owner__c = goal.Agency_Owner__c, Goal__c = goal.Id );
				goalSplits.add( goalSplit );
			}
			
			if( goal.Goal_Q2__c != null )
			{
				Goal_Split__c goalSplit = new Goal_Split__c( Name = goal.Goal_Q2__c, Client_Owner__c = goal.Client_Owner__c, Additional_Owner__c = goal.Additional_Owner__c, Agency_Owner__c = goal.Agency_Owner__c, Goal__c = goal.Id );
				goalSplits.add( goalSplit );
			}
			
			if( goal.Goal_Q3__c != null )
			{
				Goal_Split__c goalSplit = new Goal_Split__c( Name = goal.Goal_Q3__c, Client_Owner__c = goal.Client_Owner__c, Additional_Owner__c = goal.Additional_Owner__c, Agency_Owner__c = goal.Agency_Owner__c, Goal__c = goal.Id );
				goalSplits.add( goalSplit );
			}
			
			if( goal.Goal_Q4__c != null )
			{
				Goal_Split__c goalSplit = new Goal_Split__c( Name = goal.Goal_Q4__c, Client_Owner__c = goal.Client_Owner__c, Additional_Owner__c = goal.Additional_Owner__c, Agency_Owner__c = goal.Agency_Owner__c, Goal__c = goal.Id );
				goalSplits.add( goalSplit );
			}
		}
		
		if( goalSplits.size() > 0 )
			insert goalSplits;
	}
	else if( trigger.isUpdate )
	{
		List<Goal_Split__c> goalSplits = new List<Goal_Split__c>();
		
		List<Goal__c> goals = [ Select Id, Agency_Owner__c, Additional_Owner__c, Client_Owner__c, ( Select Agency_Owner__c, Additional_Owner__c, Client_Owner__c, Id from Goal_Splits__r) from Goal__c where Id IN: trigger.new ];
		
		for( Goal__c g : goals )
		{
			if( trigger.oldMap.get( g.Id ).Agency_Owner__c != g.Agency_Owner__c
				|| trigger.oldMap.get( g.Id ).Additional_Owner__c != g.Additional_Owner__c
				|| trigger.oldMap.get( g.Id ).Client_Owner__c != g.Client_Owner__c )
			{
				for( Goal_Split__c gs : g.Goal_Splits__r )
				{
					gs.Additional_Owner__c = g.Additional_Owner__c;
					gs.Agency_Owner__c = g.Agency_Owner__c;
					gs.Client_Owner__c = g.Client_Owner__c;
					
					goalSplits.add( gs );
				}
			}
		}
		
		if( goalSplits.size() > 0 )
			update goalSplits;
	}
}
Let me know if you need any other help.

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi Prentiss,

I have written the trigger code. Just replace the object and field API Names, if these are different from what I have used.
trigger GoalTrg on Goal__c( after insert, after update )
{
	if( trigger.isInsert )
	{
		List<Goal_Split__c> goalSplits = new List<Goal_Split__c>();
		
		for( Goal__c goal : trigger.new )
		{
			if( goal.Goal_Q1__c != null )
			{
				Goal_Split__c goalSplit = new Goal_Split__c( Name = goal.Goal_Q1__c, Client_Owner__c = goal.Client_Owner__c, Additional_Owner__c = goal.Additional_Owner__c, Agency_Owner__c = goal.Agency_Owner__c, Goal__c = goal.Id );
				goalSplits.add( goalSplit );
			}
			
			if( goal.Goal_Q2__c != null )
			{
				Goal_Split__c goalSplit = new Goal_Split__c( Name = goal.Goal_Q2__c, Client_Owner__c = goal.Client_Owner__c, Additional_Owner__c = goal.Additional_Owner__c, Agency_Owner__c = goal.Agency_Owner__c, Goal__c = goal.Id );
				goalSplits.add( goalSplit );
			}
			
			if( goal.Goal_Q3__c != null )
			{
				Goal_Split__c goalSplit = new Goal_Split__c( Name = goal.Goal_Q3__c, Client_Owner__c = goal.Client_Owner__c, Additional_Owner__c = goal.Additional_Owner__c, Agency_Owner__c = goal.Agency_Owner__c, Goal__c = goal.Id );
				goalSplits.add( goalSplit );
			}
			
			if( goal.Goal_Q4__c != null )
			{
				Goal_Split__c goalSplit = new Goal_Split__c( Name = goal.Goal_Q4__c, Client_Owner__c = goal.Client_Owner__c, Additional_Owner__c = goal.Additional_Owner__c, Agency_Owner__c = goal.Agency_Owner__c, Goal__c = goal.Id );
				goalSplits.add( goalSplit );
			}
		}
		
		if( goalSplits.size() > 0 )
			insert goalSplits;
	}
	else if( trigger.isUpdate )
	{
		List<Goal_Split__c> goalSplits = new List<Goal_Split__c>();
		
		List<Goal__c> goals = [ Select Id, Agency_Owner__c, Additional_Owner__c, Client_Owner__c, ( Select Agency_Owner__c, Additional_Owner__c, Client_Owner__c, Id from Goal_Splits__r) from Goal__c where Id IN: trigger.new ];
		
		for( Goal__c g : goals )
		{
			if( trigger.oldMap.get( g.Id ).Agency_Owner__c != g.Agency_Owner__c
				|| trigger.oldMap.get( g.Id ).Additional_Owner__c != g.Additional_Owner__c
				|| trigger.oldMap.get( g.Id ).Client_Owner__c != g.Client_Owner__c )
			{
				for( Goal_Split__c gs : g.Goal_Splits__r )
				{
					gs.Additional_Owner__c = g.Additional_Owner__c;
					gs.Agency_Owner__c = g.Agency_Owner__c;
					gs.Client_Owner__c = g.Client_Owner__c;
					
					goalSplits.add( gs );
				}
			}
		}
		
		if( goalSplits.size() > 0 )
			update goalSplits;
	}
}
Let me know if you need any other help.

Thanks,
Neetu
This was selected as the best answer
Prentiss Jones 3Prentiss Jones 3
This is Great!!! Thank You SOOOO MUCH!!!! I just have one issue... It's with Line 41. 

Error: Compile Error: Didn't understand relationship 'Goal_Split__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 41 column 31
 
trigger CreateGoalSplits on Goal__c (after insert, after update) 
{
if( trigger.isInsert )
    {
        List<Goal_Split__c> goalSplits = new List<Goal_Split__c>();
        
        for( Goal__c goal : trigger.new )
        {
            if( goal.GoalQ1__c != null )
            {
                Goal_Split__c goalSplit = new Goal_Split__c( Name = GoalAmount__c, ClientOwner__c = goal.ClientOwner__c, AdditionalOwner__c = goal.AdditionalOwner__c, AgencyOwner__c = goal.AgencyOwner__c, Goal__c = goal.Id );
                goalSplits.add( goalSplit );
            }
            
            if( goal.GoalQ2__c != null )
            {
                Goal_Split__c goalSplit = new Goal_Split__c( Name = GoalAmount__c, ClientOwner__c = goal.ClientOwner__c, AdditionalOwner__c = goal.AdditionalOwner__c, AgencyOwner__c = goal.AgencyOwner__c, Goal__c = goal.Id );
                goalSplits.add( goalSplit );
            }
            
            if( goal.GoalQ3__c != null )
            {
                Goal_Split__c goalSplit = new Goal_Split__c( Name = GoalAmount__c, ClientOwner__c = goal.ClientOwner__c, AdditionalOwner__c = goal.AdditionalOwner__c, AgencyOwner__c = goal.AgencyOwner__c, Goal__c = goal.Id );
                goalSplits.add( goalSplit );
            }
            
            if( goal.GoalQ4__c != null )
            {
                Goal_Split__c goalSplit = new Goal_Split__c( Name = GoalAmount__c, ClientOwner__c = goal.ClientOwner__c, AdditionalOwner__c = goal.AdditionalOwner__c, AgencyOwner__c = goal.AgencyOwner__c, Goal__c = goal.Id );
                goalSplits.add( goalSplit );
            }
        }
        
        if( goalSplits.size() > 0 )
            insert goalSplits;
    }
    else if( trigger.isUpdate )
    {
        List<Goal_Split__c> goalSplit = new List<Goal_Split__c>();
        
        List<Goal__c> goals = [ Select Id, AgencyOwner__c, AdditionalOwner__c, ClientOwner__c, (Select AgencyOwner__c, AdditionalOwner__c, ClientOwner__c, Id from Goal_Split__r) from Goal__c where Id IN: trigger.new ];
        
        for( Goal__c g : goals )
        {
            if( trigger.oldMap.get( g.Id ).AgencyOwner__c != g.AgencyOwner__c
                || trigger.oldMap.get( g.Id ).AdditionalOwner__c != g.AdditionalOwner__c
                || trigger.oldMap.get( g.Id ).ClientOwner__c != g.ClientOwner__c )
            {
                for( Goal_Split__c gs : g.Goal_Split__r )
                {
                    gs.AdditionalOwner__c = g.Additional_Owner__c;
                    gs.AgencyOwner__c = g.AgencyOwner__c;
                    gs.ClientOwner__c = g.ClientOwner__c;
                    
                    goalSplit.add( gs );
                }
            }
        }
        
        if( goalSplits.size() > 0 )
            update goalSplits;
    }
}

 
Neetu_BansalNeetu_Bansal
Hi Prentiss,

This would be the child relationship name on the Goal Split object:

Go to the Goal Split object and verify open the Master detail/look up field of Goal. In the below:

Master-Detail Options

Related To Goal                     Child Relationship Name Goal_Splits__r

You need to use this child relationship name in the query at line 41.

Thanks,
Neetu
 
Prentiss Jones 3Prentiss Jones 3
The Master Detail Relationship is from the Goal Split to Goal. Should this be reversed?
Neetu_BansalNeetu_Bansal
Hi Prentiss,

It would not be reversed. when you open Goal Split object, there would be one field Goal of (Master Detail) type. In that field, you will find master detail options, verify the child relationship name there.

If you still not able to resolve this, you can provide me the login credentials either on skype neetu.bansal.5 or gmail neetu.bansal.5@gmail.com

Thanks,
Neetu
Prentiss Jones 3Prentiss Jones 3
OK....So I found it! The Field Name is Goal Number (API Name: is GoalNunber__c) Where would GoalNunber__c go?  

Now I'm getting this: Didn't understand relationship 'Goalnumber__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
 
else if( trigger.isUpdate )
    {
        List<Goal_Split__c> goalSplit = new List<Goal_Split__c>();
        
        List<Goal__c> goals = [ Select Id, AgencyOwner__c, AdditionalOwner__c, ClientOwner__c, (Select AgencyOwner__c, AdditionalOwner__c, ClientOwner__c, Id from Goalnumber__r) from Goal__c where Id IN: trigger.new ];
        
        for( Goal__c g : goals )
        {
            if( trigger.oldMap.get( g.Id ).AgencyOwner__c != g.AgencyOwner__c
                || trigger.oldMap.get( g.Id ).AdditionalOwner__c != g.AdditionalOwner__c
                || trigger.oldMap.get( g.Id ).ClientOwner__c != g.ClientOwner__c )
            {
                for( Goal_Split__c gs : g.Goal_Split__r )
                {
                    gs.AdditionalOwner__c = g.Additional_Owner__c;
                    gs.AgencyOwner__c = g.AgencyOwner__c;
                    gs.ClientOwner__c = g.ClientOwner__c;
                    
                    goalSplit.add( gs );
                }
            }
        }
        
        if( goalSplits.size() > 0 )
            update goalSplits;
    }
}



 
Neetu_BansalNeetu_Bansal
Its difficult to explain here, can you please provide me the credentials or contact me on any of my id.
Prentiss Jones 3Prentiss Jones 3
Sure!