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 

update custom object after contact creation

Hello everyone,
on my custom object Leadevents__c I have create a custom button "New Contact" which creates the standard object "contact". For this I have all the data I need on my custom object: Name, AccountID etc.

On my custom object I have also a lookup to contacts.
User-added image
What I need is after the creation of a new contact (coming from my leadevents__c) that this contacts id gets passed into the related Leadevent and ends up as a look up value:

Here the result I would need:
User-added image
So I somehow need to update my Leadevents__c after a contact creation and figure out that the right contact is picked.
 

Does anyone have an idea to push me in the right direction? First thing I came up with (in theory) was creating a new contact via ProcessBuilder triggerd by a button on my leadevent, populate Leadevents__c ID on that contact and then "somehow" run a trigger that checks for contacts and Leadevents where the (Leadevents) - IDs match and then populate the contact id on my leadevent?

 

Best Answer chosen by Janno Rip
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi 

I think you can use Java script button to create contact and update your custom object.

Please chekc the below code:
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

var Con = new sforce.SObject("Contact"); 

//Add all mandatory fields here
Con.LastName =' Test Contact '; 


var result = sforce.connection.create([Con]); 

if(result[0].getBoolean("success")) 
{ 
alert('Contact Created successfully'+result[0].id); 
var p = new sforce.SObject("Candidate__c");
p.Id="{!Candidate__c.Id}";
p.Contact__c = result[0].id;
var result1 = sforce.connection.update([p]);
if(result1[0].getBoolean("success")) 
location.reload(true);
else
alert('Error : '+result1); 
} 
else{ 
alert('Error : '+result); 
}

Replace 'Candidate__c' with your custom object.This worked for me perfectly.Hope this will be helpful.
Thanks

All Answers

Bhargavi TunuguntlaBhargavi Tunuguntla
Hi 

I think you can use Java script button to create contact and update your custom object.

Please chekc the below code:
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

var Con = new sforce.SObject("Contact"); 

//Add all mandatory fields here
Con.LastName =' Test Contact '; 


var result = sforce.connection.create([Con]); 

if(result[0].getBoolean("success")) 
{ 
alert('Contact Created successfully'+result[0].id); 
var p = new sforce.SObject("Candidate__c");
p.Id="{!Candidate__c.Id}";
p.Contact__c = result[0].id;
var result1 = sforce.connection.update([p]);
if(result1[0].getBoolean("success")) 
location.reload(true);
else
alert('Error : '+result1); 
} 
else{ 
alert('Error : '+result); 
}

Replace 'Candidate__c' with your custom object.This worked for me perfectly.Hope this will be helpful.
Thanks
This was selected as the best answer
Janno RipJanno Rip
Thanks a lot Bhargavi,

it does exactly what I need it to do!