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
Jerrod Knapp 11Jerrod Knapp 11 

trigger help on ContentVersion

I have a trigger that is "supposed" to recheck a box only if there is a new version of a Content Document and only if the box has been unchecked.  I need to be able to uncheck this box and save the record so we can publish.  What it's doing right now is re-checking the box whether or not the version is <> '1' (I have even used >'1').  Can someone check this out and tell me where I might be mucking up the code?

trigger NotOnWebCheckbox on ContentVersion (before update) {
    for(ContentVersion cv: Trigger.new){
        If (cv.VersionNumber <> '1' || cv.Not_on_Web__c == false ){
           cv.Not_on_Web__c = true;
        }
    }
}

Thanks in advance, obviously there will be beer for Best Answer ;)
Best Answer chosen by Jerrod Knapp 11
HARSHIL U PARIKHHARSHIL U PARIKH
Hello Jerrod,

try using the following code:
 
Trigger NotOnWebCheckbox on ContentVersion (before update) 
{
    for(ContentVersion cv: Trigger.new)
    {
        If(cv.VersionNumber != null)
        {
            String CurrentVersionNum = String.ValueOf(cv.VersionNumber);
            String PriorVersionNum   = String.ValueOf(Trigger.OldMap.get(cv.Id).VersionNumber);
            
            Boolean numChanged = (CurrentVersionNum != PriorVersionNum);
            
            If(cv.Not_on_Web__c == FALSE && numChanged)
            {
                 cv.Not_on_Web__c = TRUE;   
            }
        }
    }
}

If it solves the question then please mark it as best answer!

All Answers

Amit Singh 1Amit Singh 1
Try below code.
trigger NotOnWebCheckbox on ContentVersion (before update) {
    for(ContentVersion cv: Trigger.new){
        If (cv.VersionNumber <> Trigger.oldMap.get(cv.Id).VersionNumber || cv.Not_on_Web__c == false ){
           cv.Not_on_Web__c = true;
        }
    }
}

 
Jerrod Knapp 11Jerrod Knapp 11
Amit,  Unfortunately this does not work.  Thanks!
HARSHIL U PARIKHHARSHIL U PARIKH
Hello Jerrod,

try using the following code:
 
Trigger NotOnWebCheckbox on ContentVersion (before update) 
{
    for(ContentVersion cv: Trigger.new)
    {
        If(cv.VersionNumber != null)
        {
            String CurrentVersionNum = String.ValueOf(cv.VersionNumber);
            String PriorVersionNum   = String.ValueOf(Trigger.OldMap.get(cv.Id).VersionNumber);
            
            Boolean numChanged = (CurrentVersionNum != PriorVersionNum);
            
            If(cv.Not_on_Web__c == FALSE && numChanged)
            {
                 cv.Not_on_Web__c = TRUE;   
            }
        }
    }
}

If it solves the question then please mark it as best answer!
This was selected as the best answer
Jerrod Knapp 11Jerrod Knapp 11
Harshil,  tested and this works.  Thanks for your help on this!  Could you give a brief explanation of how this worked over the other two versions presented?  I will mark this as the best answer.

Sincerely,
Jerrod
HARSHIL U PARIKHHARSHIL U PARIKH
Hi Jerrod,
Few reasons I can think of,

1) We need to make sure the excate data type when we compare two versions and so I have make use of String.ValueOf() funciton.
2) I have also added condition which says If (cv.VersionNumber != null) so it will not check against the NULL values (e.g., let's say there is a new reocord entered in the database, at this posint there is no PriorVersionNum for it.

Hope this helps!