1. Accueil
  2. /
  3. Guide Développeur
  4. /
  5. Librairie JavaScript
  6. /
  7. Sample Code Example

Sample Code Example

This example will show the JavaScript code of a custom button that converts a Lead to a Contact/Account based on the following scenario:

If the Company field is empty then display an alert: “The field Company should not be empty if you want to convert this lead”If the lead Company field is empty then display an alert: “The field Company should not be empty if you want to convert this lead”If the lead email field is empty then display an alert: “The field Email should not be empty if you want to convert this lead”ElseIf the account already exists (based on the name)Alert(“This account already exists in the database, do you want to overwrite theexisting account data?”)Yes: update the accountNo: do not update the accountElseInsert AccountIf the contact already exists (based on the email)Alert(“This contact already exists in the database, do you want to overwrite the existing contact data?”)Yes: update the contactNo: do not update the contactElseInsert contactSet the lead status to “Closed – Converted”Link all the related Invitees, Tasks, Opportunities and Campaign Members to the Contact.

<script type="text/javascript">

function ConvertLead() {

// Get lead infovar lead = GetDataByID(GetRecordGUID(), "Lead", "Id, Name, Revenue, Last_Call_Made_On, City, Company, Company_Number, Country, Email, Email_Read, First_Name, Industry, Last_Email_Sent_On, Last_Name, Lead_Origin, Mobile, No_of_Employees, Rating, Skype, Status, Street, Title, Website, Phone, ZIP, Picture, Rating, Twitter, Salutation, Department,  Secondary_Email, Description, Email_Opt_Out, Do_Not_Call, Fax, Middle_Name, OwnerId");

// Check lead company if emptyif (lead.Company == null) {

alert(GetRecordGUID() + " - The field Company should not be empty if you want to convert this lead.");

return;

        }

 

// Check lead email if emptyif (lead.Email == null) {

alert(GetRecordGUID() + " - The field Email should not be empty if you want to convert this lead.");

               return;

               }

// Prepare the Account Datavar xmlStrAccount = "<Data>";xmlStrAccount += "<Account>";xmlStrAccount += "<Name>";xmlStrAccount += htmlDecode(lead.Company);xmlStrAccount += "</Name>";xmlStrAccount += "<Company_Number>";xmlStrAccount += htmlDecode(lead.Company_Number);xmlStrAccount += "</Company_Number>";if (lead.Industry != null) {

xmlStrAccount += "<Industry>";xmlStrAccount += htmlDecode(lead.Industry);xmlStrAccount += "</Industry>";

       }

if (lead.Phone != null) {

xmlStrAccount += "<Phone>";xmlStrAccount += lead.Phone;xmlStrAccount += "</Phone>";

}

if (lead.Fax != null) {

xmlStrAccount += "<Fax>";xmlStrAccount += lead.Fax;xmlStrAccount += "</Fax>";

              }

if (lead.No_of_Employees != null) {

xmlStrAccount += "<No_of_Employees>";xmlStrAccount += lead.No_of_Employees;xmlStrAccount += "</No_of_Employees>";

              }

if (lead.Email != null) {

xmlStrAccount += "<Email>";xmlStrAccount += lead.Email;xmlStrAccount += "</Email>";

              }

if (lead.Revenue != null) {

xmlStrAccount += "<Revenue>";xmlStrAccount += lead.Revenue;xmlStrAccount += "</Revenue>";

              }

if (lead.Website != null) {

xmlStrAccount += "<Website>";xmlStrAccount += lead.Website;xmlStrAccount += "</Website>";

}

if (lead.Description != null) {

xmlStrAccount += "<Description>";xmlStrAccount += htmlDecode(lead.Description);xmlStrAccount += "</Description>";

}

if (lead.Street != null) {

xmlStrAccount += "<Billing_Street>";xmlStrAccount += htmlDecode(lead.Street);xmlStrAccount += "</Billing_Street>";

}

if (lead.Country != null) {

xmlStrAccount += "<Billing_Country>";xmlStrAccount += htmlDecode(lead.Country);xmlStrAccount += "</Billing_Country>";

}

if (lead.City != null) {

xmlStrAccount += "<Billing_City>";xmlStrAccount += htmlDecode(lead.City);xmlStrAccount += "</Billing_City>";

}

if (lead.ZIP != null) {

xmlStrAccount += "<Billing_ZIP>";xmlStrAccount += lead.ZIP;xmlStrAccount += "</Billing_ZIP>";

}

if (lead.Lead_Origin != null) {

xmlStrAccount += "<Lead_Origin>";xmlStrAccount += htmlDecode(lead.Lead_Origin);xmlStrAccount += "</Lead_Origin>";

}

if (lead.Rating != null) {

xmlStrAccount += "<Rating>";xmlStrAccount += htmlDecode(lead.Rating);xmlStrAccount += "</Rating>";

}

xmlStrAccount += "<OwnerId>";xmlStrAccount += GetOwner(lead.OwnerId);xmlStrAccount += "</OwnerId>";

var strname = "";var strfirstname = "";var strlastname = "";if (lead.First_Name != null) {

strname = htmlDecode(lead.First_Name);strfirstname = htmlDecode(lead.First_Name);

}

else {

if (lead.Last_Name != null) {

strfirstname = htmlDecode(lead.Last_Name);

}

}

if (lead.Last_Name != null) {

strname = strname + ' ' + htmlDecode(lead.Last_Name);strlastname = htmlDecode(lead.Last_Name);

}

else {

strlastname = htmlDecode(lead.Name);

}

if (strname == "") {

strname = htmlDecode(lead.Name);

}

if (strfirstname == "") {

                strfirstname = htmlDecode(lead.Name);

       }

// Prepare the Contact Datavar xmlStrContact = "<Data>";xmlStrContact += "<Contact>";xmlStrContact += "<Name>";xmlStrContact += strname;xmlStrContact += "</Name>";xmlStrContact += "<First_Name>";xmlStrContact += strfirstname;xmlStrContact += "</First_Name>";xmlStrContact += "<Last_Name>";xmlStrContact += strlastname;xmlStrContact += "</Last_Name>";

if (lead.Title != null) {

xmlStrContact += "<Title>";xmlStrContact += htmlDecode(lead.Title);xmlStrContact += "</Title>";

       }

if (lead.Mobile != null) {

xmlStrContact += "<Mobile>";xmlStrContact += lead.Mobile;xmlStrContact += "</Mobile>";

}

if (lead.Middle_Name != null) {

xmlStrContact += "<Middle_Name>";xmlStrContact += htmlDecode(lead.Middle_Name);xmlStrContact += "</Middle_Name>";

              }

              if (lead.Skype != null) {

xmlStrContact += "<Skype>";xmlStrContact += htmlDecode(lead.Skype);xmlStrContact += "</Skype>";

              }

              if (lead.Email != null) {

xmlStrContact += "<Email>";xmlStrContact += lead.Email;xmlStrContact += "</Email>";

              }

              if (lead.Phone != null) {

xmlStrContact += "<Work_Phone>";xmlStrContact += lead.Phone;xmlStrContact += "</Work_Phone>";

}

if (lead.Fax != null) {

xmlStrContact += "<Fax>";xmlStrContact += lead.Fax;xmlStrContact += "</Fax>";

              }

              if (lead.Street != null) {

xmlStrContact += "<Street>";xmlStrContact += htmlDecode(lead.Street);xmlStrContact += "</Street>";

              }

              if (lead.Country != null) {

xmlStrContact += "<Country>";xmlStrContact += htmlDecode(lead.Country);xmlStrContact += "</Country>";

              }

              if (lead.City != null) {

xmlStrContact += "<City>";xmlStrContact += htmlDecode(lead.City);xmlStrContact += "</City>";

              }

              if (lead.ZIP != null) {

xmlStrContact += "<ZIP>";xmlStrContact += lead.ZIP;xmlStrContact += "</ZIP>";

             }

             if (lead.Last_Email_Sent_On != null) {

xmlStrContact += "<Last_Email_Sent_On>";xmlStrContact += lead.Last_Email_Sent_On; // DatexmlStrContact += "</Last_Email_Sent_On>";

             }

            if (lead.Email_Read != null) {

xmlStrContact += "<Email_Read>";xmlStrContact += lead.Email_Read;xmlStrContact += "</Email_Read>";

}

if (lead.Do_Not_Call != null) {

xmlStrContact += "<Do_Not_Call>";xmlStrContact += lead.Do_Not_Call;xmlStrContact += "</Do_Not_Call>";

}

if (lead.Email_Opt_Out != null) {

xmlStrContact += "<Email_Opt_Out>";xmlStrContact += lead.Email_Opt_Out;xmlStrContact += "</Email_Opt_Out>";

             }

                    if (lead.Lead_Origin != null) {

xmlStrContact += "<Lead_Origin>";xmlStrContact += htmlDecode(lead.Lead_Origin);xmlStrContact += "</Lead_Origin>";

                     }

      if (lead.Rating != null) {

xmlStrContact += "<Rating>";xmlStrContact += htmlDecode(lead.Rating);xmlStrContact += "</Rating>";

             }

                    if (lead.Picture != null) {

xmlStrContact += "<Picture>";xmlStrContact += lead.Picture;xmlStrContact += "</Picture>";

            }

                   if (lead.Twitter != null) {

xmlStrContact += "<Twitter>";xmlStrContact += htmlDecode(lead.Twitter);xmlStrContact += "</Twitter>";

            }

            if (lead.Salutation != null) {

xmlStrContact += "<Salutation>";xmlStrContact += htmlDecode(lead.Salutation);xmlStrContact += "</Salutation>";

                   }

    if (lead.Department != null) {

xmlStrContact += "<Department>";xmlStrContact += htmlDecode(lead.Department);xmlStrContact += "</Department>";

}

if (lead.Secondary_Email != null) {

xmlStrContact += "<Secondary_Email>";xmlStrContact += lead.Secondary_Email;xmlStrContact += "</Secondary_Email>";}

if (lead.Description != null) {

xmlStrContact += "<Description>";xmlStrContact += htmlDecode(lead.Description);xmlStrContact += "</Description>";

                   }

    if (lead.Last_Call_Made_On != null) {

xmlStrContact += "<Last_Call_Made_On>";xmlStrContact += lead.Last_Call_Made_On; // DatexmlStrContact += "</Last_Call_Made_On>";

                   }

xmlStrContact += "<OwnerId>";xmlStrContact += GetOwner(lead.OwnerId);xmlStrContact += "</OwnerId>";

             var AccountGUID = "";

// Check if Account exists based on Name valuevar Account = GetDataByColumn("Name", lead.Company, "Account", "Id, Name");if (Account != null) {

// Set the Account GUIDAccountGUID = Account.Id;if (confirm("This account already exists in the database, do you want to overwrite the existing account data?")) {

xmlStrAccount += "</Account>";xmlStrAccount += "</Data>";try {

// Update AccountUpdateRecord("Account", "Name", xmlStrAccount);//if (!success) {// return;//}

}

catch (ex) {

alert(ex.message);return;

}

}

else {

}

}

else {

// Add the Remaining XML dataxmlStrAccount += "<IsDeleted>";xmlStrAccount += 0;xmlStrAccount += "</IsDeleted>";xmlStrAccount += "</Account>";xmlStrAccount += "</Data>";try {

// Insert Accountvar ObjResAccount = CreateRecord("Account", "Name", xmlStrAccount);AccountGUID = ObjResAccount.RecordGUID;if (AccountGUID == null) {

return;

}

}

catch (ex) {

alert(ex.message);return;

}

}

// Check if Contact exists based on Email valuevar ContactGUID = "";var Contact = GetDataByColumn("Email", lead.Email, "Contact", "Id");if (Contact != null) {

// Set the Contact GUIDContactGUID = Contact.Id;if (confirm("This contact already exists in the database, do you want to overwrite the existing contact data?")) {

// Add Contact Id to the XML dataxmlStrContact += "<Id>";xmlStrContact += Contact.Id;xmlStrContact += "</Id>";xmlStrContact += "<Account>";xmlStrContact += htmlDecode(lead.Company);xmlStrContact += "</Account>";xmlStrContact += "</Contact>";xmlStrContact += "</Data>";try {

// Update ContactUpdateRecord("Contact", "Email", xmlStrContact);

}

catch (ex) {

alert(ex.message);return;

}

}

else {

}

}

else {

// Add the Remaining XML dataxmlStrContact += "<Account>";xmlStrContact += htmlDecode(lead.Company);xmlStrContact += "</Account>";xmlStrContact += "<IsDeleted>";xmlStrContact += 0;xmlStrContact += "</IsDeleted>";xmlStrContact += "</Contact>";xmlStrContact += "</Data>";try {

// Insert Contactvar ObjResContact = CreateRecord("Contact", "Email", xmlStrContact);ContactGUID = ObjResContact.RecordGUID;

}

catch (ex) {

alert(ex.message);return;

}

}

// ---------- Update lead status to Converted ---------- //

// Prepare the Lead Datavar xmlStrLead = "<Data>";xmlStrLead += "<Lead>";xmlStrLead += "<Id>";xmlStrLead += GetRecordGUID();xmlStrLead += "</Id>";xmlStrLead += "<Status>";xmlStrLead += "Closed_Converted";xmlStrLead += "</Status>";xmlStrLead += "</Lead>";xmlStrLead += "</Data>";try {

// Update LeadUpdateRecord("Lead", "Id", xmlStrLead);

// Refresh the pagelocation.reload();

}

catch (ex) {

alert(ex.message);return;

}

// Get All the related Inviteevar Invitee = GetDataByColumn("Lead", GetRecordGUID(), "Invitee", "Id");if (Invitee != null) {

if (Invitee.length != undefined) {

for (var i = 0; i < Invitee.length; i++) {

// Prepare the Invitee Data and link it to the contactvar xmlStrInvitee = "<Data>";xmlStrInvitee += "<Invitee>";xmlStrInvitee += "<Id>";xmlStrInvitee += Invitee[i].Id;xmlStrInvitee += "</Id>";xmlStrInvitee += "<Contact>";xmlStrInvitee += lead.Email;xmlStrInvitee += "</Contact>";xmlStrInvitee += "</Invitee>";xmlStrInvitee += "</Data>";try {

// Update InviteeUpdateRecord("Invitee", "Id", xmlStrInvitee);

}

catch (ex) {

alert(ex.message);return;

}

}

}

else // one record

{

// Prepare the Invitee Data and link it to the contactvar xmlStrInvitee = "<Data>";xmlStrInvitee += "<Invitee>";xmlStrInvitee += "<Id>";xmlStrInvitee += Invitee.Id;xmlStrInvitee += "</Id>";xmlStrInvitee += "<Contact>";xmlStrInvitee += lead.Email;xmlStrInvitee += "</Contact>";xmlStrInvitee += "</Invitee>";xmlStrInvitee += "</Data>";try {

// Update InviteeUpdateRecord("Invitee", "Id", xmlStrInvitee);

}

catch (ex) {

alert(ex.message);return;

}

}

}

// Get All the related Tasksvar Tasks = GetDataByColumn("Lead", GetRecordGUID(), "Task", "Id");if (Tasks != null) {

if (Tasks.length != undefined) {

for (var i = 0; i < Tasks.length; i++) {

// Prepare the Task Data and link it to the contactvar xmlStrTask = "<Data>";xmlStrTask += "<Task>";xmlStrTask += "<Id>";xmlStrTask += Tasks[i].Id;xmlStrTask += "</Id>";xmlStrTask += "<Contact>";xmlStrTask += lead.Email;xmlStrTask += "</Contact>";xmlStrTask += "<Account>";xmlStrTask += htmlDecode(lead.Company);xmlStrTask += "</Account>";xmlStrTask += "</Task>";xmlStrTask += "</Data>";try {

// Update TaskUpdateRecord("Task", "Id", xmlStrTask);

}

catch (ex) {

alert(ex.message);return;

}

}

}

else // one record

{

// Prepare the Task Data and link it to the contactvar xmlStrTask = "<Data>";xmlStrTask += "<Task>";xmlStrTask += "<Id>";xmlStrTask += Tasks.Id;xmlStrTask += "</Id>";xmlStrTask += "<Contact>";xmlStrTask += lead.Email;xmlStrTask += "</Contact>";xmlStrTask += "<Account>";xmlStrTask += htmlDecode(lead.Company);xmlStrTask += "</Account>";xmlStrTask += "</Task>";xmlStrTask += "</Data>";try {

// Update TaskUpdateRecord("Task", "Id", xmlStrTask);

}

catch (ex) {

alert(ex.message);return;

}

}

}

// Get All the related Opportunitiesvar Opportunities = GetDataByColumn("Lead", GetRecordGUID(), "Opportunity", "Id");if (Opportunities != null) {

if (Opportunities.length != undefined) {

for (var i = 0; i < Opportunities.length; i++) {

// Prepare the Opportunity Data and link it to the contactvar xmlStrOpportunity = "<Data>";xmlStrOpportunity += "<Opportunity>";xmlStrOpportunity += "<Id>";xmlStrOpportunity += Opportunities[i].Id;xmlStrOpportunity += "</Id>";xmlStrOpportunity += "<Main_Contact>";xmlStrOpportunity += lead.Email;xmlStrOpportunity += "</Main_Contact>";xmlStrOpportunity += "<Account>";xmlStrOpportunity += htmlDecode(lead.Company);xmlStrOpportunity += "</Account>";xmlStrOpportunity += "</Opportunity>";xmlStrOpportunity += "</Data>";try {

// Update OpportunityUpdateRecord("Opportunity", "Id", xmlStrOpportunity);

}

catch (ex) {

alert(ex.message);return;

}

}

}

else // one record

{

// Prepare the Opportunity Data and link it to the contactvar xmlStrOpportunity = "<Data>";xmlStrOpportunity += "<Opportunity>";xmlStrOpportunity += "<Id>";xmlStrOpportunity += Opportunities.Id;xmlStrOpportunity += "</Id>";xmlStrOpportunity += "<Main_Contact>";xmlStrOpportunity += lead.Email;xmlStrOpportunity += "</Main_Contact>";xmlStrOpportunity += "<Account>";xmlStrOpportunity += htmlDecode(lead.Company);xmlStrOpportunity += "</Account>";xmlStrOpportunity += "</Opportunity>";xmlStrOpportunity += "</Data>";try {

// Update OpportunityUpdateRecord("Opportunity", "Id", xmlStrOpportunity );

}

catch (ex) {

alert(ex.message);return;

}

}

}

// Get All the related Campaign Membersvar CampaignMembers = GetDataByColumn("Lead", GetRecordGUID(), "Campaign_Member", "Id");if (CampaignMembers != null) {

if (CampaignMembers.length != undefined) {

for (var i = 0; i < CampaignMembers.length; i++) {

// Prepare the Campaign Member Data and link it to the contactvar xmlStrCampaignMember = "<Data>";xmlStrCampaignMember += "<Campaign_Member>";xmlStrCampaignMember += "<Id>";xmlStrCampaignMember += CampaignMembers[i].Id;xmlStrCampaignMember += "</Id>";xmlStrCampaignMember += "<Contact>";xmlStrCampaignMember += lead.Email;xmlStrCampaignMember += "</Contact>";xmlStrCampaignMember += "</Campaign_Member>";xmlStrCampaignMember += "</Data>";try {

// Update Campaign MemberUpdateRecord("Campaign_Member", "Id", xmlStrCampaignMember);

}

catch (ex) {

alert(ex.message);return;

}

}

}

else // one record

{

// Prepare the Campaign Member Data and link it to the contactvar xmlStrCampaignMember = "<Data>";xmlStrCampaignMember += "<Campaign_Member>";xmlStrCampaignMember += "<Id>";xmlStrCampaignMember += CampaignMembers.Id;xmlStrCampaignMember += "</Id>";xmlStrCampaignMember += "<Contact>";xmlStrCampaignMember += lead.Email;xmlStrCampaignMember += "</Contact>";xmlStrCampaignMember += "</Campaign_Member>";xmlStrCampaignMember += "</Data>";try {

// Update Campaign MemberUpdateRecord("Campaign_Member", "Id", xmlStrCampaignMember);

}

catch (ex) {

alert(ex.message);return;

}

}

}

}

</script>

Cet article vous a-t-il été utile ? Non Oui

Comment pouvons-nous aider ?