You need to sign in to do that
Don't have an account?
Custom JavaScript Button to Query for Contact and populate him lookup Field
Hello there,
I have a custom object where I allow my users to add / attach contacts given from the information from a 3rd party.
I have a created a Custom JavaScript Button, that - besides some other stuff - checks in its beginning, whether the "name" inside the field "Ansprechpartner" matches a contact inside the attached Account "Hobby Dittrich".
If no contacts with this name are found, a new contact is created in the account and attached to my custom object in the field "Kontakt". (last part of my code)
What I need now is the case where the contact already exists in the account but is not yet attached to my custom object. In this specific example (screenshot) I know the contact already exists in the account. I need my button to take the queried contactID (contactsQuery?) and with it, populate my lookup field (Kontakt) so that the contact is on my custom object.
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} var contactsQuery = sforce.connection.query ("SELECT Id FROM Contact WHERE AccountId='{!Leadevents__c.AccountId__c}' AND Salutation = '{!Leadevents__c.Anrede_JRi__c}'AND LastName = '{!Leadevents__c.Nachname_JRi__c}' "); var contactsRecords = contactsQuery.getArray("records"); if( contactsRecords.length > 0 & "{!Leadevents__c.Kontakt__c}" == "") { alert("Kontakt existiert bereits im Account. Bitte dem Leadevent zuordnen"); } else if ("{!Leadevents__c.Nachname_JRi__c}" == "" || "{!Leadevents__c.Anrede_JRi__c}" == "") { alert('Es stehen nicht genügend Informationen zur Anlage eines neuen Kontaktes zur Verfügung.'); } else if ("{!Leadevents__c.Nachname_JRi__c}" == "{!Leadevents__c.Nachname_des_AP_auf_dem_Kontakt__c}" & "{!Leadevents__c.Anrede_JRi__c}" == "{!Leadevents__c.Anrede_des_AP_auf_dem_Kontakt__c}") { alert("Kontakt wurde bereits dem Leadevent zugeordnet."); } else { var Con = new sforce.SObject("Contact"); Con.Salutation = '{!Leadevents__c.Anrede_JRi__c}'; Con.FirstName = '{!Leadevents__c.Vorname_JRi__c}'; Con.LastName = '{!Leadevents__c.Nachname_JRi__c}'; Con.AccountId = '{!Leadevents__c.Account_ID__c}'; Con.Position__c = 'Personal-Referent'; Con.LeadSource = '{!Leadevents__c.Kategorie__c}'; Con.Phone = '{!Leadevents__c.Telefon_JRi__c}'; Con.Email = '{!Leadevents__c.E_Mail_JRi__c}'; Con.erstellt_durch_Leadevent__c = 1; var result = sforce.connection.create([Con]); if (result[0].getBoolean("success")) { alert('Kontakt wird erstellt und dem Leadevent zugeordnet.'); var p = new sforce.SObject("Leadevents__c"); p.Id="{!Leadevents__c.Id}"; p.Kontakt__c = result[0].id; var result1 = sforce.connection.update([p]); if(result1[0].getBoolean("success")) location.reload(true); else alert('Error : '+result1); } }Thanks in Advance and Greetings!
Jan
Your code is complete but doesn't work as expected?
The record Ids have always an uppercase "I".
https://developer.salesforce.com/docs/atlas.en-us.ajax.meta/ajax/sforce_api_ajax_embedding.htm
p.Kontakt__c = result[0].Id;
You can verify the values by using: JSON.stringify ( )
console.log( JSON.stringify( result1 ) );
Chrome: Open the Console panel to view logged messages or run JavaScript
Press Command+Option+J (Mac) or Control+Shift+J (Windows, Linux, Chrome OS) to jump straight into the Console panel.
thanks for your quick reply. The code in its current form works just perfect. But I am actually looking for some new behaviour around this part:
I achieved to highlight an alert that says sth like tthis: "Hey, the contact you are trying to add to the account already exists there" - "Now, please attach him manually (via the lookup field) to the custom object"
Now I do not want my users to do any unnecessary work. I believe that thanks to "var contactsQuery = sforce.connection.query" we already have the contactId in question. What I do not achieve is to enter this id into my contact lookup to attach it to my custom object.
The goal is to attach the contact from my query (found on the account) to my custom object.
Hope that clears things up. Thanks!
What I need for my final solution is to replace le.Kontakt__c = '0031l000008bVb1' with the dynamic ID pulled from the sforce.connection.query. But I cannot figure out what variable to define in order to "grab" it
var contactsQuery = sforce.connection.query
("SELECT Id FROM Contact WHERE AccountId='{!Leadevents__c.AccountId__c}' AND Salutation = '{!Leadevents__c.Anrede_JRi__c}'AND LastName = '{!Leadevents__c.Nachname_JRi__c}' ");
var contactsRecords = contactsQuery.getArray("records");
var contactId = contactsRecords[0].Id;
if( contactsRecords.length > 0 & "{!Leadevents__c.Kontakt__c}" == "") {
alert("Kontakt existiert bereits und wird dem Leadevent zugeordnet.");
var le = new sforce.SObject("Leadevents__c");
le.id = "{!Leadevents__c.Id}";
le.Kontakt__c = contactsRecords[0].Id;
var result0 = sforce.connection.update([le]);
if(result0[0].getBoolean("success"))
location.reload(true);
}