+ Start a Discussion
learning1.3953795511514497E12learning1.3953795511514497E12 

How to Assign values to standardObject fields in RemoteAction method?

Hi,

In visualforce page, I have a script where I am Invoking a RemoteAction Method.
In Remote action method I want to set the values for standard Account's fields.
I tried with the follwing code but no use...


Apex Controller :

==============================
public  class CreateAccountController
{
           public static Account acc;
           public CreateAccountController(ApexPages.StandardController controller)
           {
                     acc= (Account)controller.getRecord();
           } 

           @RemoteAction
            public static void popupData(String selectedRec)
             {      
                                    System.debug('*******************  Selected Rec :' +selectedRec);       
                                    acc.Name = selectedRec;   
              }

}


Here the RemoteAction method "popupData" is not setting the value in acc.Name field.
Pls help.

         
Vinit_KumarVinit_Kumar
I don't see any setter method in your code..You should create the setter method in your Apex class.Add the below lines in your code :-

public String selectedRec{ get; set; }

If this helps,please mark it as best answer to help others :)
learning1.3953795511514497E12learning1.3953795511514497E12
Hi,

Adding the above line didnt help me to solve my issue.

public  class CreateAccountController
{
           public static Account acc;
           public CreateAccountController(ApexPages.StandardController controller)
           {
                     acc= (Account)controller.getRecord();
           }

           @RemoteAction
            public static void popupData(String selectedRec)
             {     
                                    System.debug('*******************  Selected Rec :' +selectedRec);      
                                    acc.Name = 'Account1';  
              }

}

Even if I hardcode some data say " acc.Name = 'Account1'; " This is also not working
Vinit_KumarVinit_Kumar
Can you post oyur VF code ??
learning1.3953795511514497E12learning1.3953795511514497E12
Thanks for the reply Vinit.

Below is my code.

What I am doing is.
1 : Created a autoComplete (inputText) box. As I type in the inputTextBox it invokes the webservice and display the matching result as lookup.
2 : I need to select one record from the autoComple field and create it as account -- So I need to map the Account standard fields and selectedRecords Fields.


VF Page
=======================

<apex:page StandardController="Account" extensions="CreateAccountController" sidebar="false"  >

<apex:form id="companySearchForm">
    <apex:stylesheet value="{!URLFOR($Resource.AutoCompleteWithModal, '/JQueryUI/css/ui-lightness/jquery-ui-1.8.17.custom.css')}"/>
    <apex:includeScript value="{!URLFOR($Resource.AutoCompleteWithModal, '/JQueryUI/js/jquery-1.7.1.min.js')}"/>
    <apex:includeScript value="{!URLFOR($Resource.AutoCompleteWithModal, '/JQueryUI/js/jquery-ui-1.8.17.custom.min.js')}"/>
    <apex:stylesheet value="{!URLFOR($Resource.AutoCompleteWithModal, '/JQueryModal/css/basic.css')}"/>
   
    <style type="text/css">
        .ui-autocomplete-loading { background: white url('{!URLFOR($Resource.AutoCompleteWithModal, 'AjaxLoad.gif')}') right center no-repeat; }
    </style>
   
     <apex:inputText id="theTextInput"/>
   
    <script type="text/javascript">
    //$ac - AutoComplete
   
    $ac = jQuery.noConflict();
   
    function getLoadingImage()
    {
        var loadImagURL = "{!URLFOR($Resource.AutoCompleteWithModal, 'BigLoad.gif')}";
        var retStr = ['<img src="', loadImagURL ,'" title="loading..." alt="loading..." class="middleAlign" />'];
        return retStr.join("");
    }
   
    var sourcePage = 'https://c.na15.visual.force.com/apex/Account_JSON?core.apexpages.devmode.url=0';
   
     $ac(function() {
            var txtVal =  $ac('[id="{!$Component.theTextInput}"]');
            //This method returns the last character of String
            function extractLast(term) {
                return term.substr(term.length - 1);
            }
   
            $ac('[id="{!$Component.theTextInput}"]').autocomplete({
                source: function( request, response ) {
   
                    //Abort Ajax
                    var $this = $ac(this);
                    var $element = $ac(this.element);
                    var jqXHR = $element.data('jqXHR');
                    if(jqXHR)
                        jqXHR.abort();
   
                    $ac('[id="{!$Component.theTextInput}"]').addClass('ui-autocomplete-loading');
                    //prompt('',sourcePage+'&key='+txtVal.val());
                    $element.data('jqXHR',$ac.ajax({
                        url: sourcePage+'&companySearchKeyword='+txtVal.val(),
                        dataType: "json",
                        data: {},                      
                        success: function( data ) {                       
                            response( $ac.map( data , function( item ) {                                                    
                                return {
                                    label: '<a>'+
                                item.name+"<br />"+
                                '<span style="font-size:0.8em;font-style:italic">'
                                +item.industry+', '+item.id+
                                "</span></a>",
                                value: item.name                               
                                                                      
                                }
                            }));
                        },
                        complete: function() {
   
                            //This method is called either request completed or not
                            $this.removeData('jqXHR');
   
                            //remove the class responsible for loading image
                            $ac('[id="{!$Component.theTextInput}"]').removeClass('ui-autocomplete-loading');
                        }
                    })
                    );
   
                },
                search: function() {
                    /*
                    // If last character is space
                        var term = extractLast(this.value);
                        if(term == " ")
                        {
                            return true;
                        }
                    */
   
                    //If String contains at least 1 space
                    if (this.value.indexOf(" ") >= 0)
                    {
                        $ac('[id="{!$Component.theTextInput}"]').autocomplete('option', 'delay', 500);
                        return true;
                    }
                    return false;
            },
                focus: function() {
                    // prevent value inserted on focus
                    return false;
                },
                select: function(event, ui) {
                    var selectedObj = ui.item;                  
                 
                    // alert(selectedObj);
                   // getCompanyDetail(selectedObj.compId);                                                                 
                       Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.CreateAccountController.popupData}',selectedObj,function(result, event) {if (event.status) {  alert("******* "+result)} } );
                     
                    return true;
                }      
            }).data("autocomplete")._renderItem = autoCompleteRender;           
   
        });
   
    function autoCompleteRender(ul, item) {
        return $ac("<li></li>").data("item.autocomplete", item).append(item.label).appendTo(ul);
    }
   
    </script>
    </apex:form>
        <apex:pageMessages />

     <apex:form id="myForm">          
     
         <apex:sectionHeader title="Account Edit" subtitle="{!Account.Name}" />  

        <apex:pageBlock id="pgBlock" mode="edit" title="Account Edit">

            <apex:pageBlockButtons location="both">
                <apex:commandButton value="Save" action="{!SaveAndCreteLocations}"/>
                <apex:commandButton value="Cancel" action="{!Cancel}"/>
            </apex:pageBlockButtons>

        <apex:pageBlockSection id="pgBlockSectionAcctInfo" title="Account Information" collapsible="false" columns="2" >
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Account Owner</apex:outputLabel>
                <apex:outputField id="actOwner" value="{!account.ownerid}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Rating</apex:outputLabel>
                <apex:inputField id="actRating" value="{!account.rating}" />
            </apex:pageBlockSectionItem>
          
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Account Name</apex:outputLabel>
                <apex:inputField id="actName" value="{!account.name}" required="true" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Phone</apex:outputLabel>
                <apex:inputField id="actPhone" value="{!account.Phone}" />
            </apex:pageBlockSectionItem>      
                 
         </apex:pageBlockSection>
    </apex:pageBlock>
            
    </apex:form>      
</apex:page>


Controller RemoteAction
=====================
@RemoteAction
    public static void popupData(String selectedRec)
    {      
          Account acc; 
          acc= (Account)controller.getRecord();
        try{
            System.debug('*******************  Selected Rec :' +selectedRec);
            acc.Name = 'acc1';
            }
            catch(Exception e)
            {
                System.debug('******************** Exception : ' +e);
            }
       
    
    }

1 : Here I am getting null pointer Exception. --- Exception : System.NullPointerException: Attempt to de-reference a null object
2 : One more thing I want to know is, currently in the above script i am getting the data from differennt source.  Pls refer to below line.
" var sourcePage = 'https://c.na15.visual.force.com/apex/Account_JSON?core.apexpages.devmode.url=0'; " How do  i avoid this?
how do I  get the data from same VF and apex instead of refering to some other source.