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
Robert ZentgrafRobert Zentgraf 

Javascript to create and update record

Hi,

I created a button with JS-code. In this code, I create a new record (custom object A__c) and update the currect record (object Contact). On both objects I have a look-up-field to the other object. On the new record (object A__c), I can create the connection to the current record, in the current record (object Contact), I can't assign the new record (object A__c).

Can you please help me?

Regards
Robert
Shashikant SharmaShashikant Sharma
Could you please answer to following :

1. share your code, it will help to resolve your problem 
2. which object you have created this Button
3. relationship between A__c and Contact
Robert ZentgrafRobert Zentgraf
Hi Shashikant,

here the code:
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

var B_Contact = new sforce.SObject("Contact"); 
var B_A__c = new sforce.SObject("A__c"); 

B_Contact.Id = "{!Contact.Id}"; 
B_Contact.Sourcing__c = "DONE" 

B_A__c.Contact_Active_Sourcing__c = "{!Contact.Id}"; 
result = sforce.connection.create([B_A__c]); 

var Id = sforce.connection.query("SELECT Id FROM A__c WHERE Contact_Active_Sourcing__c = '{!Contact.Id}'"); 
B_Contact.LookUpTo_A__c = Id; 
result = sforce.connection.update([B_Contact]); 

Regards
Robert
Shashikant SharmaShashikant Sharma
One suggestion in the code

Replace var Id to var contactId

So your code will be


var contactId= sforce.connection.query("SELECT Id FROM A__c WHERE Contact_Active_Sourcing__c = '{!Contact.Id}'"); 
B_Contact.LookUpTo_A__c = contactId; 


Let me know the error message if it still does not work.