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
brandfarligabrandfarliga 

How to find

Hello!

I am in the process of building a javascript function and I am needing some assistance.  I want to be able to query all of a Opportunity's products to see if the field called Publisher__c =  EAP for any record.  If any of the records have the value of EAP in Publisher field, I would like to return a value of true.  If not, return else.  I need this information so I can pull the true or false value into another function to dictate what type of documents i need to pull.

function getContractType (oppId) {
var result = sforce.connection.query("Select PricebookEntry.Product2.Name, PricebookEntry.Product2.Publisher__c From OpportunityLineItem where OpportunityId = '" + oppId + "' and PricebookEntry.Product2.Publisher__c = "EAP"");

if(!result || result['size'] != 1) { 
	return FALSE;
    }
    
    var opplineitem = result.getArray('records')[0];

    return opplineitem.Product2;
}

var EbayContract = getContractType('{!Opportunity.Id}');

ShashForceShashForce
Hi,

There is no "Product2" field on opportunityLineItem. There is only a Product2Id field, which you should be adding to the query.

Is there any other assistance you are looking for? Please provide specifics.

Thanks,
Shashank
Vinit_KumarVinit_Kumar
Your query looks just fine except for the double quotes which you are using to check of the record is EAP or not .Try the below query :-

var result = sforce.connection.query("Select PricebookEntry.Product2.Name, PricebookEntry.Product2.Publisher__c From OpportunityLineItem where OpportunityId = '" + oppId + "' and PricebookEntry.Product2.Publisher__c = 'EAP' ");
Rachel BrozinickRachel Brozinick
This is brandfarliga, just using a different account. :)  I changed my code to this now, since my client wants me to use Product Name instead of Publisher__c:

function getProductTypes (oppId) {

var hasEAPproducts = 0;

var result = sforce.connection.query("Select PricebookEntry.Product2.Name From OpportunityLineItem where OpportunityId = '" + oppId + "' and PricebookEntry.Product2.Name IN ('EAP') ");

	if(!result || result['size'] < 1) { 

	return hasEAPproducts = FALSE;
	
	} else
	
	return hasEAPproducts = TRUE;
	
	}
}

How could I change this so I could return the number of results from the query?  Would I just need to change the return statement to result['size'] to get a count of how many query results I got?  I want to to be able to call this function in another piece of code so if getProductTypes() is less than 1, run this code, else run this other code.