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
Ian DoneyIan Doney 

Custom save button on visualforce page not saving all information

I have a visualforce page located on the case object that shows a table with assets that are related to that case. From the asset table you can update the asset serial number, click the custom save button and the asset serial number updates and saves. The issue I am running into is that I added a checkbox to the asset table from the related case. When the checkbox is checked in the asset table and the save button is clicked the checkbox field on the case does not update or save. 

Here is my visualforce page code.
<apex:page standardController="Case" extensions="pullEquipmentUpdateAssetClass">     

    <p></p>
    
<!--List of Assets. Can update serial number of the asset from this table-->
    
    <apex:pageblock id="Assets"> 

         <br/> 
        
			<apex:form >
                
                <apex:pageBlockTable value="{!Assets}" var="asset">
                
                	<apex:column value="{!asset.Product_Code_Display__c}"/>
                
                	<apex:column value="{!asset.Product_Description_Display__c}"/> 
                    
                    <apex:column value="{!asset.Delivery_Address__c}"/>
                
					<apex:column value="{!asset.Serial_Number_Procurement__c}"/>
					
                    <apex:column headerValue="Serial Number"><apex:inputText value="{!asset.Serial_Number_Procurement__c}" required="true" /></apex:column>
					
                    <!-- this checkbox can be checked, but when the save button is clicked the checkbox field on the case does not update -->
                    <apex:column headerValue="Equipment has been pulled"><apex:inputCheckbox value="{!asset.Child_Fulfillment_Ticket__r.Equipment_Has_Been_Pulled__c}"/></apex:column>
                
                </apex:pageBlockTable>
               
                <apex:commandButton value="Save" action="{!saveSerialIdNumber}"/>
        
        </apex:form>
       
    </apex:pageblock>
    
</apex:page>
And here is my apex class:
public class pullEquipmentUpdateAssetClass {


    public List<Asset> Assets{get;set;}
    
    public Case Cases {get;set;} 

    public Case c {get;set;} 
    
    //Constructor 

     public pullEquipmentUpdateAssetClass(ApexPages.StandardController controller) { 

        c = (Case)controller.getRecord();      

        Cases = [SELECT id, FROM Case WHERE id=: c.id LIMIT 1]; 

        Assets = [SELECT id,Child_Fulfillment_Ticket__r.Equipment_Has_Been_Pulled__c,Serial_Number_Procurement__c,Name,Delivery_Address__c,Product_Code_Display__c,Product_Description_Display__c FROM Asset WHERE Serialized__c = true AND Child_Fulfillment_Ticket__c = :Cases.id]; 
     
     } 
  
    //save button for serial number
    
    public PageReference saveSerialIdNumber(){
        update Assets;
        Assets = [SELECT id,Child_Fulfillment_Ticket__r.Equipment_Has_Been_Pulled__c,Serial_Number_Procurement__c,Name,Delivery_Address__c,Product_Code_Display__c,Product_Description_Display__c FROM Asset WHERE Serialized__c = true AND Child_Fulfillment_Ticket__c = :Cases.id]; 
return ApexPages.currentPage();
    }
}

I am still fairly new to apex, so I am not super familiar with it 
Best Answer chosen by Ian Doney
Santosh Kumar 348Santosh Kumar 348
You need to update case as well.
As you have already fetched cases on line 15. So just append below code on line 26.

Line 15 :
Cases = [SELECT id, Equipment_Has_Been_Pulled__c FROM Case WHERE id=: c.id LIMIT 1];
Line 26:
Cases.Equipment_Has_Been_Pulled__c = Assets.Equipment_Has_Been_Pulled__c;
update cases;

This will solve your issue.

Do let me know if it helps you and close your query by marking it as solved.

Regards,
Santosh

 

All Answers

Santosh Kumar 348Santosh Kumar 348
You need to update case as well.
As you have already fetched cases on line 15. So just append below code on line 26.

Line 15 :
Cases = [SELECT id, Equipment_Has_Been_Pulled__c FROM Case WHERE id=: c.id LIMIT 1];
Line 26:
Cases.Equipment_Has_Been_Pulled__c = Assets.Equipment_Has_Been_Pulled__c;
update cases;

This will solve your issue.

Do let me know if it helps you and close your query by marking it as solved.

Regards,
Santosh

 
This was selected as the best answer
Ian DoneyIan Doney
Hi Santosh, Thank you for the reply. I have added your code to lines 15 and 26 of my class but I am now getting an error "Variable does not exist: Equipment_Has_Been_Pulled__c"
public class pullEquipmentUpdateAssetClass {

    public List<Asset> Assets{get;set;}
    
    public Case Cases {get;set;} 

    public Case c {get;set;} 
    
    //Constructor 

     public pullEquipmentUpdateAssetClass(ApexPages.StandardController controller) { 

        c = (Case)controller.getRecord();      

        Cases = [SELECT id, Equipment_Has_Been_Pulled__c FROM Case WHERE id=: c.id LIMIT 1];

        Assets = [SELECT id,Child_Fulfillment_Ticket__r.Equipment_Has_Been_Pulled__c,Serial_Number_Procurement__c,Name,Delivery_Address__c,Product_Code_Display__c,Product_Description_Display__c FROM Asset WHERE Serialized__c = true AND Child_Fulfillment_Ticket__c = :Cases.id]; 
     
     } 
  
    //save button for serial number
    
    public PageReference saveSerialIdNumber(){
        update Assets;
        Cases.Equipment_Has_Been_Pulled__c = Assets.Equipment_Has_Been_Pulled__c;
		update cases;
        Assets = [SELECT id,Child_Fulfillment_Ticket__r.Equipment_Has_Been_Pulled__c,Serial_Number_Procurement__c,Name,Delivery_Address__c,Product_Code_Display__c,Product_Description_Display__c FROM Asset WHERE Serialized__c = true AND Child_Fulfillment_Ticket__c = :Cases.id];
        return ApexPages.currentPage();
    }
}

 
Santosh Kumar 348Santosh Kumar 348
On which line you are getting the error as well as could you please let me know on which object you are having this field 'Equipment_Has_Been_Pulled__c ".
Ian DoneyIan Doney
Santosh,

I got it working, thank you for the help