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
ekarthikekarthik 

Insertion problem in object

 

 

Hi

   I am having insert problem , for the following code

 

public void init(){
Date dateValid = date.today();
List<Splash_Data__c> splashList = [select Name, Flag__c, Visit_Date__c from Splash_Data__c where Name = :Userinfo.getUserId()];
if(splashList.size() == 0){
Splash_Data__c splashNew = new Splash_Data__c(Name = Userinfo.getUserId(), Flag__c = 0, Visit_date__c = dateValid);
insert splashNew;
}

}

 

I tested this code using developer console , its woks fine .but when i execute in salesforce it doesn't insert

 

bob_buzzardbob_buzzard

How are you executing this in Salesforce, and what error do you see?

ekarthikekarthik

Hi

 I am in need of showing a splash screen .  Then i write test method , it creates some code coverage problem. In order to rectify i made some modification.

 

<script type="text/javascript">

       var j$ = jQuery.noConflict();

                              j$(document).ready(function(){

 

                                    splashScreen();

                          }

            

function splashScreen()
{

      if('{!splashRun}' == 'true'){

 

//   for calling pop up  reveal();


                j$("#splash-modal").reveal();

 

// for empting values in page load like to clear cache(not exactly cache . )


               j$("#splash-frame").contents().empty();

 

// resource file 


               j$("#splash-frame").attr('src','{!URLFOR($Page.taskSetup)}');
      }
}

 

</script>

 

 

 

In controller

---------------

// INITIALLY  I CHECK WHTHER ANY VALUES IN Flag__c i.e if Flag__c =0 -> show splashscreen ,  Flag__c =0  show me later,Flag__c = 2 , dont show.

 

For this i am checking whether any Flag__c =value else  i need  insert flag__c =0

 

//

 

public string splashRun{
get{
Date dateValid = date.today();
Date todayDate = date.newInstance(dateValid.year(),dateValid.month(),dateValid.day());
string UserId = Userinfo.getUserId();
User userLogin = [select LastLoginDate from User where Id = :UserId];
DateTime lastLogin = userLogin.LastLoginDate;
Date loginDate = date.newInstance(lastLogin.year(),lastLogin.month(),lastLogin.day());

List<Splash_Data__c> splashList = [select Name, Flag__c, Visit_Date__c from Splash_Data__c where Name = :Userinfo.getUserId()];//where Name = :Userinfo.getUserId()
if(splashList.size() == 0){
// Splash_Data__c splashNew = new Splash_Data__c(Name = Userinfo.getUserId());
Splash_Data__c splashNew = new Splash_Data__c(Name = Userinfo.getUserId(), Flag__c = 0, Visit_date__c = dateValid);
try{
insert splashNew;
}
catch(exception e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error inserting In splash data.'));
}
}

if(loginDate == todayDate){

splash = [select Name, Flag__c, Visit_Date__c from Splash_Data__c where Name = :Userinfo.getUserId()];
if(splash[0].Flag__c == 0){
splashRun = 'true';
}
else if(splash[0].Flag__c == 1){
splashRun = 'true';
}
else if(splash[0].Flag__c == 2){
splashRun = 'false';
}



}
return splashRun;
}
set;
}

 

 

 

// HERE I AM INITIALY CHECKING VALUES . IF NO  VALUES  I NEED TO INSERT VALUES . HERE I AM GETTING ERROR 

 

LIKE 

 

 

System.ListException: List index out of bounds: 0

 

in the line 

 

if(splash[0].Flag__c == 0) ,,

 

that is , when i check inserted values in developer console query editor . I dont get any values. No values in that.

 

I am struck here . I am in need of help

 

 

 

 

bob_buzzardbob_buzzard

I think the problem here is that you are trying to execute DML in a getter - this is not allowed.  You catch the exception from the DML, but then continue on the assumption that it will have succeded.

ekarthikekarthik

before working this way i did insertion in separete void method that too not working ..

 

in jquery

---------

init();

 

if('{!splashRun}' == 'true'){
j$("#splash-modal").reveal();
j$("#splash-frame").contents().empty();
j$("#splash-frame").attr('src','{!URLFOR($Page.taskSetup)}');
}

 

----

    <apex:actionFunction name="init" action="{!init}" rerender=""/>

 

In controller

---------

public void init(){
Date dateValid = date.today();
List<Splash_Data__c> splashList = [select Name, Flag__c, Visit_Date__c from Splash_Data__c where Name = :Userinfo.getUserId()];//where Name = :Userinfo.getUserId()
if(splashList.size() == 0){
Splash_Data__c splashNew = new Splash_Data__c(Name = Userinfo.getUserId(), Flag__c = 0, Visit_date__c = dateValid);
insert splashNew;
}
}

 

 

List<Splash_Data__c> splash{get;set;}
///Cookie SetupWizardHide = ApexPages.currentPage().getCookies().get('SetupWizardHide');

public string splashRun{
get{
Date dateValid = date.today();
Date todayDate = date.newInstance(dateValid.year(),dateValid.month(),dateValid.day());
string UserId = Userinfo.getUserId();
User userLogin = [select LastLoginDate from User where Id = :UserId];
DateTime lastLogin = userLogin.LastLoginDate;
Date loginDate = date.newInstance(lastLogin.year(),lastLogin.month(),lastLogin.day());

}

if(loginDate == todayDate){

splash = [select Name, Flag__c, Visit_Date__c from Splash_Data__c where Name = :Userinfo.getUserId()];
if(splash[0].Flag__c == 0){
splashRun = 'true';
}
else if(splash[0].Flag__c == 1){
splashRun = 'true';
}
else if(splash[0].Flag__c == 2){
splashRun = 'false';
}



}
return splashRun;
}
set;
}

bob_buzzardbob_buzzard

Init won't finish by the time that you hit the next line of javascript, as its asynchronous.

 

As part of your init actionfunction, you should re-render some javascript that executes the splash handling.

 

The following blog post shows how this can work and should be straightforward to adapt:

 

http://bobbuzzard.blogspot.co.uk/2011/05/refreshing-record-detail-from-embedded.html

ekarthikekarthik
Hi
What is the relationship in actionfunction and System.ListException: List index out of bounds: 0 --- error .
ekarthikekarthik
I have changed the action function rerender for that control
<apex:actionFunction name="init" action="{!init}" rerender="allTasks"/>

still i am getting the same error
bob_buzzardbob_buzzard

If you are seeing errors when you retrieve the splash, you need to sort those out first.  You should check the size of the retrieved list rather than assuming there will always be an entry.