• Sree07
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 6
    Replies
I have a requirement like
When ever user is deactivated we have to send an email to deactivated user and it's supervisor with details of that user.
  • November 15, 2021
  • Like
  • 0
for (ContentVersion document: documents) {           
  Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();                          attachment.setBody(document.VersionData);
                  attachment.setFileName(document.Title);
                  attachments.add(attachment);        
 }
  • November 10, 2021
  • Like
  • 0
I want to create more than 50,000 records in account object using code.Later I want to update Rating field to 'Warm' to all the records in present in account object.
  • October 08, 2021
  • Like
  • 0
I have a requirement which is
If account name field in account object contains any value from industry field(picklist),the word contained in account name field should be updated in industry picklist using apex. how can i do that?Can u please help me with this requirement.I have written the following code:


trigger updateIndustryFieldTrigger on Account (before update) {
    List<Account> accList = [SELECT Id,Name FROM Account];
    for(Account acc : Trigger.New){
        for(Account a : acc.Industry){
            if(acc.Name.contains(acc.Industry)){
                acc.Industry += acc.Industry;
            }else{
                acc.Industry = 'Other';
            }
        }
    }
}
  • October 01, 2021
  • Like
  • 0
I want to create more than 50,000 records in account object using code.Later I want to update Rating field to 'Warm' to all the records in present in account object.
  • October 08, 2021
  • Like
  • 0
I have a requirement which is
If account name field in account object contains any value from industry field(picklist),the word contained in account name field should be updated in industry picklist using apex. how can i do that?Can u please help me with this requirement.I have written the following code:


trigger updateIndustryFieldTrigger on Account (before update) {
    List<Account> accList = [SELECT Id,Name FROM Account];
    for(Account acc : Trigger.New){
        for(Account a : acc.Industry){
            if(acc.Name.contains(acc.Industry)){
                acc.Industry += acc.Industry;
            }else{
                acc.Industry = 'Other';
            }
        }
    }
}
  • October 01, 2021
  • Like
  • 0
I am short coverage when I deploy to production by 5%. Im at 88% total for this trigger, please, can anyone help?

Here is my trigger 
trigger filingSuccessfulEmailWithDocs on Case (after update) {
//still need text in body of email, template, criteria for trigger
    OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress where Displayname='Name' limit 1];
    String subject = 'Subject';
    String plainTextBody ='Email text here';

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
    for(Case myCase : trigger.new){
        Case oldCase = Trigger.oldMap.get(myCase.Id);
           if(myCase.RecordTypeName__c=='Order' && myCase.Type == 'Filings' && myCase.Email_Client_Attached_Docs__c==True
              && myCase.Email_Client_Attached_Docs__c != oldCase.Email_Client_Attached_Docs__c){ 
    String requirements = myCase.Report_Requirements__c;            
    String plainTextBody ='';
        plainTextBody +=
             
    // Get ContentVersion
    List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
    for (ContentDocumentLink docLinks : [Select Id, ContentDocumentId  from ContentDocumentLink where LinkedEntityId = :myCase.id ]) { 
       for (ContentDocument docs : [Select Id, FileType, Title, FileExtension from ContentDocument where Id= :docLinks.ContentDocumentId]) {
           for (ContentVersion docVersion : [Select Id, VersionData from ContentVersion where ContentDocumentId =:docLinks.ContentDocumentId ]) {

               Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();

               string fileName = docs.Title.Trim() + '.' + docs.FileExtension.Trim();
               efa.setFileName(fileName); //Title of the PDF
               efa.setBody(docVersion.VersionData); //Body of the PDF,need to do transfer into blob
               fileAttachments.add(efa);

           }
               email.setFileAttachments(fileAttachments);
       }
    }
    //add address's that you are sending the email to
    List<String> sendTo = new List<String>();
    String owneremail = [select Email from User where Id = :myCase.OwnerId].Email;
    sendTo.add(myCase.ContactEmail);
    //sendTo.add(owneremail);

    email.setSubject(subject);
    String[] toAddresses = (sendto);  
    email.setPlainTextBody(plainTextBody);  
    email.setToAddresses( toAddresses );
    email.setReplyTo('DoNotReply@xxx.com');           
    email.setOrgWideEmailAddressId(owa.id);
    email.setSaveAsActivity(true);   
    
    // Sends the email  
    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
       
       EmailMessage em = new EmailMessage();
        em.FromAddress = owa.Address;
        em.FromName = 'DoNotReply';
        em.HtmlBody = email.getHtmlBody();
        em.Subject = email.Subject;
        em.Incoming = false;
        em.Status = '3';
        em.ParentId = myCase.id;
        em.TextBody = email.getPlainTextBody();
        em.FromAddress = owa.address;
        em.ToAddress = myCase.ContactEmail;
        em.ValidatedFromAddress = owa.Address;

        insert em;

        EmailMessageRelation emr = new EmailMessageRelation();
        emr.emailMessageId = em.Id;
        emr.relationId = myCase.ContactId; 
        emr.relationType = 'ToAddress'; // OtherAddress, FromAddress,....
        insert emr;
    }
    }

This is what isnt covered currently 
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
    for (ContentDocumentLink docLinks : [Select Id, ContentDocumentId  from ContentDocumentLink where LinkedEntityId = :myCase.id ]) { 
       for (ContentDocument docs : [Select Id, FileType, Title, FileExtension from ContentDocument where Id= :docLinks.ContentDocumentId]) {
           for (ContentVersion docVersion : [Select Id, VersionData from ContentVersion where ContentDocumentId =:docLinks.ContentDocumentId ]) {

               Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();

               string fileName = docs.Title.Trim() + '.' + docs.FileExtension.Trim();
               efa.setFileName(fileName); //Title of the PDF
               efa.setBody(docVersion.VersionData); //Body of the PDF,need to do transfer into blob
               fileAttachments.add(efa);

           }
               email.setFileAttachments(fileAttachments);
       }

Here is my test class with 88% Coverage
 
@isTest
private class filingEmailWithDocsTest {
    @isTest static void createTest() {
        Contact con = New Contact();
        con.FirstName = 'Hanna';
        con.LastName = 'Banana';
        con.Email = 'hbanana@aol.com';
        insert con;
        
        State_Information__c state = New State_Information__c();
        state.state_code__c = 'MI';
        state.Name = 'Michigan';
        insert state;

        Case myCase = new Case();
        myCase.Subject = 'Hanna';
        myCase.ContactId=con.id;
        myCase.State_Filed_In1__c= state.Id;
        myCase.Status='Closed - Resolved';
        myCase.Type='Filings';
        myCase.Email_Client_Attached_Docs__c=False;
        myCase.RecordTypeId='012F0000000WjvwIAC';
        insert myCase;
        
        Case caseToUpdate;
        caseToUpdate=[SELECT id from Case where id=: myCase.Id];
        CaseToUpdate.Email_Client_Attached_Docs__c=TRUE;
        update caseToUpdate;

        //Create Attachment for testing
        Attachment Attachment =new Attachment();
        Attachment.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        Attachment.body=bodyBlob;
        Attachment.parentId=myCase.id;
        insert Attachment ;
        
        ContentVersion contentVersion = new ContentVersion(
            Title = 'Penguins',
            PathOnClient = 'Penguins.jpg',
            VersionData = Blob.valueOf('Test Content'),
            IsMajorVersion = true
        );
        insert contentVersion;    
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
        
        //create ContentDocumentLink  record 
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = myCase.id;
        cdl.ContentDocumentId = documents[0].Id;
        cdl.shareType = 'V';
        insert cdl;
        

		}
    }

 
Hi,

I am sending an email notification if a field is changed on Content Version object through Apex as we cannot create Workflows/processbuilder on Contentversion object. In my Apex code, I am referring to a email template which I created for this purpose.  In the body of email I am trying to include few ContentVersion fields, but there is no option to select merge fields for Content Version object. 

Is there a way I can use ContentVersion fields in my email template ?

Hi Experts,

I am trying to do bulk insert of new accounts in SF using batch apex concept.
 I went thru the Database.Batchable<sObject>  and guess the records fetched in Database.QueryLocator is what processed in execute method .... which means it does only update/delete? how to insert record?

how i can accomblish inserting new records in bulk >50k in SF. Kindly assist.

Also to start with i  tried with update... I dont see any records updated.... instead i see in console with message ("source for Batch Apex could not be loaded) here is the sample.... please let me know where am missing.

global class batchExample implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute         
        String query = 'SELECT Id,Name FROM Account limit 10'  ;         
        return Database.getQueryLocator(query);       
    }
     
    global void execute(Database.BatchableContext BC, List<Account> scope) {
        List<Account> accList = new List<Account>();
        // process each batch of records
          for (integer i=0;i<100;i++)
                {
        Account anew= new Account(Name ='theanu'+i,BillingCity='SA port'+i,Phone='1212'+i);
        accList.add(anew);         
            
               }
         
        try {
            // Update the Account Record
            insert accList;
            
         
         
        } catch(Exception e) {
            System.debug(e);
        }
         
    }        
    global void finish(Database.BatchableContext BC) {
        // execute any post-processing operations
       
  }
}



 

  • February 20, 2018
  • Like
  • 0
Hai, i tried to insert records through batch apex but it can't ,tell me ,my code is

global class opportunityincrement implements database.batchable<sObject>{
 global database.QueryLocator start(database.batchableContext bc){
  string query='select id,Name,Job_Description__c,Max_Pay__c,Min_Pay__cfrom Position__c';
  return database.getQueryLocator(query);
  }
 global void execute(database.batchableContext bc,list<Position__c>scope){
  system.debug(scope+'bbbbbbbbbbbbbbbbbbb');
 
  list<Position__c> oppty=new list<Position__c>();
   for(Position__c opp:scope){
  
  
   opp.Name='Test';
   opp.Job_Description__c= 'Closed';
   opp.Max_Pay__c= 10000;
    opp.Min_Pay__c= 5000;
   
    oppty.add(opp);
     system.debug(oppty+'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
  
   
   }
    insert oppty;
   }
   global void finish(database.batchableContext bc){
   
   }
  }