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
sfdc007sfdc007 

List has no rows assignment to s object error

Hi,

I am getting the following error in my vf page , its an inline vf page which captures the old status value and the new changed status value  from team form__c object

i have enabled the field history trakcing for that field where i am quering from team form history object




let me know what i am doing wrong here

MY VF PAGE :

<apex:page standardController="Team_Form__c"  extensions="CycleTimeSubstatusCalulation" tabStyle="Team_Form__c" sidebar="false" showHeader="false">

<apex:form >


<apex:pageblock title="EMEA SaSu Cycle-Time"  >

<apex:pageblockSection collapsible="true" showHeader="false"  columns="3"   >

<apex:outputText style="font-weight:700" value="Initial sub-status :{!gpsTeamForm.OldValue}"/>
<apex:outputText style="font-weight:700" value="New Sub-status :"/>
<apex:outputText style="font-weight:700" value="CycleTime :"/>


</apex:pageblockSection>
</apex:pageblock>

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

My controller :

public class CycleTimeSubstatusCalulation{

public  Team_Form__History  gpsTeamForm {get;set;}
//public  Team_Form__c  gpsTeamForms;

public CycleTimeSubstatusCalulation(ApexPages.StandardController controller) {
//gpsTeamForms= (Team_Form__c)controller.getRecord();
gpsTeamForm =[SELECT Id ,CreatedDate,Field,NewValue,OldValue,ParentId FROM Team_Form__History WHERE Id = :ApexPages.currentPage().getParameters().get('id') and   Field = 'SubStatus__c'];
    
    
    }

 






}


Kindly help me pls , i am struck

Thanks in Advance
Best Answer chosen by sfdc007
edoardo_spanoedoardo_spano
Sorry! I did a copy and past mistake in my class. This version should work.
public class CycleTimeSubstatusCalulation {

	public class MyHistory {
		public String oldValue {get;private set;}
		public String newValue {get;private set;}
		public String timeDiff {get;private set;}

		public MyHistory(String oldValue, String newValue, String timeDiff) {
			this.oldValue = oldValue;
			this.newValue = newValue;
			this.timeDiff = timeDiff;
		}
	}

	public List<MyHistory> gpsTeamFormList {get;private set;}

	public CycleTimeSubstatusCalulation(ApexPages.StandardController controller) {
		controller.addFields(new List<String>{'SubStatus__c','CreatedDate'});
		Team_Form__c tf = (Team_Form__c) controller.getRecord();
		List<Team_Form__c> tfList = [SELECT Id, CreatedDate, Field, NewValue, OldValue, ParentId
									   FROM Team_Form__History
									  WHERE ParentId = :tf.Id
										AND Field = 'SubStatus__c'
								   ORDER BY CreatedDate];
		gpsTeamFormList = new List<MyHistory>();
		if(tfList != null && tfList.size() > 0) {
			gpsTeamFormList.add(new MyHistory(
				''+tfList[0].oldValue,
				''+tfList[0].newValue,
				getFormattedDiff(tf.CreatedDate,tfList[0].CreatedDate)
			));
			if(tfList.size() > 1)
				for(Integer i=1;i<tfList.size();i++) {
					gpsTeamFormList.add(new MyHistory(
						''+tfList[i].oldValue,
						''+tfList[i].newValue,
						getFormattedDiff(tfList[i].CreatedDate,tfList[i-1].CreatedDate)
					));
				}
		}
	}

	private String getFormattedDiff(Datetime d1, Datetime d2) {
		if(d1 == null || d2 == null) return null;
        Integer days = Math.floor((d2.getTime() - d1.getTime())/86400000).intValue();
        Integer hours = Math.mod(Math.floor((d2.getTime() - d1.getTime())/3600000).intValue(),24);
        Integer minutes = Math.mod(Math.floor((d2.getTime() - d1.getTime())/60000).intValue(),60);
        return days+':'+hours+':'+minutes;
	}

}

Let me know if there are further problems.
Bye

All Answers

edoardo_spanoedoardo_spano
Hi,
You're quering for the Id field.
Instead, query for ParentId field.

Tip: use the getRecord() method.

Bye
Nitin PaliwalNitin Paliwal
Hi,
Use Below Code for your constructor:

public CycleTimeSubstatusCalulation(ApexPages.StandardController controller) {

list<Team_Form__History > tempList = [SELECT Id ,CreatedDate,Field,NewValue,OldValue,ParentId FROM Team_Form__History WHERE Id = :ApexPages.currentPage().getParameters().get('id') and   Field = 'SubStatus__c'];
    if(tempList .size()>0)
          gpsTeamForm = tempList[0];
    else
           gpsTeamForm = new Team_Form__History();
    }

 
Thanks
Nitin






 
sfdc007sfdc007
Hi used the above controller code

how do i refer it in VF PAGE , by the below way

<apex:outputText style="font-weight:700" value="New Sub-status : {!gpsTeamForm.NewValue}"/>

I am getting this error

[Error] Error: Unknown property 'Team_Form__cStandardController.gpsTeamForm'
Nitin PaliwalNitin Paliwal
Hi,
Paste this complete code,

public class CycleTimeSubstatusCalulation{

 public  Team_Form__History  gpsTeamForm {get;set;}
 public CycleTimeSubstatusCalulation(ApexPages.StandardController controller) {

 list<Team_Form__History > tempList = [SELECT Id ,CreatedDate,Field,NewValue,OldValue,ParentId FROM Team_Form__History WHERE Id = :ApexPages.currentPage().getParameters().get('id') and   Field = 'SubStatus__c'];
    if(tempList .size()>0)
          gpsTeamForm = tempList[0];
    else
           gpsTeamForm = new Team_Form__History();
    }
}


 
sfdc007sfdc007
Controller part is fine in vf page

<apex:outputText style="font-weight:700" value="Initial sub-status :{!tempList.OldValue}"/>

i am getting the following error

Error: Unknown property 'Team_Form__cStandardController.tempList
edoardo_spanoedoardo_spano
Paste this page and controller.

Page:
<apex:page standardController="Team_Form__c"  extensions="CycleTimeSubstatusCalulation" tabStyle="Team_Form__c" sidebar="false" showHeader="false">
	<apex:form >
		<apex:pageblock title="EMEA SaSu Cycle-Time"  >
			<apex:pageblockSection collapsible="true" showHeader="false"  columns="3"   >
				<apex:outputText style="font-weight:700" value="Initial sub-status :{!Team_Form__c.OldValue}"/>
				<apex:outputText style="font-weight:700" value="New Sub-status :{!Team_Form__c.NewValue}"/>
				<apex:outputText style="font-weight:700" value="CycleTime :"/>
			</apex:pageblockSection>
		</apex:pageblock>
	</apex:form>
</apex:page>

Class:
public class CycleTimeSubstatusCalulation{

	public  Team_Form__History  gpsTeamForm {get;set;}
	public CycleTimeSubstatusCalulation(ApexPages.StandardController controller) {
		list<Team_Form__History > tempList = [SELECT Id, CreatedDate, Field, NewValue, OldValue, ParentId
												FROM Team_Form__History
											   WHERE ParentId = :ApexPages.currentPage().getParameters().get('id')
												 AND Field = 'SubStatus__c'];
		if(tempList .size()>0)
			gpsTeamForm = tempList[0];
		else
			gpsTeamForm = new Team_Form__History();
	}
}

 
edoardo_spanoedoardo_spano
Sorry, the class has an error. This is the correct version:
public class CycleTimeSubstatusCalulation{

	public  Team_Form__History  gpsTeamForm {get;set;}
	public CycleTimeSubstatusCalulation(ApexPages.StandardController controller) {
		Team_Form__c tf = (Team_Form__c) controller.getRecord();
		list<Team_Form__History > tempList = [SELECT Id, CreatedDate, Field, NewValue, OldValue, ParentId
												FROM Team_Form__History
											   WHERE ParentId = :tf.Id
												 AND Field = 'SubStatus__c'];
		if(tempList .size()>0)
			gpsTeamForm = tempList[0];
		else
			gpsTeamForm = new Team_Form__History();
	}
}

 
Nitin PaliwalNitin Paliwal
Hi,
Please dont make any changes in the page, the starting vf page was correct .Just try and save the previous one only.

Here is the previous page


<apex:page standardController="Team_Form__c"  extensions="CycleTimeSubstatusCalulation" tabStyle="Team_Form__c" sidebar="false" showHeader="false">

<apex:form >


<apex:pageblock title="EMEA SaSu Cycle-Time"  >

<apex:pageblockSection collapsible="true" showHeader="false"  columns="3"   >

<apex:outputText style="font-weight:700" value="Initial sub-status :{!gpsTeamForm.OldValue}"/>
<apex:outputText style="font-weight:700" value="New Sub-status :"/>
<apex:outputText style="font-weight:700" value="CycleTime :"/>


</apex:pageblockSection>
</apex:pageblock>

</apex:form>
</apex:page>
 
sfdc007sfdc007
Now the values are fetching fine , but i have small doubt which i need help on it


1) when i change the value of sub status is is updating correctly with the old value and new value correspondinly , but when i change it for the second time it is not getting updated again



2) secondly right now we are making changes to one field which is not capturing the history , i want to capture the complete history of sub status change in my vf page



Help  me pls

VF PAGE :


<apex:page standardController="Team_Form__c"  extensions="CycleTimeSubstatusCalulation" tabStyle="Team_Form__c" sidebar="false" showHeader="false">
    <apex:form >
        <apex:pageblock title="EMEA SaSu Cycle-Time"  >
            <apex:pageblockSection collapsible="true" showHeader="false"  columns="3"   >
                <apex:outputText style="font-weight:700" value="Initial sub-status :{!gpsTeamForm .OldValue}"/>
                <apex:outputText style="font-weight:700" value="New Sub-status :{!gpsTeamForm .NewValue}"/>
                <apex:outputText style="font-weight:700" value="CycleTime :"/>
            </apex:pageblockSection>
        </apex:pageblock>
    </apex:form>
</apex:page>

CONTROLLER :


public class CycleTimeSubstatusCalulation{

    public  Team_Form__History  gpsTeamForm {get;set;}
    public CycleTimeSubstatusCalulation(ApexPages.StandardController controller) {
        Team_Form__c tf = (Team_Form__c) controller.getRecord();
        list<Team_Form__History > tempList = [SELECT Id, CreatedDate, Field, NewValue, OldValue, ParentId
                                                FROM Team_Form__History
                                               WHERE ParentId = :tf.Id
                                                 AND Field = 'SubStatus__c'];
        if(tempList .size()>0)
            gpsTeamForm = tempList[0];
        else
            gpsTeamForm = new Team_Form__History();
    }
}

Kindly help me on this regard

Thanks in Advance
edoardo_spanoedoardo_spano
Hi,
1. Every time you change the value of a tracked field, the system create a new history record. The page shows always the first record of the query result (gpsTeamForm = tempList[0];). Try to order the query, adding this code at the end of the query: "ORDER BY CreatedDate DESC".
2. If you want see all the history you must change the structure of your page and class, because you have to handle a list of record.

Here an example of what do you want.

PAGE:
<apex:page standardController="Team_Form__c" extensions="CycleTimeSubstatusCalulation" tabStyle="Team_Form__c" sidebar="false" showHeader="false">
    <apex:form >
        <apex:pageblock title="EMEA SaSu Cycle-Time">
            <apex:pageblockSection collapsible="true" showHeader="false" columns="1">
                <apex:pageBlockTable value="{!gpsTeamFormList}" var="gps">
					<apex:column value="{!gps.OldValue}" headerValue="Initial sub-status"/>
					<apex:column value="{!gps.NewValue}" headerValue="New Sub-status"/>
				</apex:pageBlockTable>
            </apex:pageblockSection>
        </apex:pageblock>
    </apex:form>
</apex:page>
CONTROLLER:
public class CycleTimeSubstatusCalulation{

    public List<Team_Form__History> gpsTeamFormList {get;private set;}

    public CycleTimeSubstatusCalulation(ApexPages.StandardController controller) {
        Team_Form__c tf = (Team_Form__c) controller.getRecord();
        gpsTeamFormList = [SELECT Id, CreatedDate, Field, NewValue, OldValue, ParentId
                             FROM Team_Form__History
                            WHERE ParentId = :tf.Id
                              AND Field = 'SubStatus__c'];
    }
}

Let me know eventually errors, because I had no time to try this code before.

I hope it helps you.
Bye
sfdc007sfdc007
i got that right , thats workin fine , now i need to calulcate the time difference

1) My requirement is i also want to calculate the time change between old sub status and new substatus and display the value

the calculated time should be DD:HH:MM format

it should skip for following

When Sub-Status=‘With Customer’
For weekends (Saturday and Sunday)
December 24th, 25th and 26th
January 1st.

Help me how to achieve thhis in my apex class

below is my code

VF PAGE :

<apex:page standardController="Team_Form__c"  extensions="CycleTimeSubstatusCalulation" tabStyle="Team_Form__c" sidebar="false" showHeader="false">
    <apex:form >
        <apex:pageblock title="EMEA SaSu Cycle-Time"  mode="inlineEdit" >
           <apex:pageBlockTable value="{!gpsTeamForm}" var="gps">
            <apex:column headervalue="Initial sub-status :" value="{!gps.OldValue}"/>
            <apex:column headervalue="New sub-status :" value="{!gps.NewValue}"/>
            <apex:column headervalue="Cycle Time :" />
            </apex:pageblockTable>
        </apex:pageblock>
    </apex:form>
</apex:page>

APEX CLASS :

public class CycleTimeSubstatusCalulation{
    
    public  List<Team_Form__History>  gpsTeamForm {get;set;}
    
    public CycleTimeSubstatusCalulation(ApexPages.StandardController controller) {
        
        gpsTeamForm = new List<Team_Form__History>();
        list<Team_Form__History > tempList = new list<Team_Form__History >();
        
        Team_Form__c tf = (Team_Form__c) controller.getRecord();
        
        tempList = [SELECT Id, CreatedDate, Field, NewValue, OldValue, ParentId
                    FROM Team_Form__History
                    
                    WHERE ParentId = :tf.Id
                    AND Field = 'SubStatus__c'];
        if(tempList .size()>0)
            for(integer i=0; i<tempList.size(); i++ )
        {
            gpsTeamForm.add(tempList[i]);
        }
        
        //gpsTeamForm = tempList[0];
        else
            gpsTeamForm = new List<Team_Form__History>();
    }
}


Kindly help me in this regard

thanks in advance
edoardo_spanoedoardo_spano
Hi,
sorry for the delay, but I'm very busy at the moment.

Try to read this example. It gives you the time diff with the requested format, but doesn't skip weedends and holidays (sorry, but it's not so simple and I would need more time).

CONTROLLER:
public class CycleTimeSubstatusCalulation {

	public class MyHistory {
		public String oldValue {get;private set;}
		public String newValue {get;private set;}
		public String timeDiff {get;private set;}

		public MyHistory(String oldValue, String newValue, String timeDiff) {
			this.oldValue = oldValue;
			this.newValue = newValue;
			this.timeDiff = timeDiff;
		}
	}

	public List<MyHistory> gpsTeamFormList {get;private set;}

	public CycleTimeSubstatusCalulation(ApexPages.StandardController controller) {
		ctrl.addFields(new List<String>{'SubStatus__c','CreatedDate'});
		Team_Form__c tf = (Team_Form__c) controller.getRecord();
		List<Team_Form__c> tfList = [SELECT Id, CreatedDate, Field, NewValue, OldValue, ParentId
									   FROM Team_Form__History
									  WHERE ParentId = :tf.Id
										AND Field = 'SubStatus__c'
								   ORDER BY CreatedDate];
		gpsTeamFormList = new List<MyHistory>();
		if(tfList != null && tfList.size() > 0) {
			gpsTeamFormList.add(new MyHistory(
				''+tfList[0].oldValue,
				''+tfList[0].newValue,
				getFormattedDiff(tf.CreatedDate,tfList[0].CreatedDate)
			));
			if(tfList.size() > 1)
				for(Integer i=1;i<tfList.size();i++) {
					gpsTeamFormList.add(new MyHistory(
						''+tfList[i].oldValue,
						''+tfList[i].newValue,
						getFormattedDiff(tfList[i].CreatedDate,tfList[i-1].CreatedDate)
					));
				}
		}
	}

	private String getFormattedDiff(Datetime d1, Datetime d2) {
		if(d1 == null || d2 == null) return null;
        Integer days = Math.floor((d2.getTime() - d1.getTime())/86400000).intValue();
        Integer hours = Math.mod(Math.floor((d2.getTime() - d1.getTime())/3600000).intValue(),24);
        Integer minutes = Math.mod(Math.floor((d2.getTime() - d1.getTime())/60000).intValue(),60);
        return days+':'+hours+':'+minutes;
	}

}

PAGE:
<apex:page standardController="Account" extensions="HystoryCtrl">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!hList}" var="h">
            <apex:column value="{!h.oldValue}" headerValue="old"/>
            <apex:column value="{!h.newValue}" headerValue="new"/>
            <apex:column value="{!h.timeDiff}" headerValue="diff"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>


To skip the holidays I suggest you to use the standard object Holiday.
You can configure holidays right here: Setup --> Company Profile --> Holidays

I hope I was helpful.

Bye
sfdc007sfdc007
Hi one small query

I am getting the following error


Error: Compile Error: Variable does not exist: ctrl at line 18 column 9



ctrl.addFields(new List<String>{'SubStatus__c','CreatedDate'});


should that be

      public String ctrl{get;private set;}

am i right
edoardo_spanoedoardo_spano
No it's the name of the standard controller variable, which is passed at the constuctor.
Replace ctrl with controller, or copy and paste all the code as I shared with you.

Bye
sfdc007sfdc007
sorry i didnt get you exactly

can you tell me what shoulkd i replace ctrl with ?
edoardo_spanoedoardo_spano
Sorry! I did a copy and past mistake in my class. This version should work.
public class CycleTimeSubstatusCalulation {

	public class MyHistory {
		public String oldValue {get;private set;}
		public String newValue {get;private set;}
		public String timeDiff {get;private set;}

		public MyHistory(String oldValue, String newValue, String timeDiff) {
			this.oldValue = oldValue;
			this.newValue = newValue;
			this.timeDiff = timeDiff;
		}
	}

	public List<MyHistory> gpsTeamFormList {get;private set;}

	public CycleTimeSubstatusCalulation(ApexPages.StandardController controller) {
		controller.addFields(new List<String>{'SubStatus__c','CreatedDate'});
		Team_Form__c tf = (Team_Form__c) controller.getRecord();
		List<Team_Form__c> tfList = [SELECT Id, CreatedDate, Field, NewValue, OldValue, ParentId
									   FROM Team_Form__History
									  WHERE ParentId = :tf.Id
										AND Field = 'SubStatus__c'
								   ORDER BY CreatedDate];
		gpsTeamFormList = new List<MyHistory>();
		if(tfList != null && tfList.size() > 0) {
			gpsTeamFormList.add(new MyHistory(
				''+tfList[0].oldValue,
				''+tfList[0].newValue,
				getFormattedDiff(tf.CreatedDate,tfList[0].CreatedDate)
			));
			if(tfList.size() > 1)
				for(Integer i=1;i<tfList.size();i++) {
					gpsTeamFormList.add(new MyHistory(
						''+tfList[i].oldValue,
						''+tfList[i].newValue,
						getFormattedDiff(tfList[i].CreatedDate,tfList[i-1].CreatedDate)
					));
				}
		}
	}

	private String getFormattedDiff(Datetime d1, Datetime d2) {
		if(d1 == null || d2 == null) return null;
        Integer days = Math.floor((d2.getTime() - d1.getTime())/86400000).intValue();
        Integer hours = Math.mod(Math.floor((d2.getTime() - d1.getTime())/3600000).intValue(),24);
        Integer minutes = Math.mod(Math.floor((d2.getTime() - d1.getTime())/60000).intValue(),60);
        return days+':'+hours+':'+minutes;
	}

}

Let me know if there are further problems.
Bye
This was selected as the best answer
sfdc007sfdc007
is working fine now but one small doubt is i am getting the time in - format also , why is that

Existing Referred 0:0:-13
Referred ILEC Request - No RPCM Involvement 0:-3:-37
sfdc007sfdc007
in my vf page should i refer

 <apex:column headervalue="Cycle Time "  value="{!gps.timeDiff}"/>

or

 <apex:column headervalue="Cycle Time "  value="{!gps.CreatedDate}"/>
edoardo_spanoedoardo_spano
Very sorry! Another copy and paste error. :(
Replace only the 37 line with this: getFormattedDiff(tfList[i-1].CreatedDate,tfList[i].CreatedDate)

You must refer the first: 
<apex:column headervalue="Cycle Time "  value="{!gps.timeDiff}"/>

I hope it's all ok now.
Bye
sfdc007sfdc007
great dude it worked thanks so much :)
simply buzzes 9simply buzzes 9
Hi Dude you have done an amazing work but would you like me please to recommend me like this (https://northell.design/blog/how-to-create-an-e-learning-app/) how can we upgrade our self according to our needs? Is there any possiblity then please let me know.