1

I'm creating a Visual Force page that enables user to add Case Comments. I am using javascript to create the comment and then I am using a controller extension to insert it. How can I call the method?

javascript:

        function createComment(){
        var comment = new sforce.SOBject('CaseComment');
        comment.CommentBody = document.getElementById('{!$Component.commentBody}').value;
        comment.IsPublished = true;
        comment.ParentId = '{!Case_Follow_up__c.Case__c}';
        // call CaseCommentExtension.addComment here
    }

Controller method

    public PageReference addComment(CaseComment comment) {
    insert comment;
    //comment = new CaseComment();
    return null;
} 
1
  • Is there a design reason you are creating the comment object in Javascript and not in the controller?
    – CyberJus
    Apr 8, 2015 at 23:33

2 Answers 2

2

Since you're using a javascript sobject variable, the shortest path to insert is to use the same sforce library to insert the record all through JS.

function createComment(){
    var comment = new sforce.SOBject('CaseComment');
    comment.CommentBody = document.getElementById('{!$Component.commentBody}').value;
    comment.IsPublished = true;
    comment.ParentId = '{!Case_Follow_up__c.Case__c}';

    var result = sforce.connection.create([comment]);
    // Check results.
    if (result[0].isSuccess()) {
        alert("Successfully created ID: " + result[0].getId());
        // now you can call an action to rerender the page, or use a self redirect
        commentInserted(result[0].getId());
    } else {
        alert('Could not create record '+result[0]);
    }
}

Then somewhere outside the javascript tags

<apex:actionFunction name="commentInserted" action="{!CommentInserted} rerender="theCommentPanel" >
    <apex:param name="theCommentId" assignTo="{!NewCommentId}" value=""/>
</apex:actionFunction>

Controller

public Id NewCommentId {get; set;}
public CaseComment NewComment {get; set;}

public PageReference CommentInserted() {

    list<CaseComment> comments = [SELECT Id, CommentBody, IsPublished, ParentId FROM CaseComment WHERE id =: NewCommentId];

    if(!comments.isEmpty())
        NewComment = comments[0];

    return null;
} 
// you could also just perform the soql in the getter of the NewComment variable

On the page, you can show the output

<apex:outputPanel id="theCommentPanel">
    comment id: {!NewComment.id}
</apex:outputPanel>
5
  • How would I pass the comment parameter to the controller method with this approach? Apr 8, 2015 at 19:04
  • Give the updated answer a try. apex:param works this way... you can use multiple apex:params within actionFunction and the call to the function passes the variables in order, so basically match the order of params. Apr 8, 2015 at 19:55
  • I keep getting "Attempt to dereference null object" error Apr 8, 2015 at 21:39
  • My bad! Forgot that apex:param only takes a few primitive data types like String, Id salesforce.com/docs/developer/pages/Content/… I changed the answer, this should work (as long as I didn't leave a bug in there by accident) Apr 8, 2015 at 23:17
  • Just fixed some syntax errors in the answer. Apr 8, 2015 at 23:22
0

You can achieve it by either of the following ways

1) Make the comments as a controller property of list type and add the comments in the list. Then for adding new comment you can give some add button in the page. But when add is clicked, you will have to call some controller method through action or an actionFunction. In the controller function, initialize a new comment and add to list. Then rerender the list of comments in page. Finally when user clicks save, you can upsert the comment list.

2) If you are looking for javascript approach (it can give more dynamic look and feel), it can be done in two ways 1) AJAX API. refer (http://www.salesforce.com/us/developer/docs/ajax/Content/sforce_api_ajax_more_samples.htm). 2) Javascript remoting. In both you can create case comment from javascript itself.

You must log in to answer this question.

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