+ Start a Discussion
Steve HuseSteve Huse 

Javascript to create new case?

Hi All,

 

I'm hoping that someone can help me with this?

 

I want to create a custom button in Opps that creates a new case, providing the Opp is 'closed won'.

If the Opp is not 'closed won', then I need it to alert "Opp must be won to create case".

 

I've created a button using 'Content Source = URL' which creates a new case and populates the new case fields with data from the related Opp fields (see code below), but now that I want this to be conditional on the related Opp being closed won, it seems I need to make the button OnClick Javascript and use some Javascript code.

 

/500/e?retURL=%2F500%2Fo
&cas4_lkid={!Account.Id}
&cas4={!Account.Name}
&CF00N30000009y6pH_lkid={!Opportunity.Id}
&CF00N30000009y6pH={!Opportunity.Name}
&cas5={!Opportunity.Type}
&cas14={!Opportunity.Name}
&retURL=/{!Opportunity.Id}
&saveURL=/{!Opportunity.Id}

 

I'm guessing the Javascript would need to be structured like this:

 

var status = "{!Opportunity.StageName}";
if (status == "Closed Won")
{
SCRIPT TO CREATE CASE AND POPULATE CASE FIELDS WITH DATA FROM RELATE OPP FIELDS

}
else
{
alert ('Opportunity needs to be won before a case can be created.');
}

 

Can anyone give me any help here?

 

Thanks in advance.

 

Steve

 

Nerd WhispererNerd Whisperer

Hey Steve,

 

Here is the code you can add into your javascript button to do what you requested.  It worked for me, but let me know if you have any problems getting it to work for yourself.

 

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
var status = "{!Opportunity.StageName}";
var newRecords = [];

if (status == "Closed Won")
{
   var c = new sforce.SObject("Case");
   c.AccountId = "{!Opportunity.AccountId}";
   //add any other related fields you like

   newRecords.push(c);

   result = sforce.connection.create(newRecords); 
}
else
{
   alert ('Opportunity needs to be won before a case can be created.');
}

 

--------------------------------------------------------------

Need Help? Call 1-888-407-9578
or visit http://www.salesforcesuperheroes.com/

Steve HuseSteve Huse

Thanks N.W ...

 

... but I'm struggling!

 

Your code works fine ... it creates a case and automatically populates the account name.

 

When I try to add other fields, the new case is no longer created.  For example, the following code no longer creates a case:

 

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
var status = "{!Opportunity.StageName}";
var newRecords = [];

if (status == "Closed Won")
{
   var c = new sforce.SObject("Case");
   c.AccountId = "{!Opportunity.AccountId}";
   c.Contact = "{!Opportunity.Opportunity_Contact__c}";    <----- ANOTHER OPP FIELD THAT I WANT POPULATED IN THE CASE
   //add any other related fields you like

 

   newRecords.push(c);

   result = sforce.connection.create(newRecords);
}
else
{
   alert ('Opportunity must be won before a case can be created.');
}

 

 

Do you know why this is?

 

Many thanks

 

Steve

Nerd WhispererNerd Whisperer

I see what the problem is. You need to use the contact's ID.

 

So change this statement:

 

c.Contact = "{!Opportunity.Opportunity_Contact__c}";  

 To this:

c.ContactId = "{!Opportunity.ContactId__c}"; 
 
Salesforce Superheroes
------------------------------