如何将MasterDetails字段插入控制器Salesforce?

发布于 2025-02-01 10:05:12 字数 3556 浏览 3 评论 0原文

在Stud Sobject中,有两个Master_Deatils,即课程和教练名称。我需要插入数据。但无法插入。请帮助我...提前thaks ..

< apex:page controller =“ insert_cc_class”> < apex:form>

 <apex:pageBlock >
   <apex:pageBlockButtons >
     <apex:commandButton value="SAVE" action="{!save}"/>
     <apex:commandButton value="CANCEL" action="{!cancel}"/>
     </apex:pageBlockButtons>
     
     <apex:pageBlockSection title="Student Details">
     
      <apex:pageBlockSectionItem >
        <apex:outputLabel value="Student Name"/>
        <apex:inputText value="{!Sname}" />
      </apex:pageBlockSectionItem><br/>
     
       
      <apex:pageBlockSectionItem >
        <apex:outputLabel value="Contact Number"/>
        <apex:inputText value="{!Snumber}"/>
      </apex:pageBlockSectionItem><br/>
       
      <apex:pageBlockSectionItem >
        <apex:outputLabel value="Course Name"/>
        <apex:inputText value="{!SCourseName}" />
      </apex:pageBlockSectionItem><br/>
      
      <apex:pageBlockSectionItem >
       <apex:outputLabel value="Trainer Name"/>
       <apex:inputText value="{!trainername}"/>
      </apex:pageBlockSectionItem><br/>
      
      <apex:pageBlockSectionItem >
        <apex:outputLabel value="Date Of Birth"/>
         <apex:inputText value="{!dob}"/> 
        </apex:pageBlockSectionItem><br/>
     
     <apex:pageBlockSectionItem >
        <apex:outputLabel value="Qualification"/>
        <apex:selectList size="1">
      <apex:selectOption itemValue="B-Tech" />
      <apex:selectOption itemValue="Diploma" />
      <apex:selectOption itemValue="Graduate" />
      <apex:selectOption itemValue="MBA" />
      <apex:selectOption itemValue="MCA" />
      <apex:selectOption itemValue="PG" />
      <apex:selectOption itemValue="PHD" />
      <apex:selectOption itemValue="Under Graduate" />
      </apex:selectList>
     </apex:pageBlockSectionItem><br/>
          
     <apex:pageBlockSectionItem >
        <apex:outputLabel value="State"/>
        <apex:inputText value="{!state}"/> 
     </apex:pageBlockSectionItem><br/>
     
     <apex:pageBlockSectionItem >
       <apex:outputLabel value="City"/>
       <apex:inputText value="{!city}"/> 
     </apex:pageBlockSectionItem>
     
    </apex:pageBlockSection>
 </apex:pageBlock>

&lt;/apex:form&gt; &lt;/apex:page&gt;

公共类Insert_cc_class {

// private Apexpages.standardController Controller;

public String city { get; set; }

public String state { get; set; }

public String qualification { get; set; }

public integer dob { get; set; }

public String trainername { get; set; }

public String SCourseName { get; set; }

public integer Snumber { get; set; }

public String Sname { get; set; }

public PageReference cancel() {
    return null;
}

public pagereference save(){

Stud__C obj = new Stud__C();

     obj.Sname__c = Sname;
     obj.Contact_Number__c = String.valueOf(Snumber);
     obj.Qualification__c = qualification;
     obj.DOB__c = date.ValueOf(dob);
     obj.Course_Name__c = SCourseName;
     obj.Trainer_Name__c = trainername;
     obj.State__c = state;
     obj.City__c = city;
     
    Insert obj;
    return null;
}

}

Here in stud Sobject there are two master_deatils i.e course and trainer name. I need to insert the data. But not able to insert.Please help me...Thaks In Advance..

<apex:page controller="insert_cc_Class">
<apex:form >

 <apex:pageBlock >
   <apex:pageBlockButtons >
     <apex:commandButton value="SAVE" action="{!save}"/>
     <apex:commandButton value="CANCEL" action="{!cancel}"/>
     </apex:pageBlockButtons>
     
     <apex:pageBlockSection title="Student Details">
     
      <apex:pageBlockSectionItem >
        <apex:outputLabel value="Student Name"/>
        <apex:inputText value="{!Sname}" />
      </apex:pageBlockSectionItem><br/>
     
       
      <apex:pageBlockSectionItem >
        <apex:outputLabel value="Contact Number"/>
        <apex:inputText value="{!Snumber}"/>
      </apex:pageBlockSectionItem><br/>
       
      <apex:pageBlockSectionItem >
        <apex:outputLabel value="Course Name"/>
        <apex:inputText value="{!SCourseName}" />
      </apex:pageBlockSectionItem><br/>
      
      <apex:pageBlockSectionItem >
       <apex:outputLabel value="Trainer Name"/>
       <apex:inputText value="{!trainername}"/>
      </apex:pageBlockSectionItem><br/>
      
      <apex:pageBlockSectionItem >
        <apex:outputLabel value="Date Of Birth"/>
         <apex:inputText value="{!dob}"/> 
        </apex:pageBlockSectionItem><br/>
     
     <apex:pageBlockSectionItem >
        <apex:outputLabel value="Qualification"/>
        <apex:selectList size="1">
      <apex:selectOption itemValue="B-Tech" />
      <apex:selectOption itemValue="Diploma" />
      <apex:selectOption itemValue="Graduate" />
      <apex:selectOption itemValue="MBA" />
      <apex:selectOption itemValue="MCA" />
      <apex:selectOption itemValue="PG" />
      <apex:selectOption itemValue="PHD" />
      <apex:selectOption itemValue="Under Graduate" />
      </apex:selectList>
     </apex:pageBlockSectionItem><br/>
          
     <apex:pageBlockSectionItem >
        <apex:outputLabel value="State"/>
        <apex:inputText value="{!state}"/> 
     </apex:pageBlockSectionItem><br/>
     
     <apex:pageBlockSectionItem >
       <apex:outputLabel value="City"/>
       <apex:inputText value="{!city}"/> 
     </apex:pageBlockSectionItem>
     
    </apex:pageBlockSection>
 </apex:pageBlock>

</apex:form>
</apex:page>

public class insert_cc_Class {

// private ApexPages.StandardController controller;

public String city { get; set; }

public String state { get; set; }

public String qualification { get; set; }

public integer dob { get; set; }

public String trainername { get; set; }

public String SCourseName { get; set; }

public integer Snumber { get; set; }

public String Sname { get; set; }

public PageReference cancel() {
    return null;
}

public PageReference save() {

Stud__C obj = new Stud__C();

     obj.Sname__c = Sname;
     obj.Contact_Number__c = String.valueOf(Snumber);
     obj.Qualification__c = qualification;
     obj.DOB__c = date.ValueOf(dob);
     obj.Course_Name__c = SCourseName;
     obj.Trainer_Name__c = trainername;
     obj.State__c = state;
     obj.City__c = city;
     
    Insert obj;
    return null;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咽泪装欢 2025-02-08 10:05:12

这是非常复杂的方法。您是否真的需要所有这些“ outputLabel,InputText”等?如果您使用Apex:InputField(它神奇地知道字段标签以及字段类型是否为文本/日期/选择列表),则更简单,并且它也可以让您轻松显示查找。

当您无法做到这一点时,很少有这样的情况(需要绕过SF配置文件字段权限),但它在“我真的知道我在做什么”类别中。对于这些,如果您不能仅显示查找,则需要手工制作一些搜索 +自动完成解决方案。网络上应该有很多示例如何按名称功能创建VF+APEX搜索。

虽然这确实使轮子发动机。认真地考虑使用&lt; apex:inputfield&gt;

public class Stack72386167{

    public Contact con {get;set;}
    
    public Stack72386167(){
        con = new Contact();
    }
    
    public void save(){
        insert con;
    }
}

<apex:page controller="Stack72386167">
<apex:form>
    <apex:pageBlock>
        <apex:pageBlockButtons>
            <apex:commandButton value="Save" action="{!save}" />
        </apex:pageBlockButtons>
        <apex:pageBlockSection columns="1">
            <apex:inputField value="{!con.Salutation}" /> <!-- picklists -->
            <apex:inputField value="{!con.FirstName}" />
            <apex:inputField value="{!con.LastName}" />
            <apex:inputField value="{!con.AccountId}" />  <!-- lookups
            <apex:inputField value="{!con.Birthdate}" />  <!-- dates (and of course right field labels picked for "free", admin can change them without code changes -->
            
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

”在此处输入图像描述”

this is very complicated way to do it. Do you really need all these "outputLabel, inputText" etc? It's simpler if you use apex:inputField (it'd magically know the field label and whether the field type is text/date/picklist) and it'd let you easily display lookups too.

There are rare situations when you can't do that (need to bypass SF profile field permissions) but it's in "I really know what I'm doing" category. And for these if you can't just display a lookup you need to hand-craft some search + autocomplete solution. There should be lots of examples on net how to create VF+apex search by name functionality.

That's really reiinventing the wheel though. Seriously, consider using <apex:inputField>

public class Stack72386167{

    public Contact con {get;set;}
    
    public Stack72386167(){
        con = new Contact();
    }
    
    public void save(){
        insert con;
    }
}

<apex:page controller="Stack72386167">
<apex:form>
    <apex:pageBlock>
        <apex:pageBlockButtons>
            <apex:commandButton value="Save" action="{!save}" />
        </apex:pageBlockButtons>
        <apex:pageBlockSection columns="1">
            <apex:inputField value="{!con.Salutation}" /> <!-- picklists -->
            <apex:inputField value="{!con.FirstName}" />
            <apex:inputField value="{!con.LastName}" />
            <apex:inputField value="{!con.AccountId}" />  <!-- lookups
            <apex:inputField value="{!con.Birthdate}" />  <!-- dates (and of course right field labels picked for "free", admin can change them without code changes -->
            
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文