2

my vf page:

<apex:page controller="ActionFunctionDemoController">
<apex:sectionHeader title="Action Function Demo" subtitle="Save a New Record"/>

    <apex:form >

        <apex:outputPanel id="jspanel"> 
            <script>  
                function onControllerReturn() 
                {
                    alert('{!message}')
                }
            </script>
        </apex:outputPanel>

        <apex:actionFunction name="doControllerSave" action="{!save}" rerender="jspanel"/>

        <apex:pageBlock id="blockSection">

            <apex:pageBlockButtons >
                <apex:commandButton onclick="doControllerSave();" oncomplete="onControllerReturn()" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageMessages />

            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!cat.name}"/>  
                <apex:inputField value="{!cat.Cat2__c}"/>  
            </apex:pageBlockSection>

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

 // Submit the form to successfully create a record. To generate an error, remove the value for 'Cat2' and save.

</apex:page>

my controller:

public with sharing class ActionFunctionDemoController 
{

    public Cat3__c cat {get;set;}
    public String message {get;set;}

    public ActionFunctionDemoController() 
    {
        cat = new Cat3__c(name='Some Value',Cat2__c='a0B70000002PuK2EAK');
        message = 'Try again!!'; // initial message.
    }

  public PageReference save() 
  { 
    try 
    {
        insert cat;
        message = 'Insert successful! -- ' + cat.id;
    } 
    catch (Exception e) 
    {
        ApexPages.addMessages(e);
        message = 'Whoops! An error occurred -- ' + e.getMessage();      
    }
    return null;
  }

}
6
  • Are you saying that your controller message property ends up wit the value "invalid type cat3__c" that gets displayed in the alert? Or does something else happen?
    – Keith C
    Aug 1 '14 at 19:13
  • is the ID in Cat2__c='a0B70000002PuK2EAK' of the proper type (Cat2__c) or is it per chance of type Cat3__c?
    – cropredy
    Aug 1 '14 at 23:01
  • does your profile has create access to the custom object Cat3__c ? Aug 2 '14 at 1:57
  • While i'm saving the controller in vf page only i'm getting this error..@KeithC
    – RaJu
    Aug 2 '14 at 10:27
  • i jus wanna insert a new record by throwing the javascript message if it is successfully inserted and not...that doesn't matter with the ca2__c, delete the reference and give me the answer... if any solution would be appreciated..@crop1645
    – RaJu
    Aug 2 '14 at 10:31
1

In your controller, you have these two getters/setters:

public Cat3__c cat {get;set;}
public String message {get;set;}

You clearly need the 2nd one for your JS pop up error message. In actuality, it only needs to be a setter since you're only setting it. Your controller never looks at it's existing value so doesn't need to get it.

Now, in your visualforce code, you do the following:

            <apex:inputField value="{!cat.name}"/>  
            <apex:inputField value="{!cat.Cat2__c}"/> 

I don't see where you're doing a get on name or on Cat2__c; instead only on cat. It appears to me that you've hardcoded Cat2__c into your controller without doing a get/set on name.

If you want to continue to hardcode Cat2__c, I think you need at a minimum to add the following getter method:

public string name {get;}

In doing that, you'll need to change the following line of code to something like:

if(name.isEmpty() == false) cat = new Cat3__c(name='Some Value',Cat2__c='a0B70000002PuK2EAK');

Its up to you whether or not to add a getter for Cat2__c. I can see why its not necessary, but you do need to know whether there's a value for both of the two; something you don't appear to be doing in your logic as it stands right now unless you have a validation rule that applies to the fields on your VF page.

Since I don't know the full purpose of your controller or page, I won't comment further on your methods. I believe I've answered your question as asked.

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.