1

I have an object called Sales_and_Marketing__c, and I would like to override the standard New button so that the field Sales_Contact__c (lookup to Contact) gets pre populated with the logged in User.

On the User object we have a field ContactId__c. This field is a of a data type of Text(18) and just holds the contact id.

I have looked on online for scenarios and have attempted to write this but i am a little confused and appreciate some help

Apex class :

public with sharing class NewMKtMaterialsRequest

{
    public Contact profileInfo {get;set;}
    public NewMKtMaterialsRequest(ApexPages.StandardController controller)
    //public NewMKtMaterialsRequest() 
{
        Id userId = UserInfo.getUserId();
        List<User> UserList = [select U.ContactId from User U where Id =: userId];
        Id contactId = UserList.get(0).ContactId;
//Contact profileInfo = [SELECT ID FROM Contact WHERE Id =: contactId LIMIT 1];

        Contact profileInfo = [SELECT 
            Id, 
            FirstName, 
            User__c, 
            User__r.Id, 
            User__r.Name 
        FROM Contact WHERE Id =: contactId LIMIT 1];

  }
public PageReference RedirectToMKTRequest()
    {
        return new PageReference('/a0R/e?Sales_Contact__c=contactId.id&nooverride=1');
  }
}

Visual Force Page ;

  <apex:page standardController="sales_and_marketing__c" extensions="NewMKtMaterialsRequest" action="{!RedirectToMKTRequest}">

        </apex:page>
0

2 Answers 2

1

You could create PageReference instance and put the parameters like this:

public PageReference RedirectToMKTRequest()
{
    PageReference pg = controller.edit();
    pg.getParameters().put('Sales_Contact__c', contactId);
    pg.getParameters().put('nooverride', '1');
    pg.setRedirect(true);
    return pg;
}
4
  • how can i pursue this avenue ?
    – Masond3
    Mar 27, 2017 at 12:34
  • @Masond3, Replace Sales_Contact__c with CF00N61000008ZRki in this code, the prepopulation would then work.
    – Raul
    Mar 28, 2017 at 11:37
  • @rahual - i get the following error List has no rows for assignment to SObject An unexpected error has occurred. Your development organization has been notified.
    – Masond3
    Mar 28, 2017 at 11:45
  • @Masond3, you should always check the list before accessing it.
    – Raul
    Mar 28, 2017 at 13:47
0

If I understand your issue correctly, I think I see the issue in here. The PageRef you're building:

return new PageReference('/a0R/e?Sales_Contact__c=contactId.id&nooverride=1');

You have the 'Sales_Contact__c=contactId.id' part, but it's inside quotes, meaning it will literally append the literal text 'contactId.id' in the URL.

Try this:

return new PageReference('/a0R/e?Sales_Contact__c=' + profileInfo.Id + '&nooverride=1');
4
  • @Dominator_101- if i include your logic i get the following error "Variable does not exist: contactId.id"
    – Masond3
    Mar 24, 2017 at 15:58
  • @Masond3 Sorry, I didn't pay attention to your other variable names. Try this instead of contactId.id (I'll update the post): profileInfo.Id Mar 24, 2017 at 16:21
  • That allowed me to save. Within my VisualForce page i need to include the field i am passing the value dont i ?
    – Masond3
    Mar 24, 2017 at 16:58
  • Not sure what you mean...BUt after a quick check, it looks like to pre-pop stuff on an edit page you might need to use the field's Id, not it's API name. To get that, go to the object's fields, click on the field you want to populate, then take the Id out of the URL. You'll also need to put 'CF' in the front of the Id. For example, say the field Id is '00N61000008ZRki', you'll need to put 'CF00N61000008ZRki=' instead of 'Sales_Contact__c='. Mar 24, 2017 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.