+ Start a Discussion
Nikunj VNikunj V 

page redirect & insert object problem

hey,


i am trying to insert an object on button click and than need to redirect the page. with this code i am not able to redirect page. and also sometime it is inserting data, sometime not. i dont know why. please help me out:
public pagereference PunchIn() {
        Id pageid = controller.getId();
    
        Integer PunchCount = [select count() From Project_Time__c WHERE Active__c = True AND Project__c =:pageid Limit 1];
        Project_Time__c obj = new Project_Time__c();
    
        obj.Project__c = pageid;
        obj.Latitude__c = valuelat;
        obj.Longitude__c = valuelong;
        obj.Punch_In_Time__c=System.NOW();
        insert obj;
             Pagereference ref = new Pagereference('/apex/Pool_PH_WaterQuality');
             ref.setRedirect(true);
             return ref;
        }

 
Shrikant BagalShrikant Bagal
Can you post your VF code?
Shrikant BagalShrikant Bagal
Can you post your VF code?
Nikunj VNikunj V
here is the code
<apex:form >
                <apex:actionRegion >
                    <center><apex:commandButton value="Punch In" onclick="doTheGeoThang1();" rendered="{!renderbutton}" /></center>
                        <apex:actionFunction name="loc1" action="{!PunchIn}" rerender="jsvalues">
                        <apex:param name="lat" value="" assignTo="{!valueLat}"/>
                        <apex:param name="long" value="" assignTo="{!valueLong}"/>
                        </apex:actionFunction>
                </apex:actionRegion>
                <apex:actionRegion >
                        <center><apex:commandButton value="Punch out" onclick="doTheGeoThang2();" rendered="{!render_pout}" /></center>
                        <apex:actionFunction name="loc2" action="{!PunchOut}" rerender="jsvalues">
                        <apex:param name="lat" value="" assignTo="{!valueLat}"/>
                        <apex:param name="long" value="" assignTo="{!valueLong}"/>
                        </apex:actionFunction>
                </apex:actionRegion>
                <br/><b>Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><apex:outputField value="{!Project__c.Name}"/>
                <br/><br/><b>Account &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><apex:outputField value="{!Project__c.Account__c}"/>
                <br/><br/><b>Total Project Time &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><apex:outputField value="{!Project__c.Total_Project_Time__c}"/>
                <br/><br/><b>Client &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><apex:outputField value="{!Project__c.Client_Rate__c}"/> <br/> <br/>
              
    </apex:form>
            <center><apex:relatedList list="Project_time__r" title="Times" /> </center>
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
public pagereference PunchIn() 
{
        Id pageid = controller.getId();
    
        Integer PunchCount = [select count() From Project_Time__c WHERE Active__c = True AND Project__c =:pageid Limit 1];
        Project_Time__c obj = new Project_Time__c();
    
        obj.Project__c = pageid;
        obj.Latitude__c = valuelat;
        obj.Longitude__c = valuelong;
        obj.Punch_In_Time__c=System.NOW();
        insert obj;
		
		/*
             Pagereference ref = new Pagereference('/apex/Pool_PH_WaterQuality');
             ref.setRedirect(true);
             return ref;
		*/
		//return new 	Pagereference('/apex/Pool_PH_WaterQuality?id='+obj.id); // You can try this line also
		return new 	Pagereference('/'+obj.id);
}

Please mark this as solution if this will help you.

Thanks,
Amit Chaudhary
 
Nikunj VNikunj V
hey, thanks for the reply , but it isnt working. i just want to refresh the page , but it isn't getting anywhere.and sometime data insert is failing. i dont know why.
Nikunj VNikunj V
Only having page redirection problem. it isn't redirecting at all. i checked debug log. it is getting executed but nothing is happening.
Shrikant BagalShrikant Bagal
Hello Nikunj,

You are assign "jsvalues" as Id for any tag in your VF page, please assign "jsvalues" as Id for any of one Visualforce page Tag.

It will definately solve your problem.

if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.
​Thanks! 

 
Shrikant BagalShrikant Bagal
Please try:
 
<apex:form >
                <apex:actionRegion >
                    <center><apex:commandButton value="Punch In" onclick="doTheGeoThang1();" rendered="{!renderbutton}" /></center>
                        <apex:actionFunction name="loc1" action="{!PunchIn}" rerender="jsvalues">
                        <apex:param name="lat" value="" assignTo="{!valueLat}"/>
                        <apex:param name="long" value="" assignTo="{!valueLong}"/>
                        </apex:actionFunction>
                   <apex:outputPanel id="jsvalues"></apex:outputPanel>
                </apex:actionRegion>
                <apex:actionRegion >
                        <center><apex:commandButton value="Punch out" onclick="doTheGeoThang2();" rendered="{!render_pout}" /></center>
                        <apex:actionFunction name="loc2" action="{!PunchOut}" rerender="jsvaluespanel">
                        <apex:param name="lat" value="" assignTo="{!valueLat}"/>
                        <apex:param name="long" value="" assignTo="{!valueLong}"/>
                        </apex:actionFunction>
                    <apex:outputPanel id="jsvaluespanel"></apex:outputPanel>
                </apex:actionRegion>
                <br/><b>Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><apex:outputField value="{!Project__c.Name}"/>
                <br/><br/><b>Account &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><apex:outputField value="{!Project__c.Account__c}"/>
                <br/><br/><b>Total Project Time &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><apex:outputField value="{!Project__c.Total_Project_Time__c}"/>
                <br/><br/><b>Client &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><apex:outputField value="{!Project__c.Client_Rate__c}"/> <br/> <br/>
              
    </apex:form>
            <center><apex:relatedList list="Project_time__r" title="Times" /> </center>

if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.
​Thanks! 

 
Shrikant BagalShrikant Bagal
Please try:
 
<apex:form >
                <apex:actionRegion >
                    <center><apex:commandButton value="Punch In" onclick="doTheGeoThang1();" rendered="{!renderbutton}" /></center>
                        <apex:actionFunction name="loc1" action="{!PunchIn}" rerender="jsvalues">
                        <apex:param name="lat" value="" assignTo="{!valueLat}"/>
                        <apex:param name="long" value="" assignTo="{!valueLong}"/>
                        </apex:actionFunction>
                   <apex:outputPanel id="jsvalues"></apex:outputPanel>
                </apex:actionRegion>
                <apex:actionRegion >
                        <center><apex:commandButton value="Punch out" onclick="doTheGeoThang2();" rendered="{!render_pout}" /></center>
                        <apex:actionFunction name="loc2" action="{!PunchOut}" rerender="jsvaluespanel">
                        <apex:param name="lat" value="" assignTo="{!valueLat}"/>
                        <apex:param name="long" value="" assignTo="{!valueLong}"/>
                        </apex:actionFunction>
                    <apex:outputPanel id="jsvaluespanel"></apex:outputPanel>
                </apex:actionRegion>
                <br/><b>Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><apex:outputField value="{!Project__c.Name}"/>
                <br/><br/><b>Account &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><apex:outputField value="{!Project__c.Account__c}"/>
                <br/><br/><b>Total Project Time &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><apex:outputField value="{!Project__c.Total_Project_Time__c}"/>
                <br/><br/><b>Client &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><apex:outputField value="{!Project__c.Client_Rate__c}"/> <br/> <br/>
              
    </apex:form>
            <center><apex:relatedList list="Project_time__r" title="Times" /> </center>

if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.
​Thanks!