You need to sign in to do that
Don't have an account?
Abhi 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;
}
}
}
}
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;
}
}
}
}
Try this code, try to put the null check condition in your code at every place where it is required:
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
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];
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
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 : -
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.
Changing the limit won't do anything...The whole process needs to be changed. That's why i posted it.