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
Abhi MehtaAbhi Mehta 

Can anyone help me bulk this class?

public class LegalAgreement_AfterSigning_Helper {
    public static void ToCreateLegalAgreement_Helper(list<echosign_dev1__SIGN_Agreement__c> ecosignList,Map<id,echosign_dev1__SIGN_Agreement__c> oldecosignList ){
        list<id> AgreementId =new list<id>();
        Set<string> AgreementType = new Set<string>();
        list<id> OppId = new list<id>();
        boolean flag=false;

        For (echosign_dev1__SIGN_Agreement__c Agreements: ecosignList){ 
            echosign_dev1__SIGN_Agreement__c AgreementInOld = oldecosignList.get(Agreements.Id);                
            if(Agreements.echosign_dev1__Opportunity__c!=null){
                if (Agreements.Send_to_Legal_Agreement__c == true && AgreementInOld.Send_to_Legal_Agreement__c!=true){                                       
                    AgreementId.add(Agreements.id);
                    OppId.add(Agreements.echosign_dev1__Opportunity__c);    
                    flag=true;
                } 
            }   
        }
        if(flag){
            List<Legal_Agreement_Type__mdt> LegaltypeList= [select id,MasterLabel from Legal_Agreement_Type__mdt];
            echosign_dev1__SIGN_Agreement__c AgreeementRecord = [select id,Name,echosign_dev1__Opportunity__c,echosign_dev1__Account__c,echosign_dev1__Account__r.name,echosign_dev1__Opportunity__r.AccountId,echosign_dev1__Opportunity__r.Account.Name from  echosign_dev1__SIGN_Agreement__c where id in : AgreementId limit 1];
            List<ContentVersion> ContentVersionList =  new list<ContentVersion>();
            List<Attachment> AttachmentList = [SELECT Id,Name,Body,Parentid,createddate FROM Attachment WHERE ParentId in : AgreementId order by createddate Asc];
            Attachment SignedAttachment = new Attachment();
            for(Attachment att: AttachmentList){
                if(att.name.contains('- signed.pdf')){
                    SignedAttachment = att;
                }
                else if(att.name.contains('approved.pdf')){
                    SignedAttachment = att;
                }
                for(Legal_Agreement_Type__mdt legType : LegaltypeList){
                    if(att.name.contains(legType.MasterLabel)){
                        AgreementType.add(legType.MasterLabel);
                    }
                }
            }
            
            for(String agtype:AgreementType){
                ContentVersion contentVersion_1 = new ContentVersion(
                    Title =SignedAttachment.Name,
                    PathOnClient =SignedAttachment.Name,
                    VersionData = SignedAttachment.Body,
                    IsMajorVersion = true,
                    IsCongaFile__c= true
                );
                ContentVersionList.add(contentVersion_1);
            }
            if(ContentVersionList.size()>0){
                insert ContentVersionList;
            }

            list<Legal_Agreements__c> LegalAgreementList = new list<Legal_Agreements__c>();     
            list<Legal_Agreements__c> LegalAgreList = [select id,name,Contract_Description__c,CreatedDate from Legal_Agreements__c where Opportunity__c in:OppId order by CreatedDate Asc];
            Map<String, id> LegalagremType = new Map<String, id>();             
            for(Legal_Agreements__c LegalAgRecord: LegalAgreList){
                for(String agrtype: AgreementType){
                    if(LegalAgRecord.Contract_Description__c.contains(agrtype)){
                        LegalagremType.put(LegalAgRecord.Contract_Description__c, LegalAgRecord.id);
                    }
                }
            }           
            for(String agtype:AgreementType){
                if(LegalagremType.containsKey(agtype)){
                    Legal_Agreements__c LegalAgreement = new Legal_Agreements__c();
                    LegalAgreement.id= LegalagremType.get(agtype);
                    LegalAgreement.Contract_Description__c = agtype ;
                    LegalAgreementList.add(LegalAgreement);
                }
                else{
                    Legal_Agreements__c LegalAgreement = new Legal_Agreements__c();
                    LegalAgreement.Opportunity__c = AgreeementRecord.echosign_dev1__Opportunity__c;
                    LegalAgreement.Account__c = AgreeementRecord.echosign_dev1__Opportunity__r.AccountId;           
                    LegalAgreement.Name =AgreeementRecord.echosign_dev1__Opportunity__r.Account.Name+ '-'+ agtype +' '+System.Today().year();
                    LegalAgreement.Contract_Description__c = agtype ;
                    LegalAgreementList.add(LegalAgreement);
                }
            }            
            if(!LegalAgreementList.isEmpty()){
                upsert LegalAgreementList;
            }             
            List<id> ContentVersionId = new list<id>();
            for(ContentVersion contver : ContentVersionList){
                ContentVersionId.add(contver.id);
            }           
            list<ContentVersion> Contentrec = [SELECT ContentDocumentId,id,Title FROM ContentVersion where id=:ContentVersionId];           
            List<ContentDocumentLink> ContentDocumentLinkList = new List<ContentDocumentLink>();
            for(integer i=0; i<Contentrec.size(); i++)
            {
                ContentDocumentLink con = new ContentDocumentLink();
                con.ContentDocumentId= Contentrec[i].ContentDocumentId;
                con.LinkedEntityId = LegalAgreementList[i].id;
                con.ShareType='I';
                ContentDocumentLinkList.add(con);
            }
            if(ContentDocumentLinkList.size()>0){
                insert ContentDocumentLinkList;
            }   
        }
    }
}
Ajay K DubediAjay K Dubedi
Hi Abhi,
Try this code, try to put the null check condition in your code at every place where it is required:
public class LegalAgreement_AfterSigning_Helper {
    public static void ToCreateLegalAgreement_Helper(list<echosign_dev1__SIGN_Agreement__c> ecosignList,Map<id,echosign_dev1__SIGN_Agreement__c> oldecosignList ) {
        Set<id> AgreementId = new Set<id>();
        Set<string> AgreementType = new Set<string>();
        Set<id> OppId = new Set<id>();
        boolean flag = false;
        echosign_dev1__SIGN_Agreement__c AgreeementRecord = new echosign_dev1__SIGN_Agreement__c();
        List<Attachment> AttachmentList = new List<Attachment>();
        List<ContentVersion> ContentVersionList =  new list<ContentVersion>();
        list<Legal_Agreements__c> LegalAgreementList = new list<Legal_Agreements__c>();
        list<Legal_Agreements__c> LegalAgreList = new list<Legal_Agreements__c>();
        Map<String, id> LegalagremType = new Map<String, id>(); 
        Set<id> ContentVersionId = new Set<id>();
        list<ContentVersion> Contentrec =  new list<ContentVersion>();
        List<ContentDocumentLink> ContentDocumentLinkList = new List<ContentDocumentLink>();

        For (echosign_dev1__SIGN_Agreement__c Agreements : ecosignList) { 
            echosign_dev1__SIGN_Agreement__c AgreementInOld = oldecosignList.get(Agreements.Id);                
            if(Agreements.echosign_dev1__Opportunity__c != null) {
                if (Agreements.Send_to_Legal_Agreement__c == true && AgreementInOld.Send_to_Legal_Agreement__c != true)         {                                       
                    AgreementId.add(Agreements.id);
                    OppId.add(Agreements.echosign_dev1__Opportunity__c);    
                    flag = true;
                } 
            }   
        }
        if(flag){
            List<Legal_Agreement_Type__mdt> LegaltypeList = [select id, MasterLabel from Legal_Agreement_Type__mdt];
            
            if(AgreementId.size() > 0) {
                AgreeementRecord = [select id,Name,echosign_dev1__Opportunity__c,echosign_dev1__Account__c,echosign_dev1__Account__r.name,echosign_dev1__Opportunity__r.AccountId,echosign_dev1__Opportunity__r.Account.Name from  echosign_dev1__SIGN_Agreement__c where id in : AgreementId limit 1];
            
                AttachmentList = [SELECT Id, Name, Body, Parentid, createddate FROM Attachment WHERE ParentId in : AgreementId order by createddate Asc];
            }
            
            if(AttachmentList.size() > 0) {
                Attachment SignedAttachment = new Attachment();
                for(Attachment att: AttachmentList) {
                    if(att.name.contains('- signed.pdf')){
                        SignedAttachment = att;
                    }
                    else if(att.name.contains('approved.pdf')){
                        SignedAttachment = att;
                    }
                    if(LegaltypeList.size() > 0) {
                        for(Legal_Agreement_Type__mdt legType : LegaltypeList){
                            if(legType.MasterLabel != null) {
                                if(att.name.contains(legType.MasterLabel)){
                                    AgreementType.add(legType.MasterLabel);
                                }
                            }
                        }
                    }
                }
            
                if(AgreementType.size() > 0) {
                    for(String agtype : AgreementType) {
                        ContentVersion contentVersion_1 = new ContentVersion(
                        Title = SignedAttachment.Name,
                        PathOnClient = SignedAttachment.Name,
                        VersionData = SignedAttachment.Body,
                        IsMajorVersion = true,
                        IsCongaFile__c = true
                        );
                        ContentVersionList.add(contentVersion_1);
                    }
                }    
            }
            
            if(ContentVersionList.size() > 0) {
                insert ContentVersionList;
            }
            
            if(OppId.size() > 0) {
                LegalAgreList = [select id,name,Contract_Description__c,CreatedDate from Legal_Agreements__c where Opportunity__c IN : OppId order by CreatedDate Asc];
            }
            
            if(LegalAgreList.size() > 0 && AgreementType.size() > 0) {
                for(Legal_Agreements__c LegalAgRecord : LegalAgreList) {
                    for(String agrtype : AgreementType) {
                        if(LegalAgRecord.Contract_Description__c != null) {
                            if(LegalAgRecord.Contract_Description__c.contains(agrtype)) {
                                LegalagremType.put(LegalAgRecord.Contract_Description__c, LegalAgRecord.id);
                            }
                        }
                    }
                }
            }            

            if(!LegalagremType.isEmpty() && AgreementType.size() > 0) {
                for(String agtype : AgreementType) {
                    if(LegalagremType.containsKey(agtype)) {
                        Legal_Agreements__c LegalAgreement = new Legal_Agreements__c();
                        LegalAgreement.id = LegalagremType.get(agtype);
                        LegalAgreement.Contract_Description__c = agtype ;
                        LegalAgreementList.add(LegalAgreement);
                    }
                    else {
                        Legal_Agreements__c LegalAgreement = new Legal_Agreements__c();
                        LegalAgreement.Opportunity__c = AgreeementRecord.echosign_dev1__Opportunity__c;
                        LegalAgreement.Account__c = AgreeementRecord.echosign_dev1__Opportunity__r.AccountId;           
                        LegalAgreement.Name =AgreeementRecord.echosign_dev1__Opportunity__r.Account.Name+ '-'+ agtype +' '+System.Today().year();
                        LegalAgreement.Contract_Description__c = agtype ;
                        LegalAgreementList.add(LegalAgreement);
                    }
                }
            }
                        
            if(!LegalAgreementList.isEmpty()) {
                upsert LegalAgreementList;
            }  
            
            if(ContentVersionList.size() > 0) {
                for(ContentVersion contver : ContentVersionList){
                    ContentVersionId.add(contver.id);
                } 
            }
            
            if(ContentVersionId.size() > 0) {
                Contentrec = [SELECT ContentDocumentId, Id, Title FROM ContentVersion where id =: ContentVersionId];
            }
            
            for(integer i = 0 ; i < Contentrec.size() ; i++)
            {
                ContentDocumentLink con = new ContentDocumentLink();
                con.ContentDocumentId = Contentrec[i].ContentDocumentId;
                con.LinkedEntityId = LegalAgreementList[i].id;
                con.ShareType = 'I';
                ContentDocumentLinkList.add(con);
            }
            
            if(ContentDocumentLinkList.size()>0){
                insert ContentDocumentLinkList;
            }   
        }
    }
}

Follow this link for bulkify your code and Apex Code Best Practices:
https://developer.salesforce.com/page/Apex_Code_Best_Practices

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Abhi MehtaAbhi Mehta
@ajay
This is still fetching a single record only...
AgreeementRecord = [select id,Name,echosign_dev1__Opportunity__c,echosign_dev1__Account__c,echosign_dev1__Account__r.name,echosign_dev1__Opportunity__r.AccountId,echosign_dev1__Opportunity__r.Account.Name from  echosign_dev1__SIGN_Agreement__c where id in : AgreementId limit 1];
 
Ajay K DubediAjay K Dubedi
Hi Abhi,
What is your requirement can you please explain.
As you put LIMIT 1 in the query that's why there is only one record.

Thanks,
Ajay Dubedi
Deepali KulshresthaDeepali Kulshrestha
Hi Abhi,
Greetings to you!

- It will fetch a single record only because it has LIMIT 1.
AgreeementRecord = [select id,Name,echosign_dev1__Opportunity__c,echosign_dev1__Account__c,echosign_dev1__Account__r.name,echosign_dev1__Opportunity__r.AccountId,echosign_dev1__Opportunity__r.Account.Name from  echosign_dev1__SIGN_Agreement__c where id in : AgreementId limit 1];
 
- If you want to get more than one record please use the below code : -
    
List<echosign_dev1__SIGN_Agreement__c> AgreeementRecord = new List<echosign_dev1__SIGN_Agreement__c>();
AgreeementRecord = [select id,Name,echosign_dev1__Opportunity__c,echosign_dev1__Account__c,echosign_dev1__Account__r.name,echosign_dev1__Opportunity__r.AccountId,echosign_dev1__Opportunity__r.Account.Name from  echosign_dev1__SIGN_Agreement__c where id in : AgreementId limit 10000];
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
Abhi MehtaAbhi Mehta
@deepali @ajay

Changing  the limit won't do anything...The whole process needs to be changed. That's why i posted it.