0

I am having an issue updating the Permit record after the project record(child) is created.
When a Permit record is created I need to also create a Project record. I then need to fetch the id from the newly create Project record and update the field permitIds.project__c that resides on the permit record. I am getting an error "Variable does not exist: Id" at: per.Permit_Project__c = permitProjectsToCreate.Id;

List<Project__c> permitProjectsToCreate = new List<Project__c>();
Project_Template__c permitTemplate;

public void initialize( String sObjectName ){
    
 List<Id> permitIds = new List<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 bulkBefore(){
    if( Trigger.isInsert){
        List<Id> permIds = new List<Id>();
    
        for( SObject record : Trigger.new ){
            Permit__c newperm = ( Permit__c ) record;
                permIds.add( newperm.Id );
        }
    }
}
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;
    }
For(Permit__c per :permIds){
        per.Permit_Project__c = permitProjectsToCreate.Id;
    }        
}
4
  • Hi @Phuc, please share the error you get
    – Json
    Commented Aug 5, 2020 at 13:43
  • I updated the class and now just getting the error here: per.Permit_Project__c = permitProjectsToCreate.Id; "Variable does not exist: Id"
    – Phuc
    Commented Aug 5, 2020 at 19:36
  • I looked at your code couple of times and it is confusing because the code and the question are not coherent. Do you need to just update any Permit record after project record is created or is there any identifier to update a particular set of Permit records? You need to start your context from Project record after insert perspective
    – Badduboy
    Commented Aug 5, 2020 at 19:42
  • Thanks for the reply. So when a Permit record is created I need to also create a Project record. The look up field(Permit_Project__c) on the Permit record needs to populated with the newly created Project record.
    – Phuc
    Commented Aug 5, 2020 at 19:48

1 Answer 1

0

You cannot do this:

For(Permit__c per :permIds){
        per.Permit_Project__c = permitProjectsToCreate.Id;
    }        
}

because permitProjectsToCreate is a List<Project__c>. That type has no Id property.

There are some other reasons this code won't work, though. In particular,

public void bulkBefore(){
    if( Trigger.isInsert){
        List<Id> permIds = new List<Id>();
    
        for( SObject record : Trigger.new ){
            Permit__c newperm = ( Permit__c ) record;
                permIds.add( newperm.Id );
        }
    }
}

Objects do not have Ids in before insert context.

It seems you're trying to construct a one-to-one relationship where each object has a relationship to the other side of a pair. That's a pretty uncommon data structure on Salesforce, and I don't really recommend it. It appears to me that you need to be doing all of your work in after insert, where you'll (1) do inserts of Permit Project records and then (2) put the newly-generated Ids back into Permit records and update those Permit records.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .