Skip to content

Instantly share code, notes, and snippets.

@johndstein
Last active August 25, 2016 19:49
Show Gist options
  • Select an option

  • Save johndstein/55254a7cbb5d978413fb65e82aa43a36 to your computer and use it in GitHub Desktop.

Select an option

Save johndstein/55254a7cbb5d978413fb65e82aa43a36 to your computer and use it in GitHub Desktop.
Add michbusiness products to memberships
String MEMBERSHIP_RECORD_TYPE = '01236000000nXvOAAU';
void addProducts(List<Opportunity> opps) {
List<OpportunityLineItem> olis = new List<OpportunityLineItem>();
Id oppId = null;
for (Opportunity o : opps) {
PricebookEntry pbe = getPricebookEntry('NEW_MEMBERSHIP');
if (o.Id == oppId) {
pbe = getPricebookEntry('MEMBERSHIP_RENEWAL');
}
oppId = o.Id;
OpportunityLineItem li = new OpportunityLineItem();
olis.add(li);
li.OpportunityId = o.Id;
li.PricebookEntryId = pbe.Id;
li.Quantity = 1;
li.TotalPrice = o.Amount;
}
System.debug(olis);
insert olis;
}
PricebookEntry getPricebookEntry(String productCode) {
String pricebookId = MembershipSettings__c.getOrgDefaults().Price_Book_Id__c;
List<PricebookEntry> pbes = getActivePricebookEntries(pricebookId);
for (PricebookEntry pe: pbes) {
if (pe.ProductCode == productCode) {
return pe;
}
}
return (PricebookEntry) null;
}
List<PricebookEntry> getActivePricebookEntries(String pricebookId) {
return [
select Id,
UnitPrice,
Pricebook2Id,
Product2Id,
ProductCode
from PricebookEntry
where Pricebook2Id = :pricebookId
and IsActive = true
and UseStandardPrice = false
];
}
List<opportunity> selectMembershipsWoProducts() {
return [
SELECT
Id,
Amount,
AccountId
FROM
Opportunity
WHERE
Id not in (select OpportunityId from OpportunityLineItem)
ORDER BY AccountId
limit 1000
];
}
System.debug(getPricebookEntry('NEW_MEMBERSHIP'));
System.debug(getPricebookEntry('MEMBERSHIP_RENEWAL'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment