Skip to main content

public class URLupdate {

    public static void urlmapping(list <ContentDistribution> cdListToInsert){

        

        system.debug('cdListToInsert'+cdListToInsert);

        id Contentversion;

        id ContentdocumentID;

        id LinkedEntityId;

        string URL;

        string docname;

        if(!cdListToInsert.IsEmpty()){

            for(ContentDistribution cv:cdListToInsert){

                Contentversion=cv.ContentVersionId;

            }

        }

        system.debug('Contentversion ID==>'+Contentversion);

        

        // we pass content version id from where in this query we get the content id 

        list<ContentDistribution> cdlist2 = new list<ContentDistribution>([SELECT Id,name, ContentVersionId,ContentDocumentId FROM ContentDistribution WHERE ContentVersionId =:Contentversion]);

        system.debug('cdlist2==>'+cdlist2);

        if(!cdlist2.IsEmpty()){

            for(ContentDistribution cv:cdlist2){

                ContentdocumentID=cv.ContentDocumentId;

                docname=cv.name;

            }

        }

        system.debug('ContentdocumentID==>'+ContentdocumentID);

        

        //Pasting of URL based on content document id

        list<ContentDistribution> cdlist3 = new list<ContentDistribution>([Select Id, ContentDocumentId, DistributionPublicUrl from ContentDistribution where ContentDocumentId=:ContentdocumentID]);

        system.debug('cdlist3==>'+cdlist3);

        if(!cdlist2.IsEmpty()){

            for(ContentDistribution cv:cdlist3){

                URL=cv.DistributionPublicUrl;

            }

        }

        system.debug('URL===>'+URL);

        

        // now we get the LinkedEntityId from ContentDocumentId

        list<ContentDocumentLink> cdlinklist= new list<ContentDocumentLink>([SELECT Id, LinkedEntityId,ContentDocumentId FROM ContentDocumentLink WHERE ContentDocumentId =:ContentdocumentID]);

        system.Debug(cdlinklist);

        if(!cdlinklist.isEmpty()){

            for(ContentDocumentLink cd:cdlinklist){

                LinkedEntityId=cd.LinkedEntityId;

            }

        }

        

        system.debug('LinkedEntityId==>'+LinkedEntityId);

        

        if(docname.toUpperCase()=='INVOICE'){

            list<lead__c> leadlist = new list<lead__c>([select id,name,Invoice_Document_Link__c ,ContentLink__c from lead__c where id=:LinkedEntityId]);

            System.debug('leadlist==>'+leadlist);

            if(leadlist.size()>0){

                for(lead__c ld:leadlist){

                    

                    ld.Invoice_Document_Link__c=URL; 

                    ld.Doc_Name__c =docname;

                }

                update leadlist;  

            }

            system.debug('leadlist==>'+leadlist);

            

        }

        

        //Now we get the leads related with that list of LinkedEntityId's

        else if(docname.toUpperCase()!='INVOICE') {

            list<lead__c> leadlist = new list<lead__c>([select id,name,ContentLink__c from lead__c where id=:LinkedEntityId]);

            System.debug('leadlist==>'+leadlist);

            if(leadlist.size()>0){

                for(lead__c ld:leadlist){

                    

                    ld.ContentLink__c=URL; 

                  

                }

                update leadlist;  

            }

            system.debug('leadlist==>'+leadlist);

            

        }

        }

        

    } 

1 answer
  1. Dec 27, 2022, 7:50 PM

    @Shivam Gupta - Please use the below testclass and make some necessary changes accordingly

     

    @isTest

    public class URLupdateTest {

      @isTest

      public static void testURLupdate() {

        // Create test records

        ContentDistribution cd = new ContentDistribution(

          ContentVersionId = '01234567890abcdef',

          ContentDocumentId = '01234567890abcdef',

          DistributionPublicUrl = 'https://test.com'

        );

        ContentDocumentLink cdl = new ContentDocumentLink(

          LinkedEntityId = '01234567890abcdef',

          ContentDocumentId = '01234567890abcdef'

        );

        lead__c lead = new lead__c(

          Id = '01234567890abcdef',

          Name = 'Test Lead'

        );

     

        // Insert test records

        insert cd;

        insert cdl;

        insert lead;

     

        // Call method under test

        List<ContentDistribution> cdList = new List<ContentDistribution>{cd};

        URLupdate.urlmapping(cdList);

     

        // Query for updated records and verify field values

        lead__c updatedLead = [SELECT Id, Name, Invoice_Document_Link__c, ContentLink__c FROM lead__c WHERE Id = :lead.Id];

        System.assertEquals('https://test.com', updatedLead.Invoice_Document_Link__c);

      }

    }

0/9000