Search code examples
salesforceapexvisualforce

Get id of parent object for the child object in apex


I have two objects (Warships and resource). both are custom objects. Now I want to add a new resource through the VF page. the resource__c is child of Warships__c(lookup relationship). the problem is I want to insert a new resource__c record but I can't find the id for the warship__c object just like (AccountId in the contact Object)

 Resource__c con = new Resource__c(Name=resName, Quantity__c=Quantity, Utilization__c=Utilize,Warship__c.id=id);

        // Warship__c.id or Warship__r.id etc??

        insert con;

please check the code for more details

public class WarshipManagerController {
    
    public String resName{get;set;}
    public Integer Quantity{get;set;}
    public Integer Utilize{get;set;}
    //public List<Resource__c>resList {get;set;} 
    public List<Warship__c>resList {get;set;}
    public List<Resource__c>newResList {get;set;}
    public string id;
    ApexPages.StandardController sc;

public  WarshipManagerController(ApexPages.StandardController sc){ 
    newResList =new List<Resource__c>();
    this.sc = sc; 
    string id=ApexPages.CurrentPage().getparameters().get('id');
   resList= new List <Warship__c>();
 
    for(Warship__c Ws:[SELECT Id ,(SELECT Id,Name, Utilization__c, Quantity__c FROM Resources__r) 
           FROM Warship__c where Id = :id] ){

         for( Resource__c Rs:Ws.Resources__r){
             newResList.add(Rs);
            
             
             
         }
       
        system.debug('---->'+newResList);
        
        
    }
 
   
          
}
    public pageReference Save(){
        Resource__c con = new Resource__c(Name=resName, Quantity__c=Quantity, Utilization__c=Utilize,Resources.Warship__r=id);
        
        insert con;
        return null; 
   }
}

VF page

<apex:page standardController="Warship__c" extensions="WarshipManagerController">
  <apex:form >
    <apex:pageBlock title="Resource">
        
         <apex:pageBlockSection >       
            First Name <apex:inputText value="{!resName}" />
            Last Name <apex:inputText value="{!Quantity}" />
            Phone <apex:inputText value="{!Utilize}" />
            <apex:commandButton value="save" action="{!save}" />
        </apex:pageBlockSection>
        
  <apex:dataTable value="{!newResList}" var="re" width="100%">
  <apex:column >
  <apex:facet name="header"><b>Resource</b></apex:facet>
     <apex:outputField value="{!re.Name}" /> 
             </apex:column>
              <apex:column >
  <apex:facet name="header"><b>Quantity</b></apex:facet>
     <apex:outputField value="{!re.Quantity__c}" /> 
             </apex:column>
              <apex:column >
  <apex:facet name="header"><b>utilization</b></apex:facet>
     <apex:outputField value="{!re.Utilization__c}" /> 
             </apex:column>
        </apex:dataTable>
 </apex:pageBlock>
  </apex:form>
</apex:page>

So how can I insert many recource__c records to Warship__C object just like Contact To Account


Solution

  • Do you have some training in normal relational databases? In Resource__c table the Warship__c column is Id column. It holds a reference (foreign key) to another table. If you type Warship__c you mean to use the ID. If you type Warship__r.Name you kind of jump over the relationship to the next table. You make a JOIN in the database and Warship__r becomes table alias.

    So if your string id holds the warship id (this is a terrible idea by the way, never call variables same as type names. Id is a type and you can screw your code up very nicely. Think Integer Account = 5; // happy debugging suckers)

    then you should be able to do new Resource__c(Name=resName, Quantity__c=Quantity, Utilization__c=Utilize,Warship__c=id);

    Check if this helps: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_understanding.htm

    Most of the time you'll use just the Id fields so simple references. There are occasions when you'll use Warship__r during insert/update but that's actually upsert operation and bit advanced topic.