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
ssurferssurfer 

What's wrong with this code?

The purpose of the following code is to copy a few values of the fields of an opportunity, create a linked custom object and populate a few fields with these values. I've seen that several people have problems with this type of code, so i hope that this will be of value to many. I've tried doing this coding using dynabean too, but from what I understand this only works for the first step, copying values from Opportunity. It breaks down before the new object is created.

I'm very grateful for any help i can get.
Best regards,
ssurfer

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<script src="/soap/ajax/8.0/connection.js" type="text/javascript"></script>
<script language="javascript">

function setupPage() {
createWork_Order();

parent.frames.location.replace("/"+Work_Order__c+"/e");
}



function createWork_Order() {

var opId = "{!Opportunity.Id}";
var opRecType = "{!Opportunity.RecordType}";
alert("Opportunity Id= " + opId + "\n Opportunity Record Type= " + opRecType );
var opPackDate = "{! Opportunity.Pack_Date_1__c }";
var work_order = new sforce.SObject("Work_Order__c");
Work_Order.RecordType = opRecType;
Work_Order.opportunity.ID=opID;
Work_Order.Name = "FILL IN WORKORDER NAME";
Work_Order.Pack_Date=opPackDate;
result = sforce.connection.create([Work_Order__c]);

if (result[0].getBoolean("success")) {
log("new Work Order created with id " + result[0].id);
} else {
log("failed to create Work Order " + result[0]);
}
}

</script>
</head>
<body onload="setupPage();"></body>
</html>

ssurferssurfer

... having modified, and hopefully improved the following code i get this error message from FireBug:

 

syntax error
onload(load )servlet.Integrati... (line 1)
var opId = "00620000002lKEu";\n

Below is the new code. I'm very grateful for any insight in what i'm doing wrong. I'm sitting with a lot of books, but noone to ask...

Best regards,
ssurfer

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<script src="/soap/ajax/8.0/connection.js" type="text/javascript"></script>
<script language="javascript">

 <script>
    function setupPage() {

 

 var opId = "{!Opportunity.Id}";
 var opRecType = "{!Opportunity.RecordType}";
 createWork_Order();
 parent.frames.location.replace("/"+Work_Order+"/e");
}
 
 
 
 function createWork_Order() {

{!Work_Order__c.RecordTypeId}

 //alert("Opportunity Id= " + opId + " Opportunity Record Type= " + opRecType );
 //var opPackDate = "{! Opportunity.Pack_Date_1__c }";
 var wo__c = new sforce.SObject("Work_Order__c");
 //wo__c.RecordTypeID = opRecType;
 //set wo__c.opportunity.Id=opId;
 //wo__c.Name = "FILL IN WORKORDER NAME";
 //wo__c.Pack_Date=opPackDate;
 result = sforce.connection.create([wo__c]);

  if (result[0].getBoolean("success")) {
    log("new Work Order created with id " + result[0].id);
  } else {
    log("failed to create Work Order " + result[0]);
  }

}

</script>
</head>
<body onload="setupPage();"></body>
</html>

cheenathcheenath
It is not clear what is going worng from the error message.
Try this code, it should give you better error message:

function createWork_Order() {

//not sure what line below is for?

//{!Work_Order__c.RecordTypeId}

 //alert("Opportunity Id= " + opId + " Opportunity Record Type= " + opRecType );
 //var opPackDate = "{! Opportunity.Pack_Date_1__c }";
 //wo__c.RecordTypeID = opRecType;
 //set wo__c.opportunity.Id=opId;
 //wo__c.Name = "FILL IN WORKORDER NAME";
 //wo__c.Pack_Date=opPackDate;


 var wo__c = new sforce.SObject("Work_Order__c");

try {

 result = sforce.connection.create([wo__c]);

}catch(error) {

alert("got an error: " + error);

}

  if (result[0].getBoolean("success")) {
    alert("new Work Order created with id " + result[0].id);
  } else {
    alert("failed to create Work Order " + result[0]);
  }

}


ssurferssurfer
 
Thank you very much for your fast reply Cheenath,
 
I now get hte following errormessage from the Consol in Firebug:

missing } in XML expression

onload(load )servlet.Integrati... (line 1)
createWork_Order();\n
 
In short i want the s-control to create a custom object called "Work Order" and populate it with a few values from Opportunity. I then want the window with Work Order to remain open in edit mode. To make things complicated, Work Order has 3 different record types and i assume i have to specify the correct record type when the object is being created? As you've doubtlessly noticed i'm in a bit above my head, but I have to make this work somehow.
 
Question:
Looking at my code, could you be able to show me just how to make it open the custom object Work Order, using an s-control? Then i could try working on getting the values from opportunity and populate it.
 
My major problem is that I can't seem to do this in small enough steps to see if i'm on the right way. I've gone through your website and done the java exercises but I just doesn't grasp the logic of how to apply it to salesforce. I'm very grateful for any insight you can give me.
 
 
Here's the code i'm using
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="/soap/ajax/8.0/connection.js" type="text/javascript"></script>
<script language="javascript">
 <script>
    function setupPage() {
 
 
 createWork_Order();
 parent.frames.location.replace("/"+Work_Order__c+"/e");
}
 
 
 
 function createWork_Order() {

//{!Work_Order__c.RecordTypeId}
 //alert("Opportunity Id= " + opId + " Opportunity Record Type= " + opRecType );
 //var opPackDate = "{! Opportunity.Pack_Date_1__c }";
 //wo__c.RecordTypeID = opRecType;
 //set wo__c.opportunity.Id=opId;
 //wo__c.Name = "FILL IN WORKORDER NAME";
 //wo__c.Pack_Date=opPackDate;
 

 var wo__c = new sforce.SObject("Work_Order__c");

try {

 result = sforce.connection.create([wo__c]);
}catch(error) {
alert("got an error: " + error);
}

  if (result[0].getBoolean("success")) {
    alert("new Work Order created with id " + result[0].id);
  } else {
    alert("failed to create Work Order " + result[0]);
  }
}
 
</script>
</head>
<body onload="setupPage();"></body>
</html>
cheenathcheenath
Try this, you had a extra <script> tag:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="/soap/ajax/8.0/connection.js" type="text/javascript"></script>
<script>

function setupPage() {
 createWork_Order();
 parent.frames.location.replace("/"+Work_Order__c+"/e");
}
 
 
 function createWork_Order() {

//{!Work_Order__c.RecordTypeId}
 //alert("Opportunity Id= " + opId + " Opportunity Record Type= " + opRecType );
 //var opPackDate = "{! Opportunity.Pack_Date_1__c }";
 //wo__c.RecordTypeID = opRecType;
 //set wo__c.opportunity.Id=opId;
 //wo__c.Name = "FILL IN WORKORDER NAME";
 //wo__c.Pack_Date=opPackDate;
 

 var wo__c = new sforce.SObject("Work_Order__c");

try {

 result = sforce.connection.create([wo__c]);
}catch(error) {
alert("got an error: " + error);
}

  if (result[0].getBoolean("success")) {
    alert("new Work Order created with id " + result[0].id);
  } else {
    alert("failed to create Work Order " + result[0]);
  }
}
 
</script>
</head>
<body onload="setupPage();"></body>
</html>

 

ssurferssurfer
Thank you Cheenath,

Here's the first batch of code that will open up the custom object, all i have to do now is add more variables to populate more fields. Hope this can help others speed up their learning of s-controls.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="/soap/ajax/8.0/connection.js" type="text/javascript"></script>
<script>
var opId="{!Opportunity.Id}";
var Work_Order__c; // added global, will become the new object id

function setupPage() {
 createWork_Order();
 parent.frames.location.replace("/"+Work_Order__c+"/e");
}
 
 
 function createWork_Order() {

var wo__c = new sforce.SObject("Work_Order__c");
//alert("Opportunity Id= " + opId + " Opportunity Record Type= " + opRecType );
//var opPackDate = "{! Opportunity.Pack_Date_1__c }";
//wo__c.RecordTypeID = opRecType;
//var wo__c.Opportunity.Id=opId;
    wo__c.Opportunity__c = opId; //  added
   
try {

 result = sforce.connection.create([wo__c]);
}catch(error) {
alert("got an error: " + error);
}

  if (result[0].getBoolean("success")) {
    // alert("new Work Order created with id " + result[0].id);
    Work_Order__c = result[0].id; // added
   
  } else {
    alert("failed to create Work Order " + result[0]);
  }
 
}
 
</script>
</head>
<body onload="setupPage();"></body>
</html>

 


cheenath wrote:
Try this, you had a extra <script> tag:

Code:
<script src="/soap/ajax/8.0/connection.js" type="text/javascript"></script> <script> function setupPage() { createWork_Order(); parent.frames.location.replace("/"+Work_Order__c+"/e"); } function createWork_Order() { //{!Work_Order__c.RecordTypeId} //alert("Opportunity Id= " + opId + " Opportunity Record Type= " + opRecType ); //var opPackDate = "{! Opportunity.Pack_Date_1__c }"; //wo__c.RecordTypeID = opRecType; //set wo__c.opportunity.Id=opId; //wo__c.Name = "FILL IN WORKORDER NAME"; //wo__c.Pack_Date=opPackDate; var wo__c = new sforce.SObject("Work_Order__c"); try { result = sforce.connection.create([wo__c]); }catch(error) { alert("got an error: " + error); } if (result[0].getBoolean("success")) { alert("new Work Order created with id " + result[0].id); } else { alert("failed to create Work Order " + result[0]); } } </script>