You need to sign in to do that
Don't have an account?
Visweswara Rao Pamala
Returning Null value
Hi All,
I getting the field values (Is_Locked__c & Locked_By__c )from old content version(previous),but these field values are returning NUll.
Use case: For first time on uploading the "new version" of document from chatter feed items, updating the field values Is_Locked__c to "True" and Locked_By__c to username.
And second time when uploading new version for feed item, i am getting the field values (Is_Locked__c& Locked_By__c) of previous content version and comparing. But i am getting Null values for these field values (Is_Locked__c & Locked_By__c).
I am using the below code and please help me to get updated field values instead null value,instead referring to correct document (Previous content version)?
Thanks in Advance.
Code:
Trigger ContentVersionStop_trigger on ContentVersion (before insert, before update) {
// Getting the current username
String userName = UserInfo.getUserName();
User activeUser = [Select name From User where Username = : userName limit 1];
String currentusername = activeUser.name;
system.debug('Current user:' + currentusername );
system.debug('Trigger New:' + Trigger.new);
ContentVersion oldCV = [Select Id,CreatedDate, PathOnClient, ContentUrl, Is_Locked__c, Locked_By__c, Title from ContentVersion Order By CreatedDate Desc Limit 1];
for (ContentVersion cv : Trigger.new) {
if (oldCV.Is_Locked__c == NULL && oldCV.Locked_By__c==NULL)
{
system.debug('Into Null Loop:' +oldCV.Is_Locked__c);
cv.Is_Locked__c='True';
cv.Locked_By__c=currentusername;
System.debug('records1 are ' + cv);
}
else if(oldCV.Is_Locked__c == 'True' && oldCV.Locked_By__c==currentusername )
{
system.debug('Into locked Loop:' +oldCV.Is_Locked__c);
cv.addError('Version is locked');
}
}
}
I getting the field values (Is_Locked__c & Locked_By__c )from old content version(previous),but these field values are returning NUll.
Use case: For first time on uploading the "new version" of document from chatter feed items, updating the field values Is_Locked__c to "True" and Locked_By__c to username.
And second time when uploading new version for feed item, i am getting the field values (Is_Locked__c& Locked_By__c) of previous content version and comparing. But i am getting Null values for these field values (Is_Locked__c & Locked_By__c).
I am using the below code and please help me to get updated field values instead null value,instead referring to correct document (Previous content version)?
Thanks in Advance.
Code:
Trigger ContentVersionStop_trigger on ContentVersion (before insert, before update) {
// Getting the current username
String userName = UserInfo.getUserName();
User activeUser = [Select name From User where Username = : userName limit 1];
String currentusername = activeUser.name;
system.debug('Current user:' + currentusername );
system.debug('Trigger New:' + Trigger.new);
ContentVersion oldCV = [Select Id,CreatedDate, PathOnClient, ContentUrl, Is_Locked__c, Locked_By__c, Title from ContentVersion Order By CreatedDate Desc Limit 1];
for (ContentVersion cv : Trigger.new) {
if (oldCV.Is_Locked__c == NULL && oldCV.Locked_By__c==NULL)
{
system.debug('Into Null Loop:' +oldCV.Is_Locked__c);
cv.Is_Locked__c='True';
cv.Locked_By__c=currentusername;
System.debug('records1 are ' + cv);
}
else if(oldCV.Is_Locked__c == 'True' && oldCV.Locked_By__c==currentusername )
{
system.debug('Into locked Loop:' +oldCV.Is_Locked__c);
cv.addError('Version is locked');
}
}
}
Hope You doing Good. Here is some piece of code where You are lagging. Hope it helps.
Thanks,
Tarun Suri
(Salesforce Developer)
I have edited my code and now the issue is,in below code iam updating locked__c field value to false (cv.Locked__c= false;). And this debug line: System.debug('records1 are ' + cv);, also shows that locked__c field has been updated to value (false) successfully, but when I retreive the same content record (oldCV), the Locked__c field is coming as true instead false.
Not sure why iam getting field (Locked__c) value as true instead of false, any input to retreive the updated field value from content version?
Below is my full code.
Trigger ContentVersionStop_trigger on ContentVersion (before insert) {
// Getting the current username
String userName = UserInfo.getUserName();
User activeUser = [Select name From User where Username = : userName limit 1];
String currentusername = activeUser.name;
// Getting handle to latest contentversion ContentVersion oldCV = [Select Id,CreatedDate, PathOnClient, ContentUrl, Locked__c, Locked_By__c, Title from ContentVersion Where IsLatest = true Order By CreatedDate Desc Limit 1];
for (ContentVersion cv : Trigger.new) {
//Locked__c is checkbox and default value is checked (i.e., true)
if (oldCV.Locked__c == true )
{
cv.Locked__c= false;
cv.Locked_By__c=currentusername;
System.debug('records1 are ' + cv);
}
else if(oldCV.Locked__c == false)
{
system.debug('Into locked Loop:' +oldCV.Locked__c);
cv.addError('Version is locked');
}
}
}
Instead of oldCV play with cv like this. This code is working in my org. Please mark this as Best answer if it solves Your Problem :)
Thanks
Tarun Suri
(Salesforce Developer)
Thanks for the help.
I have pasted the above code in my org and tested it. But it is not working.
How I Tested from front end:
Step1: chatter (tab) --> File (Link) -->"Upload file from computer"--> clicked "Share" button and new feed item created with attachment.
Step 2:Feeditem attachment-->More Action--> Upload new version and new version uploaded to attachment
Step 3 : Repeated step 2 and file is uplaoded, but it should enter into else if loop and error message should be alerted and file shouldn't uploaded.
I have tested by added "System.debug" inside for loop and if loop. Below are the observation from Developer console log:
- it is not entering into for loop at all, it is skipped.(SQL statement in for loop returned zero rows), Hence it totally skipped for loop. Below is the screen shot.
Please let me know if i missed any or any other inputs.
Thanks
Best Regards
Visu