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
Phuc Nguyen 18Phuc Nguyen 18 

Update Parent field after child created error

Trying to update lookup on record after child created.
So in the andFinally method, I am inserting the Project record.  I now want to take the id from the new project and use it to update the lookup on the Permit record.  But I am geting errors.  For starters it doe snot see the id field in teh andfinally method. ANy suggestions is appreciated
P
 
List<Project__c> permitProjectsToCreate = new List<Project__c>();
Project_Template__c permitTemplate;

public void initialize( String sObjectName ){
    
 List<Id> permitIds = new List<Id>();
     for Permit__c permit: trigger.New)
     {
        permitIds.add (permit.id);   
      }

    if( Trigger.isAfter && this.permitTemplate == null ){
        List<Project_Template__c> templates = [
            SELECT Id FROM Project_Template__c WHERE Name =: 'Permit Project' LIMIT 1
        ];

        if( !templates.isEmpty() ){
            this.permitTemplate = templates[ 0 ];
        }
    }

}

public void afterInsert( SObject so ){
    Permit__c newPermit = ( Permit__c ) so;
    

    if( permitTemplate != null ){
        permitProjectsToCreate.add( new Project__c(
            Permit__c = so.Id,
            ProjectTemplate__c = permitTemplate.Id
        ));
    }

}

public void andFinally(){

    if( Trigger.isInsert && !this.permitProjectsToCreate.isEmpty() ){
        insert this.permitProjectsToCreate;
    permitIds.project__c =         
    permitProjectsToCreate.id
    }        
}

 
Best Answer chosen by Phuc Nguyen 18
vijaya kumar 29vijaya kumar 29
Hi Phuc,


You call can  andFinally method in 



public void afterInsert( SObject so ){
Permit__c newPermit = ( Permit__c ) so;
      if( permitTemplate != null ){
        permitProjectsToCreate.add( new Project__c(
        Permit__c = so.Id,;
            ProjectTemplate__c = permitTemplate.Id
        ));
this.andFinally();

}

What ever I underLined ,Please Add to you code.




















 

All Answers

vijaya kumar 29vijaya kumar 29
Hi,
   In your code, your are try to insert  project and update permit record in andfinally  method. In andfinally methode your updating list of permit records, because of that only your getting error , you can't get lookup field in list of record.
Please change code while updating permit records.

I prepared this code for update permit records inside andfinally method,

For( permit__c ins:permitsIds){
ins.project__c=peritProjectsToCreate.Id;

}
 
Phuc Nguyen 18Phuc Nguyen 18
Thanks for the reply vijaya,
The Id is is stil throwing  'Variable does not exist: Id ' error.  Any ideas?
Is the for loop inside the if statement? 
vijaya kumar 29vijaya kumar 29

Hi,

Id error Becuease of permitProjectsToCreate is a list. You can not get Id from permitProjectsToCreate. So your are using single Parent Record I mean Only one Project record So you can make 

Project__c permitProjectsToCreate  =new Project__c();  in Place of      list<Projecct__c> permitProjectsToCreate =new list<Project__c>();

For( permit__c ins:permitsIds){
ins.project__c=peritProjectsToCreate.Id;

}

Phuc Nguyen 18Phuc Nguyen 18
Hey vijaya,
Do I need ti declare a new refrence?   When I modify the refernce from list<Projecct__c> permitProjectsToCreate =new list<Project__c>() to 
list<Projecct__c> permitProjectsToCreate =new list<Project__c>()  I get errors when for the insert.
Thank you,
P
vijaya kumar 29vijaya kumar 29
Hi Phuc Nguyen,

 See  permitProjectsToCreate is reference to list<Projects__c>. There your are store a list Of Project records, either your store  a single records also, it will stored in form of  List only.   So You can not get Id attribute from list. So you need use single instace.Do not use list<Projecct__c> permitProjectsToCreate =new list<Project__c>() to store a single Record. Use

Project__c  instance= new Project__c(
 Permit__c = so.Id,
 ProjectTemplate__c = permitTemplate.Id
);



public void andFinally(){
 if( Trigger.isInsert ){
    insert  instance;
permitIds.project__c = instance.id;
}
Phuc Nguyen 18Phuc Nguyen 18
Hello Vijaya,
Thank you for the reply. Only issue I am having now is that I get an error 'Variable does not exist: project__c'
Thank you,
P

 
vijaya kumar 29vijaya kumar 29
Hi,  
vijaya kumar 29
Hi,   
Now go to object manager and  go to permit__c object and  go to fields, in fields checks api name of lookup field of project__c  in permit_,c object and use same api name in apex also then you will slove problem. I think it will work

 
Phuc Nguyen 18Phuc Nguyen 18
Hey Vijaya,
I believe the field api name is correct.  I even tried other fields and I get the same error. 
Thanks,
P
vijaya kumar 29vijaya kumar 29
Hi,

I understand , in permitId list your are adding only ids from trigger, So you collect permit objects like

List<permit__c> permitIds = new List<permit_ _c>();
    for Permit__c permit: trigger.New)
    {        permitIds.add (permit);  
  }

Change above  code then it will properly.  I think after this it work.
 
Phuc Nguyen 18Phuc Nguyen 18
Hi Vijaya,
Still not working.  Maybe create another method from the andFinally method to do update?
 
vijaya kumar 29vijaya kumar 29
Hi Phuc,


You call can  andFinally method in 



public void afterInsert( SObject so ){
Permit__c newPermit = ( Permit__c ) so;
      if( permitTemplate != null ){
        permitProjectsToCreate.add( new Project__c(
        Permit__c = so.Id,;
            ProjectTemplate__c = permitTemplate.Id
        ));
this.andFinally();

}

What ever I underLined ,Please Add to you code.




















 
This was selected as the best answer