• deplai
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 9
    Questions
  • 6
    Replies

I created a custom object called Feature.  For each Feature, I've added multiple Products.  On an Opportunity, I made a lookup field to the Feature object.

When I add a Feature, the trigger should add all the Products associated to that Feature as product line items into the Opportunity. 

The code works for After Insert, but will not work for After Update.  I get the error if I try to do an update.  Is there something I can do to my code to get this trigger to work after an update?

trigger OpportunityTrigger2 on Opportunity (after insert, after update) {
// List containing each Opportunity being processed
list<Opportunity> OppId = new list<Opportunity>();
// Go through each opportunity and add it to the OppId list 
for(Opportunity l:trigger.new) {
//Now add them to the list
OppId.add(l); 
}

// Now for each Opportunity in the list
for (Opportunity l:OppId) {
// Get the ID for the Feature of that Opportunity
Id FeatureId = l.Feature__c;
// Now I have the Id of the feature, create a list of the products for that feature
list<Product2> theprod = [SELECT Id, Name FROM Product2 WHERE Features__c = :FeatureId ];  

// Now lets go through each of these products
for (Product2 p:theprod) {
// Now lets get the details we need from the price book entry to insert our Opportunity line item
list<PricebookEntry> TheEntry = [SELECT Id, UnitPrice, Name, Product2Id FROM PricebookEntry where Product2id = :p.id and Pricebook2Id = '01sA00000001tiRIAQ'];
// For each of these pricebook entries
for (PricebookEntry e:TheEntry) {
// Create a new opportunity line item
OpportunityLineItem newoppprod = new OpportunityLineItem(
    // This tells the line item which opportunity to link it to
    opportunityId = l.id,
    // Add the quantity, which is usually always 1
    Quantity = 1,
    // Add the Price
    UnitPrice = e.UnitPrice,
    // Add the pricebook entry ID to tell it what the line item links to in the pricebook entry
    PricebookEntryId = e.id
);
// and now insert that line item
insert newoppprod;
}

}
}
}

 

 

  • November 05, 2012
  • Like
  • 0

I have a trigger that takes incoming/outgoing case emails and makes a copy of the email body into the case comments.  I have written some code to take out any of the reply history by looking for the string "From: " or "wrote: ".  This is to take into account the various email clients and how they might reply back.

 

The idea is that when you reply back to an email it will either say:

From: abc@xyz.com

{email body here}

 

OR

 

On 9/5/12: johndoe wrote:

{email body here}

 

So the code will take anything before the From: or wrote:

 

 

if (em.TextBody.contains('From:')){myPlainText = em.TextBody.substring(0, em.TextBody.indexOf('From:'));}    else if (em.TextBody.contains('wrote:')){myPlainText = em.TextBody.substring(0, em.TextBody.indexOf('wrote:'));}     else {myPlainText= em.TextBody;}

 The problem I'm running into is if different clients have been used in the reply messages with some using "From:" and some using "wrote: ".  My code is looking for "From: " first then stops.  If that "From: " was embedded in a bunch of replies using "wrote: ", then I would have taken a bunch of replies into my comments.

 

Is there a way to write the code so that it will look for whatever is highest on the string and then use that as "myPlainText".

  • September 05, 2012
  • Like
  • 0

We have a need to capture all emails into comments so that when a customer logs in to their portal, all the information is in the same section.  We are trying not to purchase email2case premium, which I know will do the trick.  We have coded our own trigger to do this, but the problem is it captures the whole email.  I'm not quite sure how to code it to capture only the new text from the email and not all the subsequent replies.

trigger AfterEmailMessage on EmailMessage bulk (after insert) {
  List <CaseComment> commentsToInsert = new List <CaseComment>();
  Map <String,Case> cases = new Map<String,Case>();
  List<String> casesIds = new List<String>();
  
  public final Id recordTypeId = [Select id from RecordType where name =: 'Internal'].id;
  
  for (EmailMessage em : Trigger.new){
    casesIds.add(em.parentId);
  }
  
  for(Case c : [Select id,RecordTypeId from Case where id in :casesIds]){
    cases.put(c.id,c);
  }
  
  for (EmailMessage em : Trigger.new){
    if (cases.get(em.parentId).recordTypeId == recordTypeId){
      CaseComment cc = new CaseComment();
      cc.CommentBody = em.TextBody;
      cc.IsPublished = true;
      cc.ParentId = em.parentId;
      commentsToInsert.add(cc);
    }
  }
  
  insert commentsToInsert;
  
}

 Any suggestions would be greatly appreciated.  Thanks.

  • August 30, 2012
  • Like
  • 0

Is it possible to create a VF page on a case layout that shows the related case and it's comments?  And also if I wanted to make a comment, do it on the VF page and not need to click into the related case to do so?

Any help would be greatly appreciated.  Thanks.

  • August 24, 2012
  • Like
  • 0

Hello,

We had a trigger written for us by a developer who no longer works fo us.  When I do a bulk insert/update I get errors against the governor limits.  What should be done to my code to make it more effiecient?  I'm not a coder, but I kinda figure out how things work.   Any help would be greatly appreciated! Thanks!

 

trigger BeforeCaseInsert on Case (before insert,before update) {

private final Id recordTypeId = [SELECT id FROM RecordType WHERE name =: 'JIRA'].id;
private final Id recordProdSquadID = [SELECT id FROM RecordType WHERE name =: 'Prod Specialist'].id;
private final Id recordBPID = [SELECT id FROM RecordType WHERE name =: 'BenchPress'].id;
private final Id recordSupport = [SELECT id FROM RecordType WHERE name =: 'Support'].id;
private List<Id> lstAccount = new List<Id>();
private Map<Id, Account> accMap = new Map <Id,Account>();
private List <Sites__c> sitesF;

// Get a list of Accounts
for (Case c : trigger.new){
lstAccount.add(c.accountId);
}

// Create a Map with AccountId, Account to reference later in the code
for (Account a : [Select id,publication_Manager2__c from Account where id in: lstAccount]){
accMap.put(a.id,a);
}

sitesF = new List<Sites__c>([SELECT id, Name, Feedback__c, Account__c, Account__r.Publication_Manager2__c
FROM Sites__c LIMIT 50000]);

for (Case c : trigger.new) {
for(Sites__c s : sitesF) {
if (s.Feedback__c != null
&& c.Description != null
&& c.Description != ''
&& c.recordTypeID == recordSupport
&& c.Description.contains(s.Feedback__c)) {
c.sites__c = s.id;
c.accountId = s.account__c;
accMap.put(s.account__c,s.Account__r);
break;
}
}
// This is a second bit of code
// if (c.recordTypeId == recordTypeId
// && accMap.get(c.accountId)!= null
// && accMap.get(c.accountId).Publication_Manager2__c != null) {
// c.OwnerId = accMap.get(c.accountId).publication_Manager2__c;
// }

// Paul code
if (c.recordTypeId == recordProdSquadID
&& c.JCode__c != null
&& c.JCode__c != ''
) {
for(Sites__c s : sitesF) {
if (c.JCode__c == s.Name){
c.accountId = s.account__c;
c.Sites__c = s.id;
accMap.put(s.account__c,s.Account__r);
break;
}
}

}
if (c.recordTypeId == recordBPID)
{
for(Sites__c s : sitesF) {

if (c.Journal_Code__c == s.id){
c.Sites__c = s.id;
c.accountId = s.account__c;

accMap.put(s.account__c,s.Account__r);
break;
}
}
}

}

I would like to write a trigger to send an email to the related contact of a task whenever the task is marked public.  Could someone give some direction on how to achieve this?  I'm fairly new to writing code.  Thanks.

  • March 23, 2012
  • Like
  • 0

I have a lookup field in contact to the user object.  For each user, I have a contact record as well.  I'm trying to create a VF page that will automatically pull the contact record of the current logged in user.

 

I'm still fairly new to VF, but so far I have created a class called "mycontact":

 

public class MyContact{
    
    private final Contact contact;
    
    public MyContact(){
        contact = [ select id from Contact where user__c = :UserInfo.getUserID()];
        }
        
    public Contact getContact() {
        return contact;
        }
 }

 

The question now is how do I write up the VF page to use this info and render the contact info automatically?  I will be using this VF page in a overridden tab.

 

I used something like this, but it requires me to click on it to show the info:

<apex:page Controller="MyContact" >
<apex:outputlink value="/apex/mycontact"> Click Here <apex:param name="id" value="{!contact.id}"/> </apex:outputlink>
<apex:detail />
</apex:page

 

Any help to a newbie would be greatly appreciated.  Thanks.

  • March 02, 2012
  • Like
  • 0

I created a simple visualforce page that just pulls data from an custom object.  I have a salesforce site pointed to that Page. When i view the page from within salesforce using the internal link: "https://na7.salesforce.com/apex/uptime", it looks correct.  But when I access the site page, which is calling this page, the Up TIme column has no data. :http://highwire.force.com/watchmouse.

 

Any help would be greatly appreciated.  Thanks.

 

<apex:page standardController="Watchmouse__c" recordsetVar="Watchmouse" standardStylesheets="false" showheader="false" sidebar="false">


<h1>Up Time</h1>

<apex:dataTable value="{!Watchmouse}" var="watch" rowclasses="odd,even">
<apex:column headervalue="Site">
<apex:outputtext value="{!watch.name}"/>
</apex:column>
<apex:column headervalue="Up Time">
<apex:outputtext value="{!watch.UpTime__c}"/>
</apex:column>


</apex:dataTable>

</apex:page>
                



  • August 29, 2011
  • Like
  • 0

I have a custom object called "Projects" that has a master detail relationship to "Accounts"

I also have a customer object called "Milestones" that has a lookup relationship to "Projects"

 

To create a new Milestone, I go to the Prjoects object and click on New Milestone in the related list.

Is it possible to create a new button that works similar to creating a new Milestone, but also including the Account lookup as well?

And if so, how would that be accomplished.

I created a custom object called Feature.  For each Feature, I've added multiple Products.  On an Opportunity, I made a lookup field to the Feature object.

When I add a Feature, the trigger should add all the Products associated to that Feature as product line items into the Opportunity. 

The code works for After Insert, but will not work for After Update.  I get the error if I try to do an update.  Is there something I can do to my code to get this trigger to work after an update?

trigger OpportunityTrigger2 on Opportunity (after insert, after update) {
// List containing each Opportunity being processed
list<Opportunity> OppId = new list<Opportunity>();
// Go through each opportunity and add it to the OppId list 
for(Opportunity l:trigger.new) {
//Now add them to the list
OppId.add(l); 
}

// Now for each Opportunity in the list
for (Opportunity l:OppId) {
// Get the ID for the Feature of that Opportunity
Id FeatureId = l.Feature__c;
// Now I have the Id of the feature, create a list of the products for that feature
list<Product2> theprod = [SELECT Id, Name FROM Product2 WHERE Features__c = :FeatureId ];  

// Now lets go through each of these products
for (Product2 p:theprod) {
// Now lets get the details we need from the price book entry to insert our Opportunity line item
list<PricebookEntry> TheEntry = [SELECT Id, UnitPrice, Name, Product2Id FROM PricebookEntry where Product2id = :p.id and Pricebook2Id = '01sA00000001tiRIAQ'];
// For each of these pricebook entries
for (PricebookEntry e:TheEntry) {
// Create a new opportunity line item
OpportunityLineItem newoppprod = new OpportunityLineItem(
    // This tells the line item which opportunity to link it to
    opportunityId = l.id,
    // Add the quantity, which is usually always 1
    Quantity = 1,
    // Add the Price
    UnitPrice = e.UnitPrice,
    // Add the pricebook entry ID to tell it what the line item links to in the pricebook entry
    PricebookEntryId = e.id
);
// and now insert that line item
insert newoppprod;
}

}
}
}

 

 

  • November 05, 2012
  • Like
  • 0

I have a trigger that takes incoming/outgoing case emails and makes a copy of the email body into the case comments.  I have written some code to take out any of the reply history by looking for the string "From: " or "wrote: ".  This is to take into account the various email clients and how they might reply back.

 

The idea is that when you reply back to an email it will either say:

From: abc@xyz.com

{email body here}

 

OR

 

On 9/5/12: johndoe wrote:

{email body here}

 

So the code will take anything before the From: or wrote:

 

 

if (em.TextBody.contains('From:')){myPlainText = em.TextBody.substring(0, em.TextBody.indexOf('From:'));}    else if (em.TextBody.contains('wrote:')){myPlainText = em.TextBody.substring(0, em.TextBody.indexOf('wrote:'));}     else {myPlainText= em.TextBody;}

 The problem I'm running into is if different clients have been used in the reply messages with some using "From:" and some using "wrote: ".  My code is looking for "From: " first then stops.  If that "From: " was embedded in a bunch of replies using "wrote: ", then I would have taken a bunch of replies into my comments.

 

Is there a way to write the code so that it will look for whatever is highest on the string and then use that as "myPlainText".

  • September 05, 2012
  • Like
  • 0

Hello,

We had a trigger written for us by a developer who no longer works fo us.  When I do a bulk insert/update I get errors against the governor limits.  What should be done to my code to make it more effiecient?  I'm not a coder, but I kinda figure out how things work.   Any help would be greatly appreciated! Thanks!

 

trigger BeforeCaseInsert on Case (before insert,before update) {

private final Id recordTypeId = [SELECT id FROM RecordType WHERE name =: 'JIRA'].id;
private final Id recordProdSquadID = [SELECT id FROM RecordType WHERE name =: 'Prod Specialist'].id;
private final Id recordBPID = [SELECT id FROM RecordType WHERE name =: 'BenchPress'].id;
private final Id recordSupport = [SELECT id FROM RecordType WHERE name =: 'Support'].id;
private List<Id> lstAccount = new List<Id>();
private Map<Id, Account> accMap = new Map <Id,Account>();
private List <Sites__c> sitesF;

// Get a list of Accounts
for (Case c : trigger.new){
lstAccount.add(c.accountId);
}

// Create a Map with AccountId, Account to reference later in the code
for (Account a : [Select id,publication_Manager2__c from Account where id in: lstAccount]){
accMap.put(a.id,a);
}

sitesF = new List<Sites__c>([SELECT id, Name, Feedback__c, Account__c, Account__r.Publication_Manager2__c
FROM Sites__c LIMIT 50000]);

for (Case c : trigger.new) {
for(Sites__c s : sitesF) {
if (s.Feedback__c != null
&& c.Description != null
&& c.Description != ''
&& c.recordTypeID == recordSupport
&& c.Description.contains(s.Feedback__c)) {
c.sites__c = s.id;
c.accountId = s.account__c;
accMap.put(s.account__c,s.Account__r);
break;
}
}
// This is a second bit of code
// if (c.recordTypeId == recordTypeId
// && accMap.get(c.accountId)!= null
// && accMap.get(c.accountId).Publication_Manager2__c != null) {
// c.OwnerId = accMap.get(c.accountId).publication_Manager2__c;
// }

// Paul code
if (c.recordTypeId == recordProdSquadID
&& c.JCode__c != null
&& c.JCode__c != ''
) {
for(Sites__c s : sitesF) {
if (c.JCode__c == s.Name){
c.accountId = s.account__c;
c.Sites__c = s.id;
accMap.put(s.account__c,s.Account__r);
break;
}
}

}
if (c.recordTypeId == recordBPID)
{
for(Sites__c s : sitesF) {

if (c.Journal_Code__c == s.id){
c.Sites__c = s.id;
c.accountId = s.account__c;

accMap.put(s.account__c,s.Account__r);
break;
}
}
}

}

I have a lookup field in contact to the user object.  For each user, I have a contact record as well.  I'm trying to create a VF page that will automatically pull the contact record of the current logged in user.

 

I'm still fairly new to VF, but so far I have created a class called "mycontact":

 

public class MyContact{
    
    private final Contact contact;
    
    public MyContact(){
        contact = [ select id from Contact where user__c = :UserInfo.getUserID()];
        }
        
    public Contact getContact() {
        return contact;
        }
 }

 

The question now is how do I write up the VF page to use this info and render the contact info automatically?  I will be using this VF page in a overridden tab.

 

I used something like this, but it requires me to click on it to show the info:

<apex:page Controller="MyContact" >
<apex:outputlink value="/apex/mycontact"> Click Here <apex:param name="id" value="{!contact.id}"/> </apex:outputlink>
<apex:detail />
</apex:page

 

Any help to a newbie would be greatly appreciated.  Thanks.

  • March 02, 2012
  • Like
  • 0

I created a simple visualforce page that just pulls data from an custom object.  I have a salesforce site pointed to that Page. When i view the page from within salesforce using the internal link: "https://na7.salesforce.com/apex/uptime", it looks correct.  But when I access the site page, which is calling this page, the Up TIme column has no data. :http://highwire.force.com/watchmouse.

 

Any help would be greatly appreciated.  Thanks.

 

<apex:page standardController="Watchmouse__c" recordsetVar="Watchmouse" standardStylesheets="false" showheader="false" sidebar="false">


<h1>Up Time</h1>

<apex:dataTable value="{!Watchmouse}" var="watch" rowclasses="odd,even">
<apex:column headervalue="Site">
<apex:outputtext value="{!watch.name}"/>
</apex:column>
<apex:column headervalue="Up Time">
<apex:outputtext value="{!watch.UpTime__c}"/>
</apex:column>


</apex:dataTable>

</apex:page>
                



  • August 29, 2011
  • Like
  • 0