【问题标题】:Get id of parent object for the child object in apex获取顶点中子对象的父对象ID
【发布时间】:2021-06-21 08:30:06
【问题描述】:

我有两个对象(军舰和资源)。两者都是自定义对象。现在我想通过 VF 页面添加一个新资源。 resource__c 是 Warships__c(查找关系)的子项。问题是我想插入一个新的resource__c 记录,但我找不到warship__c 对象的ID,就像(联系人对象中的AccountId)

 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;

详情请查看代码

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 页面

<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>

那么我怎样才能像 Contact To Account 一样向 Warship__C 对象插入许多 reource__c 记录

【问题讨论】:

    标签: salesforce apex visualforce


    【解决方案1】:

    您是否接受过普通关系数据库方面的培训?在 Resource__c 表中,Warship__c 列是 Id 列。它保存对另一个表的引用(外键)。如果您键入Warship__c,您的意思是使用 ID。如果您输入Warship__r.Name,您会跳过与下一张表的关系。您在数据库中进行 JOIN,Warship__r 成为表别名。

    所以如果你的string id 持有军舰ID(顺便说一句,这是一个糟糕的主意,永远不要调用与类型名称相同的变量。Id 是一种类型,你可以很好地搞砸你的代码。想想@987654326 @)

    那么你应该可以做到new Resource__c(Name=resName, Quantity__c=Quantity, Utilization__c=Utilize,Warship__c=id);

    检查是否有帮助:https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_understanding.htm

    大多数情况下,您只会使用 Id 字段这样简单的引用。有时您会在插入/更新期间使用 Warship__r,但这实际上是 upsert 操作和一些高级主题。

    【讨论】:

      猜你喜欢
      • 2020-01-26
      • 2022-01-04
      • 2022-12-15
      • 2015-03-08
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 2017-12-08
      • 2017-11-23
      相关资源
      最近更新 更多