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
Jason BevenourJason Bevenour 

Priorvalue and New objects

Hi,

I created a custom object called "Account status". I also created a custom field on the account that will store the "Status" value of the account status when it is created and edited. This is working fine. I then created a "Previous Status" field on the account. I created a workflow rule and field update with the formula "TEXT(PRIORVALUE( Rating__c ))". This is working on the edit  properly. However, on the creation of a new "Account Status" object, the priorvalue funtion is returning the "Status" value of the new object. Is there a way to get the previous value on new object creation?
Best Answer chosen by Jason Bevenour
nitin sharmanitin sharma
You should use Update trigger as Update trigger will fetch you old as well as new values.The Old and new will help you.As when u update a record for the first time after insering it.The old values will go in the Old variable and the newly updated values will go in the new variable.I hope this give u some idea

All Answers

Vinit_KumarVinit_Kumar
I am bit confused when we create a new record where are the previous  values,it's all new values.

Can you eloborate ??
Jason BevenourJason Bevenour
The account status is a custom object. In the case of a new status object being created, I still want to get the value from the old object. I have attached an image of the objects related list in the accounts. When the object with the "5" in status is created, I want to still be able to get the value from the previous object with the "4" in the status. The problem on the new object, is the priorvalue is returning the value of the new object.

User-added image
Vinit_KumarVinit_Kumar
It won't coz Priorvalue returns value related to a field ,but you are talking about a different record(Or previous record to be very precise).If you want to get the values from Previous record,you shoud have an update Trigger on your object.

Hope this helps  !!
Jason BevenourJason Bevenour
So I've been working on a Trigger for this and as a test case I created a label to update a record with the old value. The problem I am running into is I need this to run before insert. The Trigger.oldMap functionality does not work with the before insert. Is there another way to go about this to grab the old record?

trigger test on Account_Rating__c (before insert) {
    
    for(Account_Rating__C a: Trigger.new){
        Account_Rating__c oldAccountRatingc = Trigger.oldMap.get(a.ID);
        a.test_label__c = oldAccountRatingc.Rating__c;
     }
  
}
nitin sharmanitin sharma
You should use Update trigger as Update trigger will fetch you old as well as new values.The Old and new will help you.As when u update a record for the first time after insering it.The old values will go in the Old variable and the newly updated values will go in the new variable.I hope this give u some idea
This was selected as the best answer
Craig KaiserCraig Kaiser
This concept applies to both workflow rules and triggers and has the same problem.  The PRIORVALUE of a field represents the value of the field on the current object before the current edit began.  The current field value represents the value that is going to be saved after all of the rules and triggers have fired.

The use case you are describing represents the current field value of another object that matches criteria that represents a "previous" characterization.  Previous needs to be carefully defined.

I am making the following assumptions:
  • Status is a numeric field.
  • Status is a required field.
  • The value of Status is always unique.
  • The intent is always to have the next new Account Status inherit a default Status value that is one greater than the largest Status value of any existing Account Status object, or one (1) if it is the first object being created and there are no existing Account Status objects.
There is no value on the new Account Status object that represents the value of a field on another Account Status object.

One way to write a business rule representing your use case for the Status field is as follows:
The Status field of a new Account Status should be assigned to be one (1) greater than the largest Status value among all existing Account Status objects, or one (1) if it is the first Account Status being created.

A trigger on the insert of a new Account Status can include a SOQL query to return the largest Status value from the collection of existing Account Status objects or zero (0) if no objects exist yet, and then one can be added to that number.

I don't yet have enough experience in Salesforce to write a sample trigger quickly, so I'll leave this as an exercise for the reader.  I also do not know whether this kind of SOQL reference can be implemented within a formula in a workflow.  If so, the criteria would include the ISNEW() function to restrict the execution to only objects that are being created for the first time.