function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SuvraSuvra 

Inserting records into custom object database

Hi,

 

I have problem in inserting list of records in a custom table called, marker. Please find the code below :

 

Apex Controller Class :

public Abstract class SearchNearby { 

    private Account acct;
    public String strLat;
    public String strLong;
    public Double dbLat,dbLong;
    
    
    public List<suvra__marker__c> marker {get; set;}
    public List<suvra__marker__c> markerListTest {get; set;}

    
    public SearchNearby(ApexPages.StandardController controller) {
        this.acct = (Account)controller.getRecord();
        acct=[select Id,suvra__Latitude__c,suvra__Longitude__c from Account where Id=:acct.Id];
        strLat=acct.suvra__Latitude__c;
        strLong=acct.suvra__Longitude__c;
    }


    public SearchNearby () {
        marker = new List<suvra__marker__c>();
        //marker.add(new suvra__marker__c());
    }

   public PageReference getNearbyPlaces() {
   
       
        dbLat= Double.valueOf(strLat);
        dbLong= Double.valueOf(strLong);        

       
       Double maxLat=dbLat+1;
       Double minLat=dbLat-1;
       Double maxLong=dbLong+1;
       Double minLong=dbLong-1;
       Integer i;
       Double sampleLat,sampleLong;
       
       List<Account> finalList ;
       List<Account> myList ;
       
       myList=[select Id,name,billingstreet,billingcity,billingPostalcode,billingstate,suvra__Latitude__c,suvra__Longitude__c from Account];          
       try{
       for(Account acc:myList)
       {
           if(acc.suvra__Latitude__c != null && acc.suvra__Longitude__c != null){
               sampleLat = Double.valueOf(acc.suvra__Latitude__c);
               sampleLong = Double.valueOf(acc.suvra__Longitude__c);
           }
           else {
               continue;
           }    
           if(sampleLat != null){
               if((sampleLat <= maxLat && sampleLat >= minLat) && (sampleLong <= maxLong && sampleLong >= minLong))
               {
                    if(acc != null)
                    finalList.add(acc);
                                       
                }
       }
       }
       
             
       
           
       for(Account accfinal:finalList){
       
           suvra__marker__c markerSingle = new suvra__marker__c(); 
           
           markerSingle = [select suvra__Record_Identifier__c,suvra__Street__c,suvra__City__c,suvra__State__c,suvra__Postal_Code__c,suvra__Country__c,suvra__Latitude__c,suvra__Longitude__c from suvra__marker__c limit 1];
           
           if(accfinal != null){
           

               markerSingle.suvra__Record_Identifier__c = accfinal.Id;
               markerSingle.suvra__Street__c = accfinal.billingstreet;
               markerSingle.suvra__City__c = accfinal.billingcity;
               markerSingle.suvra__State__c = accfinal.billingstate;
               markerSingle.suvra__Postal_Code__c = accfinal.billingpostalcode;
               markerSingle.suvra__Country__c = accfinal.billingcountry;
               markerSingle.suvra__Latitude__c = Double.valueOf(accfinal.suvra__Latitude__c);
               markerSingle.suvra__Longitude__c = Double.valueOf(accfinal.suvra__Longitude__c);
               
               marker.add(markerSingle);
            }
        }    
       
            
       
        insert marker;
        markerListTest = new List<suvra__marker__c>();
       
       markerListTest = [select s.suvra__Street__c, s.suvra__State__c, s.suvra__Record_Identifier__c, s.suvra__Postal_Code__c, s.suvra__Longitude__c, s.suvra__Latitude__c, s.suvra__Country__c, s.suvra__City__c, s.SystemModstamp, s.OwnerId, s.Name, s.LastModifiedDate, s.LastModifiedById, s.IsDeleted, s.Id, s.CreatedDate, s.CreatedById From suvra__marker__c s];
       
       if(markerListTest.size() > 0)
           System.debug('Record Inserted Successfully');
       else
           System.debug('Sorry..Record not inserted');
       }catch(Exception e){
            System.debug('Exception occured'+e);
       }
                 
       PageReference redirPage = new PageReference('/'+ 'a0C/o');
       redirPage.setRedirect(true);
       return redirPage ;
       
        
                
        
   
  }
  
 }

 

VisualForce Page :

<apex:page standardController="Account" extensions="SearchNearby">

<apex:form >
<apex:pageBlock >
<apex:outputText >
Find the nearby Places for the Present Address :

{!account.billingstreet}
{!account.billingcity}
{!account.billingstate} {!account.billingpostalcode}
{!account.billingcountry}

</apex:outputText>

<apex:commandButton value="Search" action="{!getNearbyPlaces}"/>

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

 

This code is executing properly, but no record are getting updated in marker table. Please help

JimRaeJimRae
I would start by adding a pagemessages tag to your VF page. Then the error message would be displayed to you.  Also, you could watch the page execution with the system log window and see why it is failing.
SuvraSuvra

<apex:pagemessages> tag is not giving any error on page..also, that system log is also giving success report. Still insert operation is not working. I tried with Account standard object also..its not inserting record. Kindly tell me if there is any alternative of insert comamnd in SOQL.

 

Thanks

JimRaeJimRae

I am not sure about that.  How are you validating the records are or are not inserted?  If the system log indicates that the record was inserted, it must have been inserted. Have you looked at your custom object outside of your code to see if the records are there?  I am not sure if your validation code will work they way you have it.  You might want to check the actual object table to see if the records are there or not.