• Abhishek Kumar 407
  • NEWBIE
  • 20 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 6
    Replies
 When Lead is creating / updating - we need to find If any lead with same email exist If yes then merge all the lead with same email adress.

 Lead which has Max. "Last Sync Time" value [for same email adress] will consider as Master Lead and all other duplicate leads will be considered as child leads  and  all those child lead will merge to Master lead.
Below are four main fields which we will append value so no data loss will happened in below fields , rest all we will merge and who so ever is master lead fields will be the Winner and rest lead fields value will be discarded :

Description

here is the code, which I am trying
//Trigger

trigger leadMergeTrigger on Lead (after insert, after update)
{
	leadMergeTriggerHandler handler=new leadMergeTriggerHandler();
	handler.leadMerge(Trigger.new);   
}
 
//trigger handler class

public class leadMergeTriggerHandler
{
  public void leadMerge(List<Lead> leadList)
    {
       //Creating an object of List<String> to store email addresses
        List<string> email=new List<string>();
        
        //foreach loop to take email from trigger.new
        for(lead led : leadList)
        {
            //add the Email addresses to the List<String>
            email.add(led.Email);
        }
        
        //Query on Lead to fetch id,name and email
        List<lead> leadList1=[select id,Name,email from lead];
         
        //foreach loop to take records from Lead Object 
        for(Lead led1 : leadList1)
        {
            //foreach loop to take email from List<String> email and transfer it one by one to 'email1' of string type
            for(string email1 : email)
	         {
                //compare the email address, if same then merge the lead
                 if(led1.Email==email1)
		        {
                           
//what to do now
                    //[Select Id, Name, Last_Sync_Time__c from Lead order by Last_Sync_Time__c asc LIMIT 1];
                    
                }
                 
             }
            
        }
         
        
    }

}

 
For your reference I am posting Apex and VF code by which I can select only one attachement of .zip format. Kindly help.
(Kindly mark what code you are adding or removing with proper comments. As I am new to Salesforce, So it'd be easy to understand. Thanks in advance)

//Apex Code
public class emailWithAttach
{
    public String subject          {set;get;}
    public String body             {set;get;}
   
    public blob attach             {set;get;}
    public List<Contact> contacts  {set;get;}
      
    public emailWithAttach()
    {
        contacts=[Select Id, LastName, Email from Contact  where Email='abhishekk.twopirconsulting@gmail.com'];
    }
    public PageReference Send()
    {
         List<String> mails=new List<String>();
        For(Contact c:contacts)
        {
            mails.add(c.Email);
        }
        messaging.SingleEmailMessage msg1=new messaging.SingleEmailMessage();
        msg1.setSubject(subject);
        msg1.setPlainTextBody(body);
        msg1.setToAddresses(mails);
        Messaging.Email[] emails=new Messaging.Email[]{msg1};
        
        List<Messaging.EmailFileAttachment> fileAttachments=new List<Messaging.EmailFileAttachment>();
        Messaging.EmailFileAttachment efa=new messaging.EmailFileAttachment();
        efa.setFileName('Test.zip');
        efa.setBody(attach);
        fileAttachments.add(efa);
     
        msg1.setFileAttachments(fileAttachments);
        Messaging.sendEmail(emails);
        PageReference p=new PageReference('https://mail.google.com/mail/u/0/#inbox');
        p.setRedirect(true);
        return p;
    }

}
//VF Code
<apex:page controller="emailWithAttach">
    <apex:form >
        <apex:pageBlock title="Email with multiple Attachement">
            <apex:pageBlockButtons location="Bottom">
            	<apex:commandButton value="Send" action="{!Send}"/>
            </apex:pageBlockButtons>
           
            Subject:<br/><apex:inputText value="{!subject}" size="60"/><br/><br/>
            Attachement for Zip file only:<br/><apex:inputFile value="{!attach}" /><br/><br/>
            Body:<br/><apex:inputTextarea value="{!body}"  cols="60" rows="20"/>
           
        </apex:pageBlock>
    </apex:form>
</apex:page>




 
Here is the Apex and VF code for the reference:-

//Apex Code
 
public class sendEmailbuttonApex
{
    public Account accounts                     {set;get;}
    public String subject 				{set;get;}
    public String body 			        {set;get;}
    public blob Attach                                {set;get;}  

    public sendEmailbuttonApex(Apexpages.StandardController controller)
    {
        accounts=(Account)controller.getRecord();
        accounts=new Account(Name='Test');
    }
    public PageReference sendEmail()
    {
        List<Contact> conList=[Select Id, Email from contact where Email='abhishekk.twopirconsulting@gmail.com'];
        system.debug(conList);
        List<String> mail=new List<String>();
        for(Contact c:conList)
        {
            mail.add(c.Email);
        }
        Messaging.SingleEmailMessage msg1=new Messaging.SingleEmailMessage();
        msg1.setToAddresses(mail);
        msg1.setSubject(subject);
        msg1.setPlainTextBody(body);
        Messaging.Email[] emails=new Messaging.Email[]{msg1};
        Messaging.sendEmail(emails);
        insert accounts;
        Blob b = Attach;
        Attachment At = new Attachment(Name ='NewFile',body = b,ParentId=accounts.Id);
        insert At; 
        
        PageReference p=new PageReference('https://mail.google.com/mail/u/0/#inbox');
        p.setRedirect(true);
        return p;
        
    }
}

//VF code
 
<apex:page docType="HTML-5.0" standardController="Account" extensions="sendEmailbuttonApex">
    <head lang="en">
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width,initial-scale=1.0"/>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        </head>
        <body>
           <apex:form >
                <script src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
     		<br /><br />
        <apex:outputLabel value="Subject" for="Subject"/>:<br />     
        <apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
        <br /><br />
        <apex:outputLabel value="Body" for="word_count"/>:<br />     
        <apex:inputTextarea id="word_count" cols="30" rows="10" value="{!body}" />
        <br />
        Total word Count : <span id="display_count">0</span> words. Words left : <span id="word_left">40</span>
         <br /><br /><br />
        <apex:commandButton value="Send Email" action="{!sendEmail}" /> 
        <apex:inputFile value="{!Attach}"></apex:inputFile>
           </apex:form>
    	<script>
 			$(document).ready(function() {
			//   alert('Hello, jQuery');
            $('[id$=word_count]').on('keyup', function(e) {
                //  alert('hey');
                //j$('[id$=word_count]').on('keydown', function(e) {
                var words = $.trim(this.value).length ? this.value.match(/\S+/g).length : 0;
                if (words <= 40) {
                   $('[id$=display_count]').text(words);
                   $('[id$=word_left]').text(40-words)
                }else{
                    if (e.which !== 8) e.preventDefault();
                }
            });
        }); 
        </script>
    </body>
</apex:page>

​ 
Here is the apex and VF code:
//Apex Code

 public class sendEmailbuttonApex
{
    public Account accounts            {set;get;}
    public String subject { get; set; }
    public String body { get; set; }
    public sendEmailbuttonApex(Apexpages.StandardController controller)
    {
        accounts=(Account)controller.getRecord();
    }
    public PageReference sendEmail()
    {
        List<Contact> conList=[Select Id, Email from contact where Email='abhishekk.twopirconsulting@gmail.com'];
        system.debug(conList);
        List<String> mail=new List<String>();
        for(Contact c:conList)
        {
            mail.add(c.Email);
        }
        Messaging.SingleEmailMessage msg1=new Messaging.SingleEmailMessage();
        msg1.setToAddresses(mail);
        msg1.setSubject(subject);
        msg1.setPlainTextBody(body);
        Messaging.Email[] emails=new Messaging.Email[]{msg1};
        Messaging.sendEmail(emails);
        PageReference p=new PageReference('/'+accounts.id);
        p.setRedirect(true);
        return p;

    }
}
 
//VF code

<apex:page docType="HTML-5.0" standardController="Account" extensions="sendEmailbuttonApex">
<apex:form>
      <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
        <script>
        $(document).ready(function() {
            $("#word_count").on('keydown', function(e) {
            //j$('[id$=word_count]').on('keydown', function(e) {
        var words = $.trim(this.value).length ? this.value.match(/\S+/g).length : 0;
        if (words <= 200) {
            $('#display_count').text(words);
            $('#word_left').text(200-words)
        }else{
            if (e.which !== 8) e.preventDefault();
        }
    });
 }); 
       </script>
   
    
		<br /><br />
			<apex:outputLabel value="Subject" for="Subject"/>:<br />     
			<apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
			<br /><br />
			<apex:outputLabel value="Body" for="word_count"/>:<br />     
             <apex:inputTextarea id="word_count" cols="30" rows="10" value="{!body}"/>
        <br />
Total word Count : <span id="display_count">0</span> words. Words left : <span id="word_left">200</span>

			<br /><br /><br />
        <apex:commandButton value="Send Email" action="{!sendEmail}" /> 
		</apex:form>
</apex:page>

 
Here is the Apex and VF code for the reference:-

//Apex Code
 
public class sendEmailbuttonApex
{
    public Account accounts                     {set;get;}
    public String subject 				{set;get;}
    public String body 			        {set;get;}
    public blob Attach                                {set;get;}  

    public sendEmailbuttonApex(Apexpages.StandardController controller)
    {
        accounts=(Account)controller.getRecord();
        accounts=new Account(Name='Test');
    }
    public PageReference sendEmail()
    {
        List<Contact> conList=[Select Id, Email from contact where Email='abhishekk.twopirconsulting@gmail.com'];
        system.debug(conList);
        List<String> mail=new List<String>();
        for(Contact c:conList)
        {
            mail.add(c.Email);
        }
        Messaging.SingleEmailMessage msg1=new Messaging.SingleEmailMessage();
        msg1.setToAddresses(mail);
        msg1.setSubject(subject);
        msg1.setPlainTextBody(body);
        Messaging.Email[] emails=new Messaging.Email[]{msg1};
        Messaging.sendEmail(emails);
        insert accounts;
        Blob b = Attach;
        Attachment At = new Attachment(Name ='NewFile',body = b,ParentId=accounts.Id);
        insert At; 
        
        PageReference p=new PageReference('https://mail.google.com/mail/u/0/#inbox');
        p.setRedirect(true);
        return p;
        
    }
}

//VF code
 
<apex:page docType="HTML-5.0" standardController="Account" extensions="sendEmailbuttonApex">
    <head lang="en">
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width,initial-scale=1.0"/>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        </head>
        <body>
           <apex:form >
                <script src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
     		<br /><br />
        <apex:outputLabel value="Subject" for="Subject"/>:<br />     
        <apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
        <br /><br />
        <apex:outputLabel value="Body" for="word_count"/>:<br />     
        <apex:inputTextarea id="word_count" cols="30" rows="10" value="{!body}" />
        <br />
        Total word Count : <span id="display_count">0</span> words. Words left : <span id="word_left">40</span>
         <br /><br /><br />
        <apex:commandButton value="Send Email" action="{!sendEmail}" /> 
        <apex:inputFile value="{!Attach}"></apex:inputFile>
           </apex:form>
    	<script>
 			$(document).ready(function() {
			//   alert('Hello, jQuery');
            $('[id$=word_count]').on('keyup', function(e) {
                //  alert('hey');
                //j$('[id$=word_count]').on('keydown', function(e) {
                var words = $.trim(this.value).length ? this.value.match(/\S+/g).length : 0;
                if (words <= 40) {
                   $('[id$=display_count]').text(words);
                   $('[id$=word_left]').text(40-words)
                }else{
                    if (e.which !== 8) e.preventDefault();
                }
            });
        }); 
        </script>
    </body>
</apex:page>

​ 
Here is the apex and VF code:
//Apex Code

 public class sendEmailbuttonApex
{
    public Account accounts            {set;get;}
    public String subject { get; set; }
    public String body { get; set; }
    public sendEmailbuttonApex(Apexpages.StandardController controller)
    {
        accounts=(Account)controller.getRecord();
    }
    public PageReference sendEmail()
    {
        List<Contact> conList=[Select Id, Email from contact where Email='abhishekk.twopirconsulting@gmail.com'];
        system.debug(conList);
        List<String> mail=new List<String>();
        for(Contact c:conList)
        {
            mail.add(c.Email);
        }
        Messaging.SingleEmailMessage msg1=new Messaging.SingleEmailMessage();
        msg1.setToAddresses(mail);
        msg1.setSubject(subject);
        msg1.setPlainTextBody(body);
        Messaging.Email[] emails=new Messaging.Email[]{msg1};
        Messaging.sendEmail(emails);
        PageReference p=new PageReference('/'+accounts.id);
        p.setRedirect(true);
        return p;

    }
}
 
//VF code

<apex:page docType="HTML-5.0" standardController="Account" extensions="sendEmailbuttonApex">
<apex:form>
      <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
        <script>
        $(document).ready(function() {
            $("#word_count").on('keydown', function(e) {
            //j$('[id$=word_count]').on('keydown', function(e) {
        var words = $.trim(this.value).length ? this.value.match(/\S+/g).length : 0;
        if (words <= 200) {
            $('#display_count').text(words);
            $('#word_left').text(200-words)
        }else{
            if (e.which !== 8) e.preventDefault();
        }
    });
 }); 
       </script>
   
    
		<br /><br />
			<apex:outputLabel value="Subject" for="Subject"/>:<br />     
			<apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
			<br /><br />
			<apex:outputLabel value="Body" for="word_count"/>:<br />     
             <apex:inputTextarea id="word_count" cols="30" rows="10" value="{!body}"/>
        <br />
Total word Count : <span id="display_count">0</span> words. Words left : <span id="word_left">200</span>

			<br /><br /><br />
        <apex:commandButton value="Send Email" action="{!sendEmail}" /> 
		</apex:form>
</apex:page>

 
Dear all, 
I am trying to solve one of the Trailhead Challenges in the unit: Creating Test Data for Apex Tests and is giving me the following error:

Challenge Not yet complete... here's what's wrong: 
Executing the 'generateRandomContacts' method failed. Either the method does not exist, is not static, or did not return the correct set of Contact records.

The following is the code; 
public class RandomContactFactory {

    public static List<Contact> generateRandomContacts (integer nNumContacts, string sLastName)
    {
        List<Contact> lContactList = new List<Contact>();
        
        for(integer i=0; i<nNumcontacts; i++)
        {
            Contact c = new Contact(LastName= sLastName + ' ' +i);
            lContactList.add(c);

        }    
        return lContactList;
    }
    
}

What I need to solve is the following:

 Create an Apex class that returns a list of contacts based on two incoming parameters: one for the number of contacts to generate, and the other for the last name. The list should NOT be inserted into the system, only returned. The first name should be dynamically generated and should be unique for each contact record in the list.The Apex class must be called 'RandomContactFactory' and be in the public scope.
The Apex class should NOT use the @isTest annotation.
The Apex class must have a public static method called 'generateRandomContacts' (without the @testMethod annotation).
The 'generateRandomContacts' method must accept an integer as the first parameter, and a string as the second. The first parameter controls the number of contacts being generated, the second is the last name of the contacts generated.
The 'generateRandomContacts' method should have a return type of List<Contact>.
The 'generateRandomContacts' method must be capable of consistently generating contacts with unique first names.
For example, the 'generateRandomContacts' might return first names based on iterated number (i.e. 'Test 1','Test 2').
The 'generateRandomContacts' method should not insert the contact records into the database.

Can anyone help me? 

Thanks!

Bea

 
I have this very simple class..  
trigger RestrictContactByName on Contact (before insert, before update) {
    //check contacts prior to insert or update for invalid data
    For (Contact c : Trigger.New) {
        if(c.LastName == 'INVALIDNAME') {   //invalidname is invalid
            c.AddError('The Last Name "'+c.LastName+'" is not allowed for DML');
        }
    }
}
.. and the corresponding Test Class:  
@isTest
private class TestRestrictContactByName {

	@isTest static void metodoTest() {
		
		List listaContatti = new List();
		Contact c1 = new Contact(FirstName='Francesco', LastName='Riggio');
		Contact c2 = new Contact(LastName = 'INVALIDNAME');
		listaContatti.add(c1);
		listaContatti.add(c2);
		
		//insert listaContatti;
		
		// Perform test
        Test.startTest();
        Database.SaveResult [] result = Database.insert(listaContatti, false);
        Test.stopTest(); 
		
		c1.LastName = 'INVALIDNAME';
		update c1;
       		
	}
	
}

When I run the Test class from the Developer Console I get 100% of coverage on the RestrictContactByName class but, when I check the challenge on the trailhead it returns the error:

Challenge not yet complete... here's what's wrong: The 'RestrictContactByName' class did not achieve 100% code coverage via your test methods

Has someone had my same issue?
I started a Visualforce template where I call a Static Resource (just happens to be an image file) - it appears great in the preview, but when the e-mail is sent the user gets a red box instead of the picture.
 
Is it possible for static resources or documents to be available to non-sf users outside of salesforce? Can I use them in a visual force template or link via a URL without authentication?
 
In my template I call it using
Code:
<apex:image url="{!$Resource.LetterHead}" />

 


Message Edited by BrianWK on 12-30-2008 11:02 AM