+ Start a Discussion
CvrKCvrK 

Need help with the simple trigger?

Hi ,
1) Iam New to development,now trying to create a detail record as a result based on after record inserted on its master object,but i am failing to do so
Client__c is master object and project__c is Child Object,

Trigger code:

trigger AutoProjects on Client__c (After insert) {
    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.Project.Id=Clnt.Id;
            NewProjs.add(p);
        }
        Insert NewProjs;
    }

2) I read that trigger.new does not work for after insert/update triggers but i have seen in many examples where trigger.new is used in after triggers,so wondering if trigger.new works or not for after events in any situation.

below example trigger for the reference that used trigger.new for After Insert event .

trigger AutoOpp on Account(after insert) {
  List<Opportunity> newOpps = new List<Opportunity>();
  for (Account acc : Trigger.new) {
    Opportunity opp = new Opportunity();
    opp.Name        = acc.Name + ' Opportunity';
    opp.StageName   = 'Prospecting';
    opp.CloseDate   = Date.today() + 90;
    opp.AccountId   = acc.Id; // Use the trigger record's ID
    newOpps.add(opp);
  }
  insert newOpps;
}

 
Best Answer chosen by CvrK
Arunkumar RArunkumar R
Hi,

Go to detail view of the Child object(project__c) and find the API Name that is relating to parent object(Client__c).

Copy the API name and paste in your code,

Replace:

p.Project.Id=Clnt.Id;

To:

p.MasterAPIName__c = cInt.Id;
trigger AutoProjects on Client__c (After insert) {
    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.MasterAPIName__c = cInt.Id;
            NewProjs.add(p);
        }
        Insert NewProjs;
    }

User-added image

In the above screen shot, customer media is child and customer detail is parent object.

All Answers

Vishal Negandhi 16Vishal Negandhi 16
you might correct this line:

 p.Project.Id=Clnt.Id;
to
p.Project__c = Clnt.Id;

This should fix it!
Arunkumar RArunkumar R
Hi,

Go to detail view of the Child object(project__c) and find the API Name that is relating to parent object(Client__c).

Copy the API name and paste in your code,

Replace:

p.Project.Id=Clnt.Id;

To:

p.MasterAPIName__c = cInt.Id;
trigger AutoProjects on Client__c (After insert) {
    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.MasterAPIName__c = cInt.Id;
            NewProjs.add(p);
        }
        Insert NewProjs;
    }

User-added image

In the above screen shot, customer media is child and customer detail is parent object.
This was selected as the best answer
CvrKCvrK
Thanks Vishal Negandhi 16 and Arunkumar R both of you are right,i jst added Id to the API name that was the mistake.
Would you please look into my 2nd scenario in the same question,looks like both of you are missed. 
Arunkumar RArunkumar R
Hi,

Just take a below link examples, you will get a clear idea about trigger context.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm

https://help.salesforce.com/apex/HTViewSolution?id=000003789&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000003789&language=en_US)


If you question resolved, then mark this solution..