【发布时间】: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