• shiv singh 31
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
trigger UpdateChildWithParent on Account (after insert,after update) {
       List<Account> accList = new List<Account>();
        Set<Id> UpdatedCaseIds = new Set<Id>(); 
        for(Account acc : Trigger.new) {
         UpdatedCaseIds.add(acc.RecordTypeId);
        }
       System.debug('>>UpdatedCaseIds>>'+UpdatedCaseIds);
        Map<Id, RecordType> recordTypeMap = new Map<Id, RecordType>([SELECT DeveloperName 
                                                                     FROM RecordType 
                                                                     WHERE Id IN :UpdatedCaseIds]);
         Map<ID, Account> mapAccounts = new Map<ID, Account>([SELECT Id, Status__c 
                                                              FROM Account where Id IN :UpdatedCaseIds]);
       
        for(Account acc1 : Trigger.new) {
                   RecordType optyRecordType = recordTypeMap.get(acc1.RecordTypeId);       
            if(optyRecordType != null) {            
                      if(optyRecordType.DeveloperName == 'Brand') {                
                 List<Account> AccountForUpdating1 = [SELECT Status__c  FROM Account 
                                                      WHERE Id IN :UpdatedCaseIds]; 
                List<Account> AccountForUpdating2 = [SELECT Status__c  FROM Account 
                                                      WHERE Id IN :UpdatedCaseIds];                 
                List<Opportunity> OppForUpdating1 =[SELECT Status__c FROM Opportunity
                                                      WHERE Id IN:UpdatedCaseIds];
                  List<Opportunity> OppForUpdating2 =[SELECT Status__c FROM Opportunity
                                                      WHERE Id IN:UpdatedCaseIds];               
                                for(Account childObj1:Trigger.new){ 
                                AccountForUpdating1.add(childObj1);
                               }
                           for(Account childObj2:Trigger.new){
                          AccountForUpdating2.add(childObj2);  
                             }                                                    
                       for(Opportunity opp1:OppForUpdating1){
                         Account acc = mapAccounts.get(opp1.AccountId);
                             acc.Status__c = opp1.Status__c ;                     
                      accList.add(acc);
                       }
                       System.debug('>>accList>>>'+accList);
                      Update AccountForUpdating1;
                      }            
              else if(optyRecordType.DeveloperName == 'Merch Owner') {
                    List<Account> AccountUpdating3 = [SELECT AccountManager__c  FROM Account 
                                                     WHERE Id IN :UpdatedCaseIds ];
                    List<Account> AccountUpdating4 = [SELECT AccountManager__c  FROM Account 
                                                     WHERE Id IN :UpdatedCaseIds ];
                    List<Opportunity> OppForUpdating =[SELECT AccountManager__c FROM Opportunity
                                                       WHERE Id IN:UpdatedCaseIds];
                    List<Opportunity> OppForUpdating4 =[SELECT AccountManager__c FROM Opportunity
                                                       WHERE Id IN:UpdatedCaseIds];
                       for(Account childObj3:Trigger.new){ 
                          AccountUpdating3.add(childObj3);
                          // System.debug('>>childObj3>>>'+AccountUpdating3);                            
                      for(Account childObj4:Trigger.new){
                     AccountUpdating4.add(childObj4);  
                            // System.debug('>>childObj4>>>'+AccountUpdating4);
                              } 
                       }
                    for (Opportunity opp : OppForUpdating) {
                        UpdatedCaseIds.add(opp.AccountId);  
                    if (opp.AccountManager__c != null) {
                            Account a = mapAccounts.get(opp.AccountManager__c);          
                                }
                              }                  
                    update AccountUpdating3;       
                 } 
            }
        }
     }
Hello,

I'm creating an Apex class that creates a Salesforce File record and associates it with a parent record. I'm having a problem creating the association (ContentDocumentLink) record.

Here is what I'm doing:
  • Create a ContentVersion record. This creates a ContentDocument record automatically if you leave the ContentVersion.ContentDocumentId field blank. (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocument.htm - "To create a document, create a new version via the ContentVersion object without setting the ContentDocumentId. This automatically creates a parent document record.")
  • Insert ContentVersion record
  • DEBUG: Confirm ContentVersion.ContentDocumentId is set (Here is where the problem is because it is NULL; however if I SOQL query ContentVersion after the Apex completes, ContentVersion.ContentDocumentId is set and the corresponding ContentDocument record exists)
  • Create ContentDocumentLink record (commented out because it fails because ContentDocumentLink.ContentDocumentId is a required field)
  • Insert ContentDocumentLink record (commented out because it fails because ContentDocumentLink.ContentDocumentId is a required field)
Here is the code:
public class FileController {
    
    @AuraEnabled
    public static Id saveTheFile(Id parentId, String fileName, String base64Data, String contentType, Id contentDocumentId) { 
        base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
        
        ContentVersion cv = new ContentVersion();
        cv.ContentLocation = 'S';
        cv.ContentDocumentId = contentDocumentId;
        cv.VersionData = EncodingUtil.base64Decode(base64Data);
        cv.Title = fileName;
        cv.PathOnClient = filename;
        
        insert cv;
        
        //***This DEBUG statement must return an Id for the rest of the code to work***
        System.debug('contentDocumentId='+cv.ContentDocumentId);               
        
        //ContentDocumentLink cdl = new ContentDocumentLink();
        //cdl.ContentDocumentId = cv.ContentDocumentId;
        //cdl.LinkedEntityId = parentId;
        //cdl.ShareType = 'I';
        
        //insert cdl;
        
        return cv.Id;
    }

    @AuraEnabled
    public static Id saveTheFile(Id parentId, String fileName, String base64Data, String contentType) {         
        return saveTheFile(parentId, fileName, base64Data, contentType, NULL);
    }
}

Does anyone know what I'm doing wrong, not considering, etc?
Appreciate any thoughts and input!