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
Janno RipJanno Rip 

Condition inside custom JavaScript Button that creates a contact

Hello everyone,

i have a custom javascript button ("Kontakt anlegen und zuordnen), that allows me to create a new contact in an account while I am on my custom object and refer that newly created contact to that custom object:

Here is the button:

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

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); 
    } 

else{ 
    alert('Es stehen keine Informationen zur Anlage eines neuen Kontaktes zur Verfügung.'); 
    }


Here is my custom object:

User-added image
Now whenever I hit "Kontakt anlegen und zuordnen" the button creates a new contact in the connected account and links it to the custom object depenending on the information inside the field "Ansprechpartner". If the "salutation" or "lastname" are missing it alerts "Not enough information ti create a new contact". 

Now the problem I have is the following: The button executes everytime I hit it. So I can create the same contact over and over again. What I need is a condition that goes like this: When the information inside the field "Ansprechpartner" is the same as in "Kontakt" (maybe I would refer to it in another text formula to make it comparable) then do not create a new contact but alert sth like: "Contact already exists in that account."

I am not sure where to put my condition :/ Hope problem is clear and someone with more experience can help out.

Thanks in advance
Best Answer chosen by Janno Rip
Adilson Arcoverde JrAdilson Arcoverde Jr
Hi Janno,

First of all, I'm sorry for my poor german. I'll try to answer you with the information I undestood.

Well, before you create a new record, you have to query contacts that matches with your condition. If the query results one or more records, then an alert should be displayed. Here some code that will help you (include before the code you create a new record):
var contactsQuery = sforce.connection.query("Select Id from Contact where AccountId="{!Account.Id}" and YOUR_CONTACT_FIELD = "{!YOUR_ACCOUNT_FIELD}";

var contactsRecords = contactsQuery.getArray("records");

if( contactsRecords.length > 0 ) {
   alert("Contact already exists in that account";
} else {
   // ADD YOUR RECORD CREATION CODE HERE
}

Hope I've helped in some way.

Regards.
 

All Answers

Adilson Arcoverde JrAdilson Arcoverde Jr
Hi Janno,

First of all, I'm sorry for my poor german. I'll try to answer you with the information I undestood.

Well, before you create a new record, you have to query contacts that matches with your condition. If the query results one or more records, then an alert should be displayed. Here some code that will help you (include before the code you create a new record):
var contactsQuery = sforce.connection.query("Select Id from Contact where AccountId="{!Account.Id}" and YOUR_CONTACT_FIELD = "{!YOUR_ACCOUNT_FIELD}";

var contactsRecords = contactsQuery.getArray("records");

if( contactsRecords.length > 0 ) {
   alert("Contact already exists in that account";
} else {
   // ADD YOUR RECORD CREATION CODE HERE
}

Hope I've helped in some way.

Regards.
 
This was selected as the best answer
Janno RipJanno Rip

Hi Adilson,

thanks for your help. Sorry I couldn't response earlier. That was exactly what I was looking for. My final code looks (for now) like this:

{!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 LastName = '{!Leadevents__c.Nachname_JRi__c}'");

var contactsRecords = contactsQuery.getArray("records");

if( contactsRecords.length > 0 ) {
   alert("Kontakt existiert bereits im Account. Bitte mit Leadevent verknüpfen");
} else {

alert("Neuanlage");
}
For the moment I'm fine with 2 alerts. Now I just need to figure out how to implement my 2nd part into this one. It has some conditions on its own as well that need to be checked before creating the new contact.
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 existiert bereits im verknüpften Account.");
}

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); 

}
	}