+ Start a Discussion
Josh Reigner 2Josh Reigner 2 

Button to create a new object record and attach child as related record

Hi Community,
I am not a developer and am just trying to make, what I thought, was a quick update to an existing button.

The button creates a Project (Custom Object) from a Request (Custom Object).  I need to be able to relate the Request to the newly created Project.  This is the code that creates the project, but I need to then call the ID of the newly created Project and insert it into the Project Lookup Field on the Request so the Request becomes a record of the related list on the Project.  Does that make sense?  Is this even possible?

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

var Proj = new sforce.SObject("SFDC_Project__c"); 

Proj.SFDC_Project_Name__c = '{!SFDC_Request__c.Name}' 
Proj.SFDC_Project_Description__c = '{!SFDC_Request__c.Request_Detail__c}' 
Proj.Users_Affected__c = '{!SFDC_Request__c.Users_Affected__c}' 
Proj.Segment_specific_or_Veritiv__c = '{!SFDC_Request__c.Segment_Specific_or_Veritiv__c}' 
Proj.Urgency__c = '{!SFDC_Request__c.request_priority__c}' 
Proj.Recommended_Solution__c = '{!SFDC_Request__c.Requirements__c}' 

// {!SFDC_Request__c.SFDC_Project__c} = {!SFDC_Project__c.Id} 

var result = sforce.connection.create([Proj]) 

if(result[0].success=='true'){ 
alert('Project ' + Proj.SFDC_Project_Name__c + ' successfully created'); 
}else{ 
alert('Could not create record ' + result); 
}
 
Best Answer chosen by Josh Reigner 2
Lalit Mistry 21Lalit Mistry 21
Hi Josh,

Below script should work for you
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

var Proj = new sforce.SObject("SFDC_Project__c"); 

Proj.SFDC_Project_Name__c = '{!SFDC_Request__c.Name}' 
Proj.SFDC_Project_Description__c = '{!SFDC_Request__c.Request_Detail__c}' 
Proj.Users_Affected__c = '{!SFDC_Request__c.Users_Affected__c}' 
Proj.Segment_specific_or_Veritiv__c = '{!SFDC_Request__c.Segment_Specific_or_Veritiv__c}' 
Proj.Urgency__c = '{!SFDC_Request__c.request_priority__c}' 
Proj.Recommended_Solution__c = '{!SFDC_Request__c.Requirements__c}' 

var result = sforce.connection.create([Proj]) 

if(result[0].success=='true'){ 
    var requestResult = sforce.connection.query("Select Id, SFDC_Project__c from SFDC_Request__c WHERE Id ='" + '{!SFDC_Request__c.Id}' + "'", {
      onSuccess : function(queryResult){
            var it = new sforce.QueryResultIterator(queryResult);
            while(it.hasNext()){
                var record = it.next();
                record.SFDC_Project__c = result[0].id;
                var updateResult = sforce.connection.update([record]);
                if(updateResult[0].success =='true'){
                    alert('Request updated successfully.');
                } else {
                    alert('Failed update request');
                }
            }
      },
      onFailure : function(queryResult){
        alert('Failed to retrieve request');
      }});
}else{ 
    alert('Could not create record ' + result); 
}

Mark this as an answer if that works well for others to benefit.

All Answers

Lalit Mistry 21Lalit Mistry 21
Hi Josh,

Below script should work for you
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

var Proj = new sforce.SObject("SFDC_Project__c"); 

Proj.SFDC_Project_Name__c = '{!SFDC_Request__c.Name}' 
Proj.SFDC_Project_Description__c = '{!SFDC_Request__c.Request_Detail__c}' 
Proj.Users_Affected__c = '{!SFDC_Request__c.Users_Affected__c}' 
Proj.Segment_specific_or_Veritiv__c = '{!SFDC_Request__c.Segment_Specific_or_Veritiv__c}' 
Proj.Urgency__c = '{!SFDC_Request__c.request_priority__c}' 
Proj.Recommended_Solution__c = '{!SFDC_Request__c.Requirements__c}' 

var result = sforce.connection.create([Proj]) 

if(result[0].success=='true'){ 
    var requestResult = sforce.connection.query("Select Id, SFDC_Project__c from SFDC_Request__c WHERE Id ='" + '{!SFDC_Request__c.Id}' + "'", {
      onSuccess : function(queryResult){
            var it = new sforce.QueryResultIterator(queryResult);
            while(it.hasNext()){
                var record = it.next();
                record.SFDC_Project__c = result[0].id;
                var updateResult = sforce.connection.update([record]);
                if(updateResult[0].success =='true'){
                    alert('Request updated successfully.');
                } else {
                    alert('Failed update request');
                }
            }
      },
      onFailure : function(queryResult){
        alert('Failed to retrieve request');
      }});
}else{ 
    alert('Could not create record ' + result); 
}

Mark this as an answer if that works well for others to benefit.
This was selected as the best answer
Josh Reigner 2Josh Reigner 2
Hi Lalit!  This worked perfectly!!  Thank you so much!