1

I have a custom controller on a custom object book__c:

    public class fetchingController {

    public String newTitle;
    public list<Book__c>  bookList {get;set;}
    public Integer id  {get;set;}
    public Integer uid {get;set;}

    public String getNewTitle() {
        return newTitle;
    }

    public void setNewTitle(String newT) {
        newTitle = newT;
    }

    public fetchingController(){
           bookList = [Select Id , Name, author__c, title__c, price__c from Book__c];
    }


   // Method to update the title Book Record 

   public PageReference updatebook() {
        String bookUid = ApexPages.currentPage().getParameters().get('uid');

        System.debug('Bookid = ' + bookUid + ', newTitle = ' + newTitle);
        Book__c book =[select Id  from Book__c where id = : bookUid];
        PageReference pageRef = new PageReference('/apex/Controller2');
        pageRef.setRedirect(true);
        return pageRef;
   }
}

and the page is:

<apex:page sidebar="false" controller="fetchingController" id="pageId" >
<apex:form id="formId">
        <apex:pageBlock id="pageBlockId1" title="Books Available">
            <apex:pageBlockTable value="{!bookList}" var="bk" id="pageBlockTableId">
                <apex:column headerValue="id">
                    <apex:outputText value="{!bk.id}"/>
                </apex:column>
                <apex:column headerValue=" Name">
                    <b>  {!bk.Name } </b> 
                </apex:column>
                <apex:column headerValue="Author">
                    {!bk.Author__c}
                </apex:column>
                <apex:column headerValue="Title">
                    {!bk.Title__c} 
                </apex:column>
                <apex:column headerValue="Price">
                    {!bk.Price__c}
                </apex:column>

                <apex:column headerValue="revisedTitle">
                    <apex:inputText value="{!newtitle}" id="inputtitle" />
                    <apex:commandLink action="{!updatebook}" value="Salva" >
                        <apex:param value="{!bk.id}" name="uid" />
                    </apex:commandLink>
                </apex:column>
            </apex:pageBlockTable>

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

What I want to do is to update the title book using that inputText. The problem is that it always read a null string, but the program can see the right Id record. I'm quite new to Apex, so what am I missing?

UPDATE: If I have a look at the debug, I see that it prints a bookid that is valid, while newtitle is '' (empty). enter image description here

Btw, I updated the code with the dml operation, which is update book. So, it seems it doesn't get the string inserted.

4 Answers 4

2

As you need to update the title only, so instead of the DML insert statement use update statement. So update your method as below.

 public PageReference updatebook() {
    String bookUid = ApexPages.currentPage().getParameters().get('uid');
    System.debug('Bookid = ' + bookUid + ', newTitle = ' + newTitle);
    Book__c book =[select Id, Title__c  from Book__c where id = : bookUid];
    book.Title__c = newTitle;

    //update the book object
    update book;

    PageReference pageRef = new PageReference('/apex/Controller2');
    pageRef.setRedirect(true);
    return pageRef;
}
3
  • I updated the question because it doesn't get the value of the string.
    – Franktrt
    Apr 11, 2017 at 20:35
  • Ok, got it Try to replace "newTitle" with the "this.newTitle" in "getNewTitle", "setNewTitle" and "updatebook" methods. Apr 12, 2017 at 3:18
  • I tried, but I still get the empty string
    – Franktrt
    Apr 12, 2017 at 7:54
0

You have to set the title field in your apex method such as

ObjectReference.FieldApiName = Titlename;

and your DML operation to insert/update the record in the Db:

insert ObjectReference;

1
  • I updated the question because it doesn't get the value of the string.
    – Franktrt
    Apr 11, 2017 at 20:35
0

Just declare the String newTitle with getter/setter in controller:

public String newTitle {get;set;}
1
  • Hi @Subhasis, I tried before with this method, but I didn't get the string anyway.
    – Franktrt
    Apr 12, 2017 at 7:55
0

You are missing assigning newTitle to books Title__c and Update DML to update book. update updateBook Method as below.

    public PageReference updatebook() {
            String bookUid = ApexPages.currentPage().getParameters().get('uid');

            System.debug('Bookid = ' + bookUid + ', newTitle = ' + newTitle);
            Book__c book =[select Id, Title__c  from Book__c where id = : bookUid];
           book.Title__c = newTitle;
         update book;
        PageReference pageRef = new PageReference('/apex/Controller2');
        pageRef.setRedirect(true);
        return pageRef;
   }
1
  • I updated the question because it doesn't get the value of the string.
    – Franktrt
    Apr 11, 2017 at 20:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .