• Akshay S
  • NEWBIE
  • 0 Points
  • Member since 2019
  • Salesforce Developer

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 6
    Replies
Hi,  
I have a visualforce page for a custom object. It uses standard controller and an extension.  I want to take users to the object listview after saving the record or cancelling.  Here is my Visualforce and Apex coce.  After I click save nothing happens.   Thanks for your help!

Visualforce:
<apex:page standardController="Self_Matrix__c" extensions="e1">
  <apex:form >
    <apex:pageBlock title="">
    
          <apex:pageblockButtons >
              <apex:commandbutton action="{!save}" value="Save"/>
          </apex:pageblockButtons>
              <apex:pageblockButtons >
              <apex:commandbutton action="{!cancel}" value="cancel"/>
          </apex:pageblockButtons>
                
          <apex:pageblockSection >
              <apex:inputfield value="{!Self_Matrix__c.name}"/>
              <apex:inputfield value="{!Self_Matrix__c.Date__c}"/>
              <apex:inputfield value="{!Self_Matrix__c.student_ID__c}"/>
              <apex:inputfield value="{!Self_Matrix__c.Type__c}"/>        
          </apex:pageblockSection>
          <apex:pageBlockSection title="Housing" columns="1">
             <apex:pageblockTable value="{!Self_Matrix__c}" var="act" >
              <apex:column headerValue="1" headerClass="headingCenter" > 
                 <apex:outputLabel > Subject.</apex:outputLabel>
              </apex:column>
              <apex:column headerValue="3" headerClass="headingCenter">
                 <apex:outputLabel > Interest.</apex:outputLabel>
              </apex:column>
              <apex:column headerValue="Student Goal" headerClass="headingCenter"> 
                 <apex:outputLabel value=""> </apex:outputLabel>
                 <apex:inputCheckbox value="{!act.Student_Goal__c}" style="float:center"/>
              </apex:column>
         </apex:pageblockTable>
       
       </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>
 
Apex:
public class e1 {
    public e1(ApexPages.StandardController controller) {}
public static PageReference listView(Schema.sObjectType destinationSObject) {
   Schema.DescribeSObjectResult destination = Self_Matrix__c.SObjectType.getDescribe();
   PageReference pageRef = new PageReference('/' + destination.getKeyPrefix() + 'a/o' );
   pageRef.setRedirect(true);
   return listView(Self_Matrix__c.sObjectType);
    }
}
Hi anyone can help on how to do code coverage for this trigger ? 
This trigger to prevent user submit without image attachment field [rich text field]

Thank you
trigger PraTrigger on RequestApproval__c (before insert, before update) {
    for (RequestApproval__c p:Trigger.new){
    if(p.TSM_Email_c && (String.isBlank(p.Attachmentc) || (String.isNotBlank(p.Attachmentc) && !p.Attachment_c.contains('<img')))){
        p.addError('Picture is required to proceed');
    }
            
}
}

 
Hi All,
I am trying to update related record field values into parent record in a self lookup. I tried writing a triggrt for this but it is not working. This is my code.

 
trigger updComments on college__c (after insert, after update, after delete)
   Set<id> accIdList = new Set<id>();
    List<College__c> colllist = new List<College__c>();
   for(College__c con : Trigger.new)
   {
    accIdList.add(con.College__c);
   }
    for(College__c acc : [Select id, name, University__c,Comments__c,Combined_COmments__c, 
                             (Select Id, name,University__c,Comments__c,Combined_COmments__c From College__r) 
                        From College__c Where Id In : accIdList])
{
    List<String> lstSrting = new List<String>();
    for(College__c con : acc.University__c__r)
    {
        lstSrting.add(con.Comments__c);
    }
    College__c a = new College__c();
    a.id = acc.College__c;
    a.Combined_COmments__c = String.join(lstSrting, ',');
    colllist.add(a);
}
    update colllist;



In above code University__c is a self lookup, I am updaing Comments__c child values into Combined_COmments__c parent record.

Can anyone please suggest me where I am doing wrong
Thanks in Advance

Hi, novice here.

I have a Contact module on which I upload files(generally images) from the default File upoading function(PFA). I have used an after insert trigger to generate public url and made it visible. Then this URL is saved in a custom field of Contact module. It works fine. But when I write test class for this process, it doesn't work. Upon investigationl l found that the SObject it gives is 'User' for some reason, even though I linked it to a contact. I am sharing the original trigger code as well as test class for it. What am I doing wrong? Please guide.

Trigger 1-
trigger ContactFileLinkTrigger on ContentDocumentLink (before insert) {

    if(Trigger.isBefore){
        
        for(ContentDocumentLink l:Trigger.new) {
            l.Visibility='AllUsers';
        }
    }    
}
Trigger 2-
trigger ContactFileVersionTrigger on ContentVersion (after insert)
 {

    checkFileParent(trigger.New);
    
    public static void checkFileParent(List<ContentVersion> contentVersions){
       
        set<Id> contentDocumentIdSet = new set<Id>();
        map<Id, ContentVersion> contentDocumentIdVersionListMap = new map<Id, ContentVersion>();

        for(ContentVersion objContentVersion : contentVersions){
                    contentDocumentIdSet.add(objContentVersion.ContentDocumentId);
            contentDocumentIdVersionListMap.put(objContentVersion.ContentDocumentId, objContentVersion);
        }
		
        if(!contentDocumentIdSet.isEmpty())
        {
            List<ContentDocumentLink> contentDocumentList = [select LinkedEntityId, ContentDocumentId from ContentDocumentLink WHERE ContentDocumentId in :contentDocumentIdSet];

            system.debug('contentDocumentList: ' + contentDocumentList);
            
            if(contentDocumentList.size() > 0)
            {
                List<ContentVersion> validContentVersionList = new List<ContentVersion>();
                Map<Id, contact> versionIdContactIdMap = new Map<Id, contact>();
                for(ContentDocumentLink ContentDocumentSingle:contentDocumentList)
                {    
                    system.debug('ContentDocumentSingle: ' + ContentDocumentSingle.LinkedEntityId.getSObjectType().getDescribe().getName());
                    String documentSOject = ContentDocumentSingle.LinkedEntityId.getSObjectType().getDescribe().getName();
                    //if(documentSOject == 'Contact' || (Test.isRunningTest() && documentSOject == 'User') )
                    if(documentSOject == 'Contact')

                    {
                    	validContentVersionList.add(contentDocumentIdVersionListMap.get(ContentDocumentSingle.ContentDocumentId)); 
                        versionIdContactIdMap.put(contentDocumentIdVersionListMap.get(ContentDocumentSingle.ContentDocumentId).Id, [Select Contact_Image_URL__c, Contact_Image_Name__c FROM Contact WHERE id=:ContentDocumentSingle.LinkedEntityId LIMIT 1]);
                    }
                }
                
                if(validContentVersionList.size() > 0)
                {
                    createPublicLinkForFile(validContentVersionList, versionIdContactIdMap);
                }
            }
        }
    }
    
    public static void createPublicLinkForFile(List<ContentVersion> contentVersions, Map<Id, contact> versionIdContactIdMap)
    {
        ContentDistribution[] distributionsToInsert = new List<ContentDistribution>();
        set<Id> contentDistributionSet = new set<Id>();
        map<Id, ContentVersion> contentDistributionIdContentVersionMap = new map<Id, ContentVersion>();
       	for(ContentVersion objContentVersion : contentVersions){
            //if image uploaded then only create public link
            //if(objContentVersion.ContentDocumentId.ParentId.getSobjectType()==Contact.SobjectType){
                ContentDistribution getDist = createContentDistribution(objContentVersion.Id);
                //distributionsToInsert.add(createContentDistribution(objContentVersion.Id));
                distributionsToInsert.add(getDist);
            	system.debug('objContentVersion');
            	system.debug(objContentVersion);
            	contentDistributionIdContentVersionMap.put(objContentVersion.Id, objContentVersion);
                if(!contentDistributionSet.isEmpty())
                {
                    contentDistributionSet.clear(); 
                }
            	contentDistributionSet.add(objContentVersion.Id);
            //}
        }
        system.debug('distributionsToInsert: ' + distributionsToInsert);
        if(!distributionsToInsert.isEmpty())
        {
        	insert distributionsToInsert;
            List<ContentDistribution> contetDistributionList = [select ContentDownloadUrl, ContentVersionId from ContentDistribution WHERE ContentVersionId in :contentDistributionSet];
			
            system.debug('ContentDistribution: ' + contetDistributionList);
            system.debug('ContentDistribution url: ' + contetDistributionList[0].ContentDownloadUrl);
            
            List<contact> contactToUpdateList = new List<contact>();
            for(ContentDistribution contetDistributionsingle:contetDistributionList)
            {
                
                contact contactToUpdate = versionIdContactIdMap.get(contetDistributionsingle.ContentVersionId);
                
                contactToUpdate.Contact_Image_URL__c = contetDistributionsingle.ContentDownloadUrl;
                contactToUpdate.Contact_Image_Name__c = contentDistributionIdContentVersionMap.get(contetDistributionsingle.ContentVersionId).Title + '.' + contentDistributionIdContentVersionMap.get(contetDistributionsingle.ContentVersionId).FileType;
                
                contactToUpdateList.add(contactToUpdate);
            }
            
            if(contactToUpdateList.size() > 0)
            {
            	update contactToUpdateList;   
            }
        }
    }
    
    public static ContentDistribution createContentDistribution(Id contentVersionId){
        ContentDistribution newDist = new ContentDistribution();
        newDist.ContentVersionId = contentVersionId;
        newDist.Name = 'External Link';
        newDist.PreferencesNotifyOnVisit = false;
        newDist.PreferencesAllowViewInBrowser = true;
        newDist.PreferencesAllowOriginalDownload=true;
        system.debug('createContentDistribution created');
        system.debug(newDist);
        return newDist;
    }
}
I also tried to modify the code with SObject Type as User but then the test class just generates an error. (PFA)

Test Class (Excerpt code)-
@isTest
private class ContentDocumentLinkVisibilityHandlerTest {
    @isTest static void ContentDocumentLinkTriggerTest() {
        Test.startTest();
 
        Account accountObj = new Account();
        accountObj.Name = 'CRECAccount';
        INSERT accountObj;
        
        Contact contactObj = new Contact();
        contactObj.FirstName = 'CREC';
        contactObj.LastName = 'Project';
        contactObj.Email = 'abc@CERC.com';
        contactObj.AccountId = accountObj.Id;
        INSERT contactObj;
        
        ContentVersion content1 = new ContentVersion(); 
        content1.Title = 'Header_Picture1'; 
        content1.PathOnClient = '/' + content1.Title + '.jpg'; 
        Blob bodyBlob1 = Blob.valueOf('snc'); 
        content1.VersionData = bodyBlob1; 
        content1.origin = 'H';
        insert content1;
        ContentDocumentLink contentlink1 = new ContentDocumentLink();
        contentlink1.LinkedEntityId = contactObj.id; // linked with contact module
        contentlink1.contentdocumentid = [select contentdocumentid from contentversion where id =: content1.id].contentdocumentid;
        // contentlink1.ShareType = 'I';
        contentlink1.Visibility = 'AllUsers';
        insert contentlink1;
        Update contentlink1;
  
        Test.stopTest();
    }
}



upload image from this place
Error when change SObjectType to User in the trigger