function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhiman MungalAbhiman Mungal 

i have one question

public with sharing class OrderApex {

public Order__c c{get; set;}

public String nameuser{get; set;}

public boolean show{get;set;}
public boolean Edit{get;set;}
public boolean save{get;set;}

 public OrderApex(ApexPages.StandardController controller) {
 c=new Order__c ();
 if(c==null)
 {
     save=true;
     show=false;
     Edit=false; 
 }
 else
 {
     nameuser=userInfo.getName();
     c.Customer_Name__c=nameuser;
     try
     {
     c=[select Name,Address__c,Booking_Date__c,Time__c,customerName__c,Customer_Name__c from Order__c limit 1];

 save=false;
 show=true;
 Edit=false; 
 }
 catch(Exception e) 
 { } 
 }
 }
 
 
 public pageReference save(){
 
 insert c;
  c.Customer_Name__c=nameuser;
 c=[select Name,Address__c,Booking_Date__c,Time__c,customerName__c from Order__c limit 1];
 
 show=true;
 save=false;
 
 return null;
 }
 
 public pageReference editData(){
 
 Edit=true;
 show=false;
 save=false;
 
 c=[select Name,Address__c,Booking_Date__c,Time__c,customerName__c from Order__c limit 1];
 
 
 return null;
 }
 
 public pageReference editAfterSave(){
 upsert c;
 
 c=[select Name,Address__c,Booking_Date__c,Time__c,customerName__c from Order__c where Customer_Name__c=:nameuser limit 1];

 Edit=false;
 show=true;
 save=false;
  
 return null;
 }
 
}


<apex:page standardcontroller="Order__c" extensions="OrderApex" sidebar="false" >
 <apex:form >
 <apex:pageBlock rendered="{!save}" title="Save Profile">
 <apex:pageblockSection >
 
 <apex:inputField value="{!c.Name}"/>
 <apex:inputField value="{!c.Address__c}"/>
 <apex:inputField value="{!c.Booking_Date__c}"/>
 <apex:inputField value="{!c.Time__c}"/>
 <apex:inputField value="{!c.customerName__c}"/>
 <apex:inputField value="{!c.Customer_Name__c}"/>
 
 <apex:commandButton action="{!save}" value="SaveData"/>
 </apex:pageblockSection>
 </apex:pageBlock>

 <apex:pageBlock rendered="{!show}" title="Show Profile">
 <apex:pageblockSection >

 <apex:outputText value="{!c.Name}"/>
 <apex:outputText value="{!c.Address__c}"/>
 <apex:outputText value="{!c.Booking_Date__c}"/>
 <apex:outputText value="{!c.Time__c}"/>
 <apex:outputText value="{!c.customerName__c}"/>
 <apex:outputText value="{!c.Customer_Name__c}"/>
   
 <apex:commandButton action="{!editData}" value="Edit"/>
 </apex:pageblockSection>
 </apex:pageBlock>

 <apex:pageBlock rendered="{!Edit}" title="Edit Profile">
 <apex:pageblockSection >
 
 
 <apex:inputField value="{!c.Name}"/>
 <apex:inputField value="{!c.Address__c}"/>
 <apex:inputField value="{!c.Booking_Date__c}"/>

 <apex:inputField value="{!c.Time__c}"/>
 <apex:inputField value="{!c.customerName__c}"/>
 <apex:inputField value="{!c.Customer_Name__c}"/>
   
 
 <apex:commandButton action="{!editAfterSave}" value="save"/>
 </apex:pageblockSection>
 </apex:pageBlock>


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




when i try to run this code it will give me error like 


System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Order__c.Customer_Name__c 

what i have to do???
GAURAV CHOPRAGAURAV CHOPRA
 c=[select Name,Address__c,Booking_Date__c,Time__c,customerName__c from Order__c where Customer_Name__c=:nameuser limit 1]; 

Here if you see you are using the Customer_Name__c in WHERE comdition so in Response this field data is absent. This is why you are getting this error. (I think by mistake you have used customerName__c  in place of Customer_Name__c)

Change this to 

 c=[select Name,Address__c,Booking_Date__c,Time__c,customerName__c, Customer_Name__c from Order__c where Customer_Name__c=:nameuser limit 1]; 
Sohan Raj GuptaSohan Raj Gupta
Hi Abhiman,

As per you code you want to show order information with save and edit feature. When user will Save/Edit order information then you want to show updated information on same page.

According to your code you are getting Customer_Name__c only in constructor, and when user click on Save/Edit button then constructor will not call that time. And you are updating Order__c instance c in Save/Edit function without fetching Customer_Name__c field. Thats why it gives that error.

You should use below code:
 
public with sharing class OrderApex {

public Order__c c{get; set;}

public String nameuser{get; set;}

public boolean show{get;set;}
public boolean Edit{get;set;}
public boolean save{get;set;}

 public OrderApex(ApexPages.StandardController controller) {
 c=new Order__c ();
 if(c==null)
 {
     save=true;
     show=false;
     Edit=false; 
 }
 else
 {
     nameuser=userInfo.getName();
     c.Customer_Name__c=nameuser;
     try
     {
     c=[select Name,Address__c,Booking_Date__c,Time__c,customerName__c,Customer_Name__c from Order__c limit 1];

 save=false;
 show=true;
 Edit=false; 
 }
 catch(Exception e) 
 { } 
 }
 }
 
 
 public pageReference save(){
 
 insert c;
  c.Customer_Name__c=nameuser;
 c=[select Name,Address__c,Booking_Date__c,Time__c,customerName__c, Customer_Name__c from Order__c limit 1];
 
 show=true;
 save=false;
 
 return null;
 }
 
 public pageReference editData(){
 
 Edit=true;
 show=false;
 save=false;
 
 c=[select Name,Address__c,Booking_Date__c,Time__c,customerName__c, Customer_Name__c  from Order__c limit 1];
 
 
 return null;
 }
 
 public pageReference editAfterSave(){
 upsert c;
 
 c=[select Name,Address__c,Booking_Date__c,Time__c,customerName__c, Customer_Name__c from Order__c where Customer_Name__c=:nameuser limit 1];

 Edit=false;
 show=true;
 save=false;
  
 return null;
 }
 
}

Hope this will help you. Let me know if it helped or you need any more assistance. 

Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta 
Abhiman MungalAbhiman Mungal
no its not working 
i think u didn't understand my question ...
i want to store current user name in order__c object as Customer_name__c field  automaticaly and when i want to update these records that time it should not be change 
 
Sohan Raj GuptaSohan Raj Gupta
Hi Abhiman,

Your problem is solved or not? If you need help in this point then let me know.

Regards,
Sohan Raj Gupta