You need to sign in to do that
Don't have an account?
Insert appears to work but data isn't there
Hi,
I have a component which sits on a visualforce page, everytime this component is rendered I would like to do a check and insert some data. Initially I tried to put this code in the component controllers' constructor obviously this failed because no DML operations are allowed at this point.
Instead I now call the method from one of the components get properties. This appears to work and the debug logs show it as working, unfortunatly the data is not there. If I execute the same code anonamously from Eclipse it works perfectly.
Here is the log of the particular method in question:
17:6:34.930|METHOD_ENTRY|[16,6]|01pS00000005AJs|FieldAuditController.CreateSnapShot()
17:6:34.930|METHOD_ENTRY|[31,6]|01pS00000005AJs|FieldAuditController.MustCreateSnapshot()
17:6:34.930|METHOD_ENTRY|[46,192]|LIST<FieldAuditSnapshot__c>.size()
17:6:34.930|SOQL_EXECUTE_BEGIN|[46,15]|Aggregations:0|SELECT Id FROM FieldAuditSnapshot__c WHERE User__c=:Userinfo.getUserId() AND Month__c=:DateTime.Now().month() AND Year__c=:DateTime.Now().Year() AND ObjectType__c=:objectType
17:6:34.930|METHOD_ENTRY|[46,68]|Userinfo.getUserId()
17:6:34.930|METHOD_EXIT|[46,68]|Userinfo.getUserId()
17:6:34.930|METHOD_ENTRY|[46,118]|Datetime.month()
17:6:34.930|METHOD_ENTRY|[46,103]|DateTime.Now()
17:6:34.930|METHOD_EXIT|[46,103]|DateTime.Now()
17:6:34.930|METHOD_EXIT|[46,118]|Datetime.month()
17:6:34.930|METHOD_ENTRY|[46,154]|Datetime.Year()
17:6:34.930|METHOD_ENTRY|[46,139]|DateTime.Now()
17:6:34.930|METHOD_EXIT|[46,139]|DateTime.Now()
17:6:34.931|METHOD_EXIT|[46,154]|Datetime.Year()
17:6:34.937|SOQL_EXECUTE_END|[46,15]|Rows:0
17:6:34.937|METHOD_EXIT|[46,192]|LIST<FieldAuditSnapshot__c>.size()
17:6:34.937|METHOD_EXIT|[31,6]|FieldAuditController.MustCreateSnapshot()
17:6:34.938|METHOD_ENTRY|[36,18]|Userinfo.getUserId()
17:6:34.938|METHOD_EXIT|[36,18]|Userinfo.getUserId()
17:6:34.938|METHOD_ENTRY|[37,33]|Datetime.year()
17:6:34.938|METHOD_ENTRY|[37,18]|DateTime.Now()
17:6:34.938|METHOD_EXIT|[37,18]|DateTime.Now()
17:6:34.938|METHOD_EXIT|[37,33]|Datetime.year()
17:6:34.938|METHOD_ENTRY|[38,34]|Datetime.month()
17:6:34.938|METHOD_ENTRY|[38,19]|DateTime.Now()
17:6:34.938|METHOD_EXIT|[38,19]|DateTime.Now()
17:6:34.938|METHOD_EXIT|[38,34]|Datetime.month()
17:6:34.938|METHOD_ENTRY|[39,5]|Database.insert(SOBJECT:FieldAuditSnapshot__c)
17:6:34.938|DML_BEGIN|[39,5]|Op:Insert|Type:FieldAuditSnapshot__c|Rows:1
17:6:34.979|DML_END|[39,5]
17:6:34.979|METHOD_EXIT|[39,5]|Database.insert(SOBJECT:FieldAuditSnapshot__c)
17:6:34.979|METHOD_EXIT|[16,6]|FieldAuditController.CreateSnapShot()
Here is the controller:
public without sharing class FieldAuditController { public string objectType{get;set;} public string title{get;set;} public FieldAuditController(){ } //for this prototype we'll just use the candidate object with a small set of fields public integer AuditPercent{ get{ if(AuditPercent == null){ //these queries would be where we filter by date string q = 'SELECT AVG(Completedness__c) FROM '+objectType+' WHERE OwnerId=\''+Userinfo.getUserId()+'\''; AggregateResult result = Database.query(q); AuditPercent = double.valueOf(result.get('expr0')).intValue(); CreateSnapShot(); } return AuditPercent; } set; } // the incomplete percentage public integer AuditIncompletePercent{ get{ return 100 - AuditPercent; }set; } public void CreateSnapShot(){ //check if its neccessary to create snapshot if(MustCreateSnapshot()){ FieldAuditSnapshot__c ss = new FieldAuditSnapshot__c(); ss.Complete__c = AuditPercent; ss.ObjectType__c = objectType; ss.User__c = Userinfo.getUserId(); ss.Year__c = DateTime.Now().year(); ss.Month__c = DateTime.Now().month(); Database.insert(ss); } } private boolean MustCreateSnapshot(){ //Query the FieldAuditSnapshot__c record to see if a snapshot has been created for this user on candidate for this month and year integer c = [SELECT Id FROM FieldAuditSnapshot__c WHERE User__c=:Userinfo.getUserId() AND Month__c=:DateTime.Now().month() AND Year__c=:DateTime.Now().Year() AND ObjectType__c=:objectType].size(); if(c==0){ return true; } return false; } }
And here is the component:
<apex:component controller="FieldAuditController" allowDML="true">
<apex:attribute assignTo="{!objectType}" name="objectType" required="true" type="string" description="object type"/>
<apex:attribute assignTo="{!title}" name="title" required="true" type="string" description="title"/>
<div class="sidebox">
<div class="title-left"><div class="title-right"><div class="title-center">
<span>Field Audit</span>
<img class="widgetImage" src="{!URLFOR($Resource.goldicons,'lead_gold.png')}" />
</div></div></div>
<div class="content">
<!-- Attempt using google apis chart library, bind the data values inside the url -->
<img src="http://chart.apis.google.com/chart?chs=200x120&cht=p&chd=t:{!AuditPercent},{!AuditIncompletePercent}&chdl=Completed+data|Data+left+blank&chma=0,0,30&chtt={!title}" />
Your {!title} data is {!AuditPercent}% complete.
</div>
</div>
</apex:component>
Any ideas?
/bump