0

I created a custom object named REDTasks with lookup fields to both Contacts and Accounts. On my Contact page, I have created a custom button on the related list to create a NEW REDTask. When I click the button to create a NEW REDTask, I need both the Contact Name and the Account to auto populate in the new REDTask. I have tried everything I can think of but I can only get one or the other (Contact Name or Account) to populate -never both at the same time. What am I doing wrong?

Here's my code that will pull in the Contact Name but not the Account:

/a00/e?CF00Nf100000BqZ57={!RED_Tasks__c.ContactName__c}&CF00Nf100000BqZ57_lkid= {!RED_Tasks__c.ContactNameId__c}&CF00Nf100000BqvWZ={!RED_Tasks__c.Account__c}&retURL={!RED_Tasks__c.ContactNameId__c}

Here's my code that will pull in the Account but not the Contact Name:

/a00/e?CF00Nf100000BqZ57={!RED_Tasks__c.ContactName__c}&CF00Nf100000BqZ57_1kid={!RED_Tasks__c.ContactNameId__c}&CF00Nf100000BqvWZ={!Account.Name}&retURL={!RED_Tasks__c.ContactNameId__c}

Where am I messing up?

2 Answers 2

0

You need the field name and lkid hidden field, and you should be merging using Contact and Account. I'd also recommend using URLFOR. Here's what I would write:

{!URLFOR($Action.RED_Tasks__c.New, null, 
   [CF00Nf100000BqZ57=Contact.Name, 
    CF00Nf100000BqZ57_lkid=Contact.Id, 
    CF00Nf100000BqvWZ=Account.Name, 
    CF00Nf100000BqvWZ_lkid=Account.Id], true)}
1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Adrian Larson
    Jun 26, 2017 at 23:23
0

For new record which you are creating from Contact it could take Account Name based on the Contact to Account relationship. For that scenario you need to fetch account information from SOQL.

Here is a sample code that can be written behind javascript button when page is opened from Contact:

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/19.0/apex.js")}

var url = "a00/e";
document.location.href = '{!$Site.CurrentSiteUrl}';

var callingURL = document.location.href;
var position = callingURL.lastIndexOf("/");
var recordId = callingURL.substring(position + 1,position + 1+ 15);
if(recordId.length==15 && recordId.startsWith('003'))
{
    var contactRec = sforce.connection.query("select Name, Account.Name, Account.Id from Contact where Id='" + recordId + "'");
    var records1 = contactRec.getArray('records');
    if(records1 !=null)
    {
        var strURL = url + "?CF00Nf100000BqZ57=" + encodeURIComponent("{!RED_Tasks__c.ContactName__c}")
        + "&CF00Nf100000BqZ57_lkid=" +  encodeURIComponent("{!RED_Tasks__c.ContactNameId__c}")
        + "&CF00Nf100000BqvWZ=" + encodeURIComponent(contactRec.records.Account.Id)
        + "&retURL=" + encodeURIComponent("{!RED_Tasks__c.ContactNameId__c}");

        window.open(strURL, "_self");
    }
}

Now, for new record created from Account, you cannot able to link the Contact Name. You can only have AccountId and Account Name.

From Account if we get the single contact from the SOQL then that can be passed.

Now it's up to you how you can handle this Contact population.

2
  • There's no need for a query here, or most of this JavaScript. It simply won't work as it's written, either. You may want to take a few minutes to review your code.
    – sfdcfox
    Jun 26, 2017 at 17:52
  • I have tried to utilize my sample code. But the question is how to get Contact info without having a query if it is opened from Account details page? Jun 26, 2017 at 17:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .