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
bhagavanmca04@yahoo.combhagavanmca04@yahoo.com 

I need simple Update method code for Custom Object.

Hi am new SFDC. i have Furniture__C custom object with Name,Type__C fields. So I need to insert,delete n update through code upto 100 records. i have exception in Update method. FATAL_ERROR|System.QueryException: List has more than 1 row for assignment to SObject. Furniture__c fr = [Select Name From Furniture__c Where Name = 'Das']; fr.Name = 'ram'; Update fr; any modifications or possibility to Update records. I need Update method code.

steve456steve456

Furniture__c fr = [Select Name From Furniture__c Where Name = 'Das' Limit 1];

 

Give this way

Devendra@SFDCDevendra@SFDC

Hi,

 

Give LIMIT to your query if query is returning more than one row.

Try this code: 

Furniture__c fr = [Select Name From Furniture__c Where Name = 'Das' LIMIT 1];
fr.Name = 'ram';
Update fr;

 Try below code for editing record:

 

public class MyClass
{
 public Furniture__c f{get; set;}
 String accId;
 public MyExtension(ApexPages.StandardController stdcon)
 {
  
   f= (Furniture__c)stdcon.getRecord();
 }
 
 public PageReference save()
 {
  update f;
   return null;   
 }
 
 public PageReference Cancel()
 {
  return null;
 }
}

//-----------visualforce page-------------------

<apex:page standardController="Furniture__c" extensions="MyClass"/>
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButton>
              <apex:commandButton value="Save" action="{!save}"/>
              <apex:commandButton value="Cancel" action="{!cancel}"/>
            <apex:pageBlockButton>
        
        
            <apex:pageBlockSection>
                <apex:pageBlockSectionItem>
                 <apex:inputField value="{!f.name}"/>
                 
                 <!-- like above field you can bind other fields of Furniture here -->
                 
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 Hope this helps:)

 

Thanks,

Devendra

Rajesh SriramuluRajesh Sriramulu

Hi

 

Yes Use limit on ur query so that it will not take more than 1.

SRIADISRIADI

You can do this way to update all records of Name='Das' to Name='ram' :

 

List<Furniture__c> objC = [Select Name From Furniture__c Where Name = 'Das'];
for(Furniture__cobjEach : objC)
{
objEach.Name = 'ram';
}
update objC;