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
Kiran MarketingKiran Marketing 

Button when Clicked redirected to Contact Page with values

I have a requirement where I need to create a button on lead page when clicked it should redirect to Contact page with prepopulated values like  Lead Company Name - Account Name, Lead Phone - Contact Phone and so on.(standard pages) dont want to create a VF Page. Any suggestions would be appreciated.
Best Answer chosen by Kiran Marketing
SaranSaran
Hi Kiran,

Remove the after insert from the trigger 

Either do this 
trigger Referedtest on Lead (Before Insert) {
    LeadTriggerHelperClass2.isBeforeInsert(trigger.new);
}

or you can do like this
 
trigger Referedtest on Lead (Before Insert,After Insert) {
if(trigger.isBefore)
    LeadTriggerHelperClass2.isBeforeInsert(trigger.new);
}
Thanks
 

All Answers

ADITYA PRAKASH 5ADITYA PRAKASH 5
Yes, You can accomplish it using JS button on detail page. Through the JS button you can update the Contact records fileds using JS Soap API's that you want to show after redirecting to contact. And after updating simply redirect the page to Contact detail page.
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}
Kiran MarketingKiran Marketing
Aditya

The requirement is it shoud create a new Lead all together nothing to update. Like i have a contact who actually reffered me another contact which we consider it as a lead. So i need to create a Custom Button on Contact on detail page when clicked it shoud take me to a standard Lead page (just like creating a new lead) but with populated values from that contact on which i clicked the reffered button. (Basic fields like Company Name in Lead should be prepopulated with Account Name of contact and so on). If this can we acheived by JS can you guide me to a program or some example code so that i can build on it. Its actually kinda urgent.

Thanks and truly appreciate your help
SaranSaran
Hi Kiran

Create a custom button with the below code

window.open("https://salesforce domain url/00Q/e?retURL=00Q&lea3={!Account.Name}")

to open a lead edit page by pre populating the company name as contacts account name.

hope this helps.

User-added image 

Thanks.
ADITYA PRAKASH 5ADITYA PRAKASH 5
Kiran Marketing

1. Go to setup
2. Type Contact
3. Click on "Button and Links"
4. for Content Source slect "OnClick JavaScript"
 Use the below code then:
    {!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
    {!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}
    var contactObject = sforce.connection.query("SELECT Name, FROM Contact, WHERE Id = '{!Contact.Id}'");
    records = contactObject.getArray("records");
    var record = records[0];
    var objLead = new sforce.SObject("Lead");
    objLead.Name = record.Name;  //Similarly you can map other values of Lead before inserting it.
    var result = sforce.connection.create([objLead]);
    
    if(result[0].success == "true")
    {
        window.open("/objLead.Id");
    }
    
    This would definitely help you.
Kiran MarketingKiran Marketing
Saran / Adithya

Thanks for the help. But my problem persists.

I have alrady done with prepopulating certain values but that the problem is, i have a lookup / formula field created on user object where the present user id or account owner id should prepopulate. Not sure for some reason when i am creating a lookup / formula field on user its not getting a hyperlink its giving values but not like fields like owner id when u click your redirected to the user profile. I need this as i need to pull reports based on it.

Created formula field (Text) also tried with look up and with User.Id and below one both showing values but not as hyperlinks to the profile.

00N280000048Ny0={$User.FirstName + " "+ " " +$User.LastName}

Any help would be higly appreciated.
Kiran MarketingKiran Marketing
Saran / Adithya

I got that pat covered, however my below code is completely wrong i guess as once i prepopulate the values and when i choose a user in the reffered lookup field mentioned in above post. After clicking Save button the lead should be saved and its lead owner should be the one selected in reffered lookup field. Currently when i click it saves current user as Lead Owner.

Hence i wrote a trigger for this before insert on Lead but seems to be completely wrong. RecordType id = 01228000000YtCD
Tried with trigger.new too but its failing.
 
public class LeadTriggerHelperClass {
public static void isBeforeInsert(Map<Id,Lead> Lead)
{Lead Lea = New Lead();
 
    If(Lea.RecordTypeid == '01228000000YtCD')
    {
      Lea.OwnerId = Lea.Reffered_To__c;
    	Insert Lea;
    }
}
}
Any help would be highly appreciated

 
SaranSaran
Hi Kiran,

If you want to change the owner id of the particular lead based on the record type values then the below code would help you.

The parameter that you are passing to the method isBeforeInsert must be triggwe.newMap.

So,
 
public class LeadTriggerHelperClass {

public static void isBeforeInsert(Map<Id,Lead> leadMap)

 	id RecTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Record_Type_Name').getRecordTypeId();

 	for(Lead lea : leadMap.values())
 	{
	    If(Lea.RecordTypeid == RecTypeId)
	    {
	      Lea.OwnerId = Lea.Reffered_To__c;
	    }
    }
}

In the above code replace the record type name with your record type name.

Hope this helps 

Thanks

 
Kiran MarketingKiran Marketing
Saran

I am getting the below error.

Error - Referedtest: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.LeadTriggerHelperClass2.isBeforeInsert: line 7, column 1

User-added image
trigger Referedtest on Lead (Before Insert,After Insert) {
LeadTriggerHelperClass2.isBeforeInsert(trigger.newmap);

}
 
public class LeadTriggerHelperClass2 {

public static void isBeforeInsert(Map<Id,Lead> leadMap)
{
    id RecTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Refered').getRecordTypeId();

 	for(Lead lea : leadMap.values())
 	{
	    If(Lea.RecordTypeid == RecTypeId)
	    {
	      Lea.OwnerId = Lea.Reffered_To__c;
	    }
    }
}
}

 
SaranSaran
Hi Kiran,

I think you have some problem with your recordType Id.

Is the name Refered is the correct name?

Run this two lines in developer console and see if you can able to print the ID
 
id RecTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Refered').getRecordTypeId();]

system.debug('$$$$$' + recTypeId);

If you can able to print the Id then instead of passing trigger.newMap you can pass trigger.new.

So that,

Your trigger will be like 
 
trigger Referedtest on Lead (Before Insert,After Insert) {
if(trigger.isBefore)
    LeadTriggerHelperClass2.isBeforeInsert(trigger.new);
}

And your class wlll be 
 
public class LeadTriggerHelperClass2 {

public static void isBeforeInsert(list<Lead> leadList)
{
    id RecTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Refered').getRecordTypeId();

 	for(Lead lea : leadList)
 	{
	    If(Lea.RecordTypeid == RecTypeId)
	    {
	      Lea.OwnerId = Lea.Reffered_To__c;
	    }
    }
}
}

Thanks
Kiran MarketingKiran Marketing
Saran

Please find below is the error which is throwing up when i am trying to enter the Lead Record.

Error - Review all error messages below to correct your data.
Apex trigger Referedtest caused an unexpected exception, contact your administrator: Referedtest: execution of AfterInsert caused by: System.FinalException: Record is read-only: Class.LeadTriggerHelperClass2.isBeforeInsert: line 11, column 1


User-added image
SaranSaran
Hi Kiran,

Remove the after insert from the trigger 

Either do this 
trigger Referedtest on Lead (Before Insert) {
    LeadTriggerHelperClass2.isBeforeInsert(trigger.new);
}

or you can do like this
 
trigger Referedtest on Lead (Before Insert,After Insert) {
if(trigger.isBefore)
    LeadTriggerHelperClass2.isBeforeInsert(trigger.new);
}
Thanks
 
This was selected as the best answer
Kiran MarketingKiran Marketing
Saran

Lead is created but for reason Email is not firing.

Like i have created below code in the same class to send an email alert to the new lead owner.
 
public static void isAfterInsert(list<Lead> LeadList){
    
    
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        
    for(Lead EmailLead :[SELECT id FROM Lead WHERE id in :LeadList]){
        If(EmailLead.RecordTypeid == '01228000000YtCD'){
             String emailMessage ='A New Lead '
                 +'Has been Referred '
                 +'By a Sales Representative.' 
                 +' For Account'+ EmailLead.Company 
                 +' Requirement of ' 
                 + EmailLead.Description +
                 ' Lead Number'
                 + EmailLead.ID 
                 +' records were not updated successfully.';
            Messaging.SingleEmailMessage mail =    new Messaging.SingleEmailMessage();
           String[] toAddresses =new String[] {EmailLead.createdBy.email};
            mail.setToAddresses(toAddresses);
            mail.setReplyTo('noreply@salesforce.com');
            mail.setSenderDisplayName('Lead Created');
            mail.setSubject('Lead Created');
            mail.setPlainTextBody(emailMessage);
            mail.setHtmlBody(emailMessage);
            mails.add(mail);
        }
    }
    
      Messaging.sendEmail(mails);
   }
Truly appreciate your help
 
SaranSaran
Kiran,

Change the trigger like,
 
trigger Referedtest on Lead (Before Insert,After Insert) {
if(trigger.isBefore)
    LeadTriggerHelperClass2.isBeforeInsert(trigger.new);
if(trigger.isAfter)
    LeadTriggerHelperClass2.isAfterInsert(trigger.new);
}

Hope this solves that too.

Thanks