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
sml9099sml9099 

salesforce custom javascript button to check condition on opportunity

Hey 
I have a custom button on opportunity Line Item called "TRAFFIC" . User can select any number of products in opportunity can press TRAFFIC button. When they press TRAFIC button , it changes the status of TRAFFICKED__c checkbox from false to true.  Trafficked__c checkbox is a field on opportunity products. I want to add a functionality in my existing code where it shuold also check status__c field condition in opportunity . Its like checking two conditions. Trafficked__c field on OLI and Status__c field on Opportunity and gives an alert message ( ARE YOU SURE ?)Below is my code.

{!REQUIRESCRIPT('/soap/ajax/28.0/connection.js')}

var opportunityRecord= {!GETRECORDIDS($ObjectType.OpportunityLineItem)};

if (opportunityRecord[0] == null) {
alert("Please select at least one product to traffic.") }
else {

var oppLineItemsIds = "";
//fetch opportunity Line items Ids
for(var rowNum in opportunityRecord){
oppLineItemsIds += "'"+ opportunityRecord[rowNum] + "',";
}

//remove last comma
oppLineItemsIds = oppLineItemsIds.slice(0, oppLineItemsIds.length - 1);

//Enclose the ids in round brackets
if(oppLineItemsIds.length > 1){
oppLineItemsIds = "(" + oppLineItemsIds + ")";
}


var result = sforce.connection.query("SELECT id,Trafficked__c FROM OpportunityLineItem WHERE ID in "+oppLineItemsIds);

var records = result.getArray("records");
if(records.Trafficked__c == 'false'){ 
alert("all set");
}
for(var i=0;i<records.length;i++)
{
if(records[i].Trafficked__c == 'false'){
records[i].Trafficked__c = true;
}

}

var resultoli = sforce.connection.update(records);
if(resultoli[0].success=='true'){
window.location.reload(); }
}
Chidambar ReddyChidambar Reddy
I don't have a clear idea on this. But I can suggest

by querying "Select Id, Status__c, (SELECT id,Trafficked__c FROM OpportunityLineItems) from Opportunity Where Ids IN SOMECONDITION "


for ( Opportunity opp : opps){
      
     for( OpportunityLineItem oli : opp.OpportunityLineItems){
            
              i
f((oli.Trafficked__c == 'false')&&(opp.Status__c ='something')){
                         records[i].Trafficked__c = true;
           }

  }
}

Thank you
Chidambar ReddyChidambar Reddy
Query can be 

Select Id, Status__c, (SELECT id,Trafficked__c FROM OpportunityLineItems) from Opportunity Where Ids IN (Select OpportunityId from OpportunityLineItems)
sml9099sml9099
Hey , 
Thanks for your reply. I made the changes you suggested and I am getting this error. I think the problem is in SOQL query.  Below is my code also. Thanks again for your help.

A problem with the OnClick JavaScript for this button or link was encountered:

Expected token ')'


{!REQUIRESCRIPT('/soap/ajax/28.0/connection.js')}

var opportunityRecord= {!GETRECORDIDS($ObjectType.OpportunityLineItem)};

if (opportunityRecord[0] == null) {
alert("Please select at least one product to traffic.") }
else {

var oppLineItemsIds = "";
//fetch opportunity Line items Ids
for(var rowNum in opportunityRecord){
oppLineItemsIds += "'"+ opportunityRecord[rowNum] + "',";
}

//remove last comma
oppLineItemsIds = oppLineItemsIds.slice(0, oppLineItemsIds.length - 1);

//Enclose the ids in round brackets
if(oppLineItemsIds.length > 1){
oppLineItemsIds = "(" + oppLineItemsIds + ")";
}


var result = sforce.connection.query(Select Id,Flight_Billing_Type__c , (SELECT id,Trafficked__c FROM OpportunityLineItems WHERE ID in "+oppLineItemsIds) from Opportunity Where Ids IN (Select OpportunityId from OpportunityLineItems));



var records = result.getArray("records");
for ( Opportunity opp : opps){
for(var i=0;i<records.length;i++)
{
if(records[i].Trafficked__c == 'false'&&opp.Flight_Billing_Type__c ='Strict Billing/Strict Targets'){
records[i].Trafficked__c = true;
}

} }

var resultoli = sforce.connection.update(records);
if(resultoli[0].success=='true'){
window.location.reload(); }
}