0

I have created visual force page for updating records from the page. . in my code Owner__c is Parent, Vehicle__c is Child. once i enter value owner object discount__c field , i need to update same value in Vehicle object Disc__c filed. Here Owner object have number of vehicles. Once I click the save button i am getting "Refuse to connect" error, after clicking save button need to redirect same page. save button is not working and not updating my data in dynamically. Why this happening? How can I resolve this issue. Please can any one help me out. Please find the below my code.

Page:

<div class="slds-col slds-size_6-of-12">
                            <label class="slds-form-element__label"> Discount:
                                 </label>
                           <b><label class="slds-form-element__label"><apex:inputfield value="{!Owner__c.Discount__c}"/></label></b> 

                        </div>

      <apex:commandButton styleClass="slds-button slds-button_brand" status="status"
                                                        action="{!Save}"
                                                        reRender="none" value="Save" id="saveButton"/>

Controller:

public with sharing class OwnervehicleController {

  public List<Vehicle__c> Retest{ get; set; }
    public Owner__c Offer {get;set;}
    public String OwnrId { get; set; }

     public OwnervehicleController(ApexPages.StandardController sc) {

        OwnrId = sc.getId();

        Retest = [
                SELECT Id,Name,Owner__c,Disc__c FROM Vehicle__c
                WHERE Owner__c = :OwnrId ];
        Offer = [SELECT id,Discount__c FROM Owner__c 
                 WHERE id = : OwnrId];
    }

    public PageReference Save(){   


    Owner__c sc=[select id,Discount__c from Owner__c where id=:OwnrId];    

    update sc; 

    list<Vehicle__c> Vcle=[select id,Disc__c,Owner__c From Vehicle__c where Owner__c=:sc.id];

    for(Vehicle__c oitem:Vcle)
    {
    oitem.Disc__c =sc.Discount__c;


    }
      update Vcle;
        UPDATE Retest; 
        PageReference pg = new PageReference('/'+OwnrId);
         pg.setRedirect(true);


        return pg;


    }
  • 2
    I would suggest you to go through some trailhead module and follow best practices. There are multiple code related issues in Save method. Unnecessary DML statement, DML inside for loop etc. – Santanu Boral Dec 3 '18 at 11:00
1

You're basically requerying the data that was already in the database rather than the updated records from the page. Just update the existing values:

public PageReference Save(){   
  update offer;
  for(Vehicle__c oitem:Vcle) {
    oitem.Disc__c = offer.Discount__c;
  }
  update retest;
  // ...
  • I am updating records from the page. – SFDCLearneR Dec 3 '18 at 11:08
  • 2
    @SFDCLearneR Yes, but your queries are ignoring the updated values from the page. You need to use the values from the page, which are the records you have bound to the controller. – sfdcfox Dec 3 '18 at 11:09
  • still input field value not saving in database, as well as not populating in child records based on above your code. – SFDCLearneR Dec 3 '18 at 12:31
0

Just return null; from the save apex function.

Also as you have added rerender="none" you have to remove that or you can put id of the component/output panel to be re-rendered.

Also remove update Vcle; from for loop

  • Thanks for your quick reply, But given data is not saving in database and at the same time not populating in child records – SFDCLearneR Dec 3 '18 at 11:01
  • Also to remove update Vcle; from for loop – rahul gawale Dec 3 '18 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.