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
Poorna Priya 16Poorna Priya 16 

Trigger to Fields Auto populate

I have Custom object named Team__c and Fields are like, Category__c(Picklist), Close_Date__c(Date), Stage__c(Picklist) also have Standard Opportunity object. The Team__c object and Opportunity has look-up relationship with Category__c(Picklist), Close_Date__c(Date), Stage__c(Picklist) fields . In opportunity I have custom field named Type__c(Picklist), whenever the Type__c from Opportunity is equal to 'New Team' (Type__c == 'New Team') the Category__c, Close_Date__c, Stage__c fields should populate in Team__c record while it create. Can anyone help on this? Thanks in advance. I have try with following trigger, 
if(trigger.isAfter && trigger.isInsert) 
{
 List<Team__c> tt = new List<Team__c>(); 
Opportunity opp = new Opportunity(); 
if(opp.Type__c == 'New Team')
 {
 for(Opportunity oppty : trigger.new) 
{
 Team__c team = new Team__c(); 
team.Category__c = oppty.Category__c; 
team.Close_Date__c = oppty.Close_Date__c; 
team.Stage__c = oppty.Stage__c; 
tt.add(team); 
}
 }
 }

 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you confirm which is parent and which is child in the relationship and also . I guess Opportunity  is parent and Team is child here, If the type is newteam then when new team craeted all the fields should popuate from opportuntiy ?

Are the fields close date, Stage custom fields in opporunity?

Thanks,
 
Poorna Priya 16Poorna Priya 16

Hi Sai,

Yes,  Opportunity  is parent and Team is child. 
If the type is newteam then when new team craeted all the fields should popuate from opportuntiy ? --> Yes
Are the fields close date, Stage custom fields in opporunity? --> Yes

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Priya, 

Thanks for confirming the details. Can you try below trigger on team__c object.
 
trigger NewTeam on Team__c (before insert) {
    
    set<id> oppyid= new set<id>();
    for(Team__c t:Trigger.new){
        oppyid.add(t.opportunity__c);
    }
    
    map<id,opportunity> mapopp=new map<id,opportunity>([select id,Type__c,Category__c,Close_Date__c,Stage__c from opportunity where Type__c='New Team']);
    
    for(Team__c t:Trigger.new){
        
        if(mapopp.containsKey(t.Opportunity__c)){
            t.Category__c=mapopp.get(t.Opportunity__c).Category__c;
t.Close_Date__c=mapopp.get(t.Opportunity__c).Close_Date__c;
t.Stage__c=mapopp.get(t.Opportunity__c).Stage__c;
        }
    }
    

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Priya, 

Did you get chance to check the above trigger logic. Let me know if you face any issues

Thanks,