+ Start a Discussion
Santanu HalderSantanu Halder 

Insert single record using Wrapper Class

Hi, I am able to show records with fields from different objects using Wrapper class. Now I want to create a new record of those objects using Wrapper Class. I am quite sure its possible. I am missing some link somewhere. Need your help.

I have Incident__c object with status fields and has a master detail relationship with Comment__c. Everytime a comment is inserted for any incident, user will update the status of the incident as well.

Here is what I have done till now
My Wrapper class
public class CommentWrapperController {
ApexPages.StandardController stdCtlr;
    public CommentWrapperController(ApexPages.StandardController std)
    {
        stdCtlr = std;
    }
public class CommentWithStatus // This is the Wrapper class using Comment Custom object and Status field from Incident custom object.
{
    Comment__c com{get; set;}
    String status{get; set;}
    public CommentWithStatus(Comment__c c, String st)
    {
        com = c;
        status = st;
    }
}
      
    public void saveComment()
    {
        CommentWithStatus cws = (CommentWithStatus)stdCtlr.getRecord(); //Throwing error
        Comment__c com = cws.getComment();
 //    TO DO: here I want to have the Comment__c record & insert it, after that status field value & update the Incident__c record.  
    }  
}

My VF page
<apex:page controller="CommentWrapperController"> <!-- Can I use standardController here? -->
  <apex:form >
  <apex:pageBlock title="block">
 <apex:pageBlockSection>
 <apex:inputField value="{!CommentWithStatus.com.Comment_Body__c}"/> <!-- I messed up here -->
  </apex:pageBlockSection>
  <apex:pageBlockButtons >
  <apex:commandButton value="Save" action="{!saveComment}"/>
  </apex:pageBlockButtons>
  </apex:pageBlock>
  </apex:form>
</apex:page>
sandeep@Salesforcesandeep@Salesforce
For creating records you need to insert instance of the object which you want to create. Here these object will be as property of the wrapper class for example 
Public class A{
Public ClassB{
Account A;
}
}

so we need to insert A