+ Start a Discussion
Stefanos.ThomaidisStefanos.Thomaidis 

Onclick Javascript button in a related object which is supposed to clone the first record.

2 issues.

 

1.  var a never gets the actual salesforce 15 digit id for the custom object "Location__c". How do I get this value?

2. When I build the query I ge the message "unexpected token".  How do I use a variable like "a" in a query statement such as below?:

 

 

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

 

var a = {!Location__c.Id}

var result = sforce.connection.query("SELECT Id FROM Promo_Material_Snapshot__c"+
" WHERE Location__c = a limit 1")

 

var records = result.getArray("records")

var IdsToClone = String(records[0]).substring(40, 55)

 

window.open("/"+IdsToClone +"/e?clone=1&retURL=/"+IdsToClone )

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

var a = {!Location__c.Id}

var query1='SELECT Id FROM Promo_Material_Snapshot__c WHERE Location__c =\''+ a+'\' limit 1';

var result =sforce.connection.query(query1);

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

Prad@SFDCPrad@SFDC

Hi,

 

Try the Below code as a reference:

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
var a="{!Location__c.Id}";
var strQuery="SELECT Id FROM Promo_Material_Snapshot__c where Location__c =\'"+ a + "\' limit 1";
var result = sforce.connection.query(strQuery);
var records = result.getArray("records");
var IdsToClone = String(records[0]).substring(21, 36);
window.open("/"+IdsToClone +"/e?clone=1&retURL=/"+IdsToClone );

 

If there is problem then let me know the problem.

Stefanos.ThomaidisStefanos.Thomaidis

Hi ,

 

Thank you for the immediate reply.

I had similar lines applied but it won't give the Salesforce session 15 digit Id.  It keeps giving a field ID__c , one of many custom fields I have introduced into this custom object.

Is there any chance we have an override of the standard session value with this one?

all the best,

Prad@SFDCPrad@SFDC

Hi,

What you need ? You need salesforce session id or Id get from the Object.

 

If you need session id then you can use {!GETSESSIONID()}

 

If need field id then use

 

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
var a="{!Location__c.Id}";
var strQuery="SELECT Id FROM Promo_Material_Snapshot__c where Location__c =\'"+ a + "\' limit 1";
var result = sforce.connection.query(strQuery);
var records = result.getArray("records");
var IdsToClone =records[0].Id;

 

Enter the field name what you need.If this is not the solution of your problem then let me know.

Stefanos.ThomaidisStefanos.Thomaidis

Hi,

 

I need the session Id.

The situation is the following:

I have a custom object Location__c.  For this, I have a related custom object Promo_Material_Snapshot__c.

What I want is to clone via a button in Location__c the first record of the relatede child object Promo_Material_Snapshot__c.

 

I have the following code on a button:

 

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

var a = {!GETSESSIONID()}

var result = sforce.connection.query("SELECT Id FROM Promo_Material_Snapshot__c"+
" WHERE Location_Name__c = '"+ a +"' limit 1")
alert(result)

var records = result.getArray("records")

var IdsToClone = String(records[0]).substring(40, 55)

alert(IdsToClone)

 window.open("/"+IdsToClone +"/e?clone=1&retURL=/"+IdsToClone )

 

but when I try to alert the result (whcih will also show the session id) keeps giving the error:

"Unexpected token ILLEGAL"

 

Thanks 4 all,


Prad@SFDCPrad@SFDC

 

Hi,

 

If i am right when you click on the button on detail page of Location__c then it should open a window with the following URL  : window.open("/"+IdsToClone +"/e?clone=1&retURL=/"+IdsToClone );

Where IdsToClone is a id of  Promo_Material_Snapshot__c.

 

Did you tried my Answer ?

 

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
sforce.connection.sessionid="{!GETSESSIONID()}";
var a="{!Location__c.Id}";
var strQuery="SELECT Id FROM Promo_Material_Snapshot__c where id =\'"+ a + "\' limit 1";
var result = sforce.connection.query(strQuery);
var records = result.getArray("records");
var IdsToClone =records[0].Id;
alert(IdsToClone);

window.open("/"+IdsToClone +"/e?clone=1&retURL=/"+IdsToClone );

 

 

IdsToClone is a id of Promo_Material_Snapshot__c.

Stefanos.ThomaidisStefanos.Thomaidis

Hi,

 

I tried your answer but when I try to first alert what I get from {!GETSESSIONID()}, it doesn't give the session salesforce id but the value of a field with API : ID__c (i.e 6.073)

This is why I stated if we have a conflict.

 

Thanks,


Stefanos.ThomaidisStefanos.Thomaidis

The most funny of all is that when I try the same code, same button in the Sandbox, I get the desired 15digit session Id!!!!

And the fields are all the same as in production!

Any ideas?

 

all the best,

Prad@SFDCPrad@SFDC

Hi,

 

Have you checked that the Location__c has the child Record Promo_Material_Snapshot__c.

 

Please paste your code also.

Stefanos.ThomaidisStefanos.Thomaidis

Hi,

 

Yes, there is a lookup in Promo_Material_Snapshot__c to Location__c which is the parent.

Both exist in production and in the Sandbox.

 

I try your code in Production and I do not get the session id but the value from the field "ID__c".

The same field exists in the Sandbox and when I try your code, it gives me the session ID.

all the best,

Prad@SFDCPrad@SFDC

Hi,

 

I am talking about the record Location__c has a record of Promo_Material_Snapshot__c in related list when you click on the Button on detail page of Location__c record.

 

Stefanos.ThomaidisStefanos.Thomaidis

Hi, 

 

Yes it has.

 

best,

jflint86jflint86

I just ran into a similar issue.  I created a field in my sandbox called Id__c and copied data from Production to the Sandbox.  I populated this Id__c field with the value from Id in Production. 

 

Some Apex code I was running picked up the value of the Id__c field instead of the standard field Id.  It caused an error of course, since the Id does not exist in the Sandbox.

 

I don't know why this occured, but I learned never to name a custom field "Id" again.

 

Stefanos.ThomaidisStefanos.Thomaidis

Cheers mate,

 

Thanks for the answer.  This is what I also came across and finally solve it - and now it works - by comparing some other 2 fields which exist both in Parent and child objects.

The customer is happy, I am also!

 

All the best,

Stefanos