• Alaric Wimer
  • NEWBIE
  • 50 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 21
    Replies
I am using Email Services and I'm trying to extract field values (First Name, Last Name, Email, Phone, etc.) from the body of the email. Both the sender address and the subject do not contain the information I need.


In my class, I was able to isolate the the text of the email by using the following: 
String emailText = email.plainTextBody;


But I am struggling using String methods to isolate and get the field values from the text. 


Here's an example of the text I have to work with from the body of the email:

"
Requested by: John Doe
johndoe@example.com
Phone: 5555555555
Address of site: 123 Main St, Anywhere CA 12345
Electric Spend: 100 per month
Utility Company: PacifiCorp
Additional information: System Size (Kw): 7.76
Roof Type: Shingles
Roof Pitch: Slight
Shading: Shaded (can improve)
Credit Score: Over 680 Estimate
"
 

Any help is greatly appreciated! Thank you!

I receive certain email that I always have to manually create leads from. These emails contain certain text in their subject line that helps to uniquely identify them.

 

For example, lets say every email I receive that contains "Test 1" or "Example 1" should be automatically turned into a lead.

 

Does anyone know how to go about this?

I have a custom field called "Consultant" on my Opportunity. I need the BCC email address to automatically equal the Consultant's email address by default. 

Does anyone know how I can accomplish this? Any help is greatly appreciated.
I had to adjust a formula for one of my formula fields. However, I also have a field that is set to match the value of the formula field. The field is set via process builder.
 
After I updated the formula, the second field only updates after an update. (Edit => Save)
 
Is there any way I can perform this action (Edit => Save) on all Opportunities at once?

I need some help with how to approach the following tasks.

1. I need to perform a one time mass update that will change all account and contact owners to match the owner of the opportunities they are related to. 

2. (after the one time mass update is finished) when opp owner is updated also updated related contact and account owner to the new owner

I'm not really sure how to approach these in the most efficient way and safest way. I'm thinking an Apex trigger but would need some guidance with the code. Any help is greatly appreciated. Thank you!

I have a custom checkbox field on Opportunity called “Same as Mailing Address?” that pulls the Accounts mailing address information (all custom fields) to the Opportunity’s custom “Project Address” fields when the user checks it. The issue I have is as follows:

1. The user tries to edit any of the Project Address, they should get a warning telling them that they must uncheck the “Same as Mailing Address?” checkbox before they can edit. I have been able to do this with validation rule BUT:


*** I also want the user to be able to reset the Project address to the Account mailing address by rechecking the checkbox if they want. The problem I’m having is that the validation rule also fires if the user tries to reset the Project Address details by rechecking the box because the box is checked and the address is being edited.

For Example:
1. User checks the “Same as Mailing Address?” box.

2. Project address custom fields are automatically mapped over after Save (via process builder).
2. User tries to edit the Project Address without unchecking the box: validation rule should fire telling them to uncheck the box first.
3. If a user unchecks the box, then edits the address, saves. And later decides to reset Project Address by rechecking the box, this needs to be successful. (right now my validation rule fires again because the address is trying to be changed with the box checked).


Does anyone know how I can get this to work?


Here’s my current validation rule:
Same_as_Mailing_Address__c = True &&
 (
    ISCHANGED(Project_Address__c) ||
    ISCHANGED(Project_City__c)  ||
    ISCHANGED(Project_State__c) ||
    ISCHANGED(Project_Zip_Code__c)
)

 

I trying to create an Opportunity trigger that sets the name of a new Opportunity to "AccountName - AccountCity, AccountState" for example ("Test Corp. - San Franciso, CA").

What's difficult is that I have a custom checkbox field on the Opportunity named "Same as Mailing Address?". When this checkbox is checked, it sets the custom Opportunity address fields "Project Address", "Project City", "Project State" and "Project Zip Code" to the Account's custom fields "Mailing Address", "Mailing City", "Mailing State", "Mailing Zip Code" via process builder. But the Opportunity name doesn't update right away.

The goal is to have the user create an Opportunity, check the box, hit Save, and then The opportunity name is automatically displayed correctly.

However, what happens is that only the Account name shows up as the Opportunity name while the Project Address fields are correctly filled out. Then after one edit of any kind, the name changes to the correct format. 

I want it to show the correct naming convention after the user hits Save for the first time.

Screenshots of Opportunity create screen:

User-added image
After Save, this is what displays in the Opportunity record:

User-added image

Then after any update, it changes to the correct name:

User-added image
 

Here's my trigger I'm working on that doesn't work.

trigger NameOpportunity on Opportunity (before insert) {
     Set<Id> AccountIds = new Set<Id>(); // Initialize a list of Ids
     String accName; // Initialize account name variable
     String newOppName; // Initialize final opportunity name variable

    // Add an account ID to the list for every opportunity caught by trigger
    for (Opportunity op : Trigger.new) {
      AccountIds.add(op.AccountId);
    }

    // Create a map of Ids and account, querying the name from account records whose Id is in the AccountIds list generated earlier
    Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Name FROM Account WHERE Id IN :AccountIds]);
    System.debug(accountMap);

    // Update the Opportunity name for every opportunity entering the trigger that has and Account, and "Same as Mailing Address" checked
    for (Opportunity myOpp : Trigger.new) {
            if (myOpp.AccountId != null && myOpp.Same_as_Mailing_Address__c == true) {
                accName = accountMap.get(myOpp.AccountId).Name;
                // set naming conventions
                newOppName = accountMap.get(myOpp.AccountId).Name + ' - ' + myOpp.Project_City__c + ', ' + myOpp.Project_State__c;
                myOpp.Name = newOppName;
            }
        }
}

Any help is greatly appreciated thank you!

I'm attempting to write a trigger that adds Opportunity Products' names to custom fields on the Opportunity. I'm running in to a couple of issues: 

1. How can I reference :opp.Id in a before trigger? Ideally, I'd like to avoid using an after trigger.

2. How can I move that SOQL query outside of the trigger loop?

3. I keep getting an error that says I'm referencing "read only" fields on the oli. 

Here's my trigger so far:

trigger AddProductNames on Opportunity (before insert, before update) {
    for (Opportunity opp : Trigger.new) {
        // Get opportunity products
        List<OpportunityLineItem> olis = [
          SELECT Product2.Family, Product2.Name
            FROM OpportunityLineItem
           WHERE OpportunityId = :opp.Id
        ];
        if (!olis.isEmpty()) {
            // iterate through olis and assign their names to opp fields
            for (OpportunityLineItem oli : olis) {
                if (oli.Product2.Family == 'Inverters') {
                    opp.Inverter_Name__c = oli.Product2.Name;
                } else if (oli.Product2.Family == 'Panels') {
                    opp.Panel_Name__c = oli.Product2.Name;
                }
            }
        }
    }
}

I have a trigger that updates the quantity of one opportunity product when another one's quantity is updated. Everything seems to be working when I mess around in the sandbox. However, the last test class assertion keeps failing. Does anyone know why it's failing? Any help is greatly appreciated.

Here's my trigger:

trigger UpdateInverterQuantity on OpportunityLineItem (before update) {
    Map<Id, Product2> prodMap = new Map<Id, Product2>([
        SELECT Id, Name 
          FROM Product2
         WHERE Name = 'Heliene 60M-BLK 310W' 
            OR Name = 'Heliene 60M-HBLK 300W'
    ]); 

    for (OpportunityLineItem oli : Trigger.new) {

        // Get related inverter from same opportunity
        List<OpportunityLineItem> inverters = [ 
            SELECT Id, Quantity, Product2.Name 
              FROM OpportunityLineItem
             WHERE OpportunityId = :oli.OpportunityId
               AND (Product2.Name = 'APSystems QS1'
                OR Product2.Name = 'YC600') 
             LIMIT 1
        ];

        if (prodMap.ContainsKey(oli.Product2Id)) { // panel 1 or panel 2
            // Get quantity of panels
            if (!inverters.isEmpty()) {
                // set inverter quantity formulas
                Decimal inverterQuantityOne = (inverters.get(0).Quantity / 4);
                Decimal inverterQuantityTwo = (inverters.get(0).Quantity / 2);
                inverterQuantityOne = inverterQuantityOne.round(System.RoundingMode.CEILING);
                inverterQuantityTwo = inverterQuantityTwo.round(System.RoundingMode.CEILING);

                // update quantity of inverter
                if (inverters.get(0).Product2.Name == 'APSystems QS1') { // inverter 1  
                    inverters.get(0).Quantity = inverterQuantityOne;
                } else if (inverters.get(0).Product2.Name == 'YC600') { // inverter 2
                    inverters.get(0).Quantity = inverterQuantityTwo;
                }
                update inverters;
            }
        }
    }
}


And here's my test class:

@isTest
private class UpdateInverterQuantityTest {
    @isTest static void updatePanelQuantity() {
        // create an opportunity
        Opportunity myOpp  = new Opportunity();
        myOpp.Name         = 'Test';
        myOpp.StageName    = 'Closed Won';
        myOpp.CloseDate    = Date.today();
        myOpp.Pricebook2Id = Test.getStandardPricebookId();
        insert myOpp;

        Id pricebookId = Test.getStandardPricebookId();

        //Create your panel
        Product2 panel = new Product2(
            Name        = 'Heliene 60M-HBLK 300W',
            ProductCode = 'H300W',
            isActive    = true
        );
        insert panel;

        //Create your inverter
        Product2 firstInverter = new Product2(
            Name        = 'APSystems QS1',
            ProductCode = 'APS',
            isActive    = true
        );
        insert firstInverter;

        //Create your pricebook entry for panel
        PricebookEntry  panelEntry = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id   = panel.Id,
            UnitPrice    = 100.00,
            IsActive     = true
        );
        insert panelEntry;

        //Create your pricebook entry for inverter
        PricebookEntry inverterEntry = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id   = firstInverter.Id,
            UnitPrice    = 100.00,
            IsActive     = true
        );
        insert inverterEntry;

        // add panel to opp products
        OpportunityLineItem oli = new OpportunityLineItem(
            OpportunityId    = myOpp.Id,
            Quantity         = 20,
            PricebookEntryId = panelEntry.Id,
            TotalPrice       = 20 * panelEntry.UnitPrice
        );
        insert oli;

        // add inverter to opp products
        OpportunityLineItem oli2 = new OpportunityLineItem(
            OpportunityId    = myOpp.Id,
            Quantity         = 20,
            PricebookEntryId = inverterEntry.Id,
            TotalPrice       = 20 * inverterEntry.UnitPrice
        );
        insert oli2;

        // Get updated inverter quantity
        OpportunityLineItem inverter = [
            SELECT Quantity
              FROM OpportunityLineItem
             WHERE OpportunityId = :oli.OpportunityId
               AND Id = :oli2.Id
             LIMIT 1
        ];

        System.assertEquals(5, inverter.Quantity);

        // Update quantity of panels
        oli.Quantity = 40;
        update oli;

        // Get updated quantity of inverters
        OpportunityLineItem updatedInverter = [
            SELECT Quantity
              FROM OpportunityLineItem
             WHERE OpportunityId = :oli.OpportunityId
               AND Id = :oli2.Id
             LIMIT 1
        ];

        System.assertEquals(10, updatedInverter.Quantity);
    }
}

Hi everyone, I have a trigger that should fire but it doesn't seem to work. Basically, if the Opportunity Product being added is an inverter, the trigger should check to see if there is a panel in the Opportunity Products and then set the quanitity of the inverter being added to a number based on the quantity of the panel.

The trigger worked for me when I hardcoded the Product2Id's but I want to avoid doing that. When I changed the Product2Id to Product2.Name the trigger stopped working.

Here is my code, any help is greatly appreciated!

trigger setInverterQuantity on OpportunityLineItem (before insert) {
    for (OpportunityLineItem oli : Trigger.new) {
        if (oli.Product2.Name == 'APSystems QS1' || oli.Product2.Name == 'Y6COO') { // inverter 1 or inverter 2
            // get quantity of panels
            List<OpportunityLineItem> panels = [
                SELECT Id, Quantity, Product2.Name
                  FROM OpportunityLineItem
                 WHERE OpportunityId = :oli.OpportunityId
                   AND (Product2.Name = 'Heliene 60M-BLK 310W'
                    OR Product2.Name = 'Heliene 60M-HBLK 300W') // panel 1 or panel 2
                 LIMIT 1
            ];

            if (!panels.isEmpty()) {
                // set inverter quantity formulas
                Decimal inverterQuantityOne = (panels.get(0).Quantity / 4);
                Decimal inverterQuantityTwo = (panels.get(0).Quantity / 2);
                inverterQuantityOne = inverterQuantityOne.round(System.RoundingMode.CEILING);
                inverterQuantityTwo = inverterQuantityTwo.round(System.RoundingMode.CEILING);

                // set quantity of inverter
                if (oli.Product2.Name == 'APSystems QS1') { // inverter 1
                    oli.Quantity = inverterQuantityOne;
                } else if (oli.Product2.Name == 'Y6COO') { // inverter 2
                    oli.Quantity = inverterQuantityTwo;
                }
            }
        }
    }
}
I am using Email Services and I'm trying to extract field values (First Name, Last Name, Email, Phone, etc.) from the body of the email. Both the sender address and the subject do not contain the information I need.


In my class, I was able to isolate the the text of the email by using the following: 
String emailText = email.plainTextBody;


But I am struggling using String methods to isolate and get the field values from the text. 


Here's an example of the text I have to work with from the body of the email:

"
Requested by: John Doe
johndoe@example.com
Phone: 5555555555
Address of site: 123 Main St, Anywhere CA 12345
Electric Spend: 100 per month
Utility Company: PacifiCorp
Additional information: System Size (Kw): 7.76
Roof Type: Shingles
Roof Pitch: Slight
Shading: Shaded (can improve)
Credit Score: Over 680 Estimate
"
 

Any help is greatly appreciated! Thank you!
I had to adjust a formula for one of my formula fields. However, I also have a field that is set to match the value of the formula field. The field is set via process builder.
 
After I updated the formula, the second field only updates after an update. (Edit => Save)
 
Is there any way I can perform this action (Edit => Save) on all Opportunities at once?

I need some help with how to approach the following tasks.

1. I need to perform a one time mass update that will change all account and contact owners to match the owner of the opportunities they are related to. 

2. (after the one time mass update is finished) when opp owner is updated also updated related contact and account owner to the new owner

I'm not really sure how to approach these in the most efficient way and safest way. I'm thinking an Apex trigger but would need some guidance with the code. Any help is greatly appreciated. Thank you!

I trying to create an Opportunity trigger that sets the name of a new Opportunity to "AccountName - AccountCity, AccountState" for example ("Test Corp. - San Franciso, CA").

What's difficult is that I have a custom checkbox field on the Opportunity named "Same as Mailing Address?". When this checkbox is checked, it sets the custom Opportunity address fields "Project Address", "Project City", "Project State" and "Project Zip Code" to the Account's custom fields "Mailing Address", "Mailing City", "Mailing State", "Mailing Zip Code" via process builder. But the Opportunity name doesn't update right away.

The goal is to have the user create an Opportunity, check the box, hit Save, and then The opportunity name is automatically displayed correctly.

However, what happens is that only the Account name shows up as the Opportunity name while the Project Address fields are correctly filled out. Then after one edit of any kind, the name changes to the correct format. 

I want it to show the correct naming convention after the user hits Save for the first time.

Screenshots of Opportunity create screen:

User-added image
After Save, this is what displays in the Opportunity record:

User-added image

Then after any update, it changes to the correct name:

User-added image
 

Here's my trigger I'm working on that doesn't work.

trigger NameOpportunity on Opportunity (before insert) {
     Set<Id> AccountIds = new Set<Id>(); // Initialize a list of Ids
     String accName; // Initialize account name variable
     String newOppName; // Initialize final opportunity name variable

    // Add an account ID to the list for every opportunity caught by trigger
    for (Opportunity op : Trigger.new) {
      AccountIds.add(op.AccountId);
    }

    // Create a map of Ids and account, querying the name from account records whose Id is in the AccountIds list generated earlier
    Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Name FROM Account WHERE Id IN :AccountIds]);
    System.debug(accountMap);

    // Update the Opportunity name for every opportunity entering the trigger that has and Account, and "Same as Mailing Address" checked
    for (Opportunity myOpp : Trigger.new) {
            if (myOpp.AccountId != null && myOpp.Same_as_Mailing_Address__c == true) {
                accName = accountMap.get(myOpp.AccountId).Name;
                // set naming conventions
                newOppName = accountMap.get(myOpp.AccountId).Name + ' - ' + myOpp.Project_City__c + ', ' + myOpp.Project_State__c;
                myOpp.Name = newOppName;
            }
        }
}

Any help is greatly appreciated thank you!

I have a trigger that updates the quantity of one opportunity product when another one's quantity is updated. Everything seems to be working when I mess around in the sandbox. However, the last test class assertion keeps failing. Does anyone know why it's failing? Any help is greatly appreciated.

Here's my trigger:

trigger UpdateInverterQuantity on OpportunityLineItem (before update) {
    Map<Id, Product2> prodMap = new Map<Id, Product2>([
        SELECT Id, Name 
          FROM Product2
         WHERE Name = 'Heliene 60M-BLK 310W' 
            OR Name = 'Heliene 60M-HBLK 300W'
    ]); 

    for (OpportunityLineItem oli : Trigger.new) {

        // Get related inverter from same opportunity
        List<OpportunityLineItem> inverters = [ 
            SELECT Id, Quantity, Product2.Name 
              FROM OpportunityLineItem
             WHERE OpportunityId = :oli.OpportunityId
               AND (Product2.Name = 'APSystems QS1'
                OR Product2.Name = 'YC600') 
             LIMIT 1
        ];

        if (prodMap.ContainsKey(oli.Product2Id)) { // panel 1 or panel 2
            // Get quantity of panels
            if (!inverters.isEmpty()) {
                // set inverter quantity formulas
                Decimal inverterQuantityOne = (inverters.get(0).Quantity / 4);
                Decimal inverterQuantityTwo = (inverters.get(0).Quantity / 2);
                inverterQuantityOne = inverterQuantityOne.round(System.RoundingMode.CEILING);
                inverterQuantityTwo = inverterQuantityTwo.round(System.RoundingMode.CEILING);

                // update quantity of inverter
                if (inverters.get(0).Product2.Name == 'APSystems QS1') { // inverter 1  
                    inverters.get(0).Quantity = inverterQuantityOne;
                } else if (inverters.get(0).Product2.Name == 'YC600') { // inverter 2
                    inverters.get(0).Quantity = inverterQuantityTwo;
                }
                update inverters;
            }
        }
    }
}


And here's my test class:

@isTest
private class UpdateInverterQuantityTest {
    @isTest static void updatePanelQuantity() {
        // create an opportunity
        Opportunity myOpp  = new Opportunity();
        myOpp.Name         = 'Test';
        myOpp.StageName    = 'Closed Won';
        myOpp.CloseDate    = Date.today();
        myOpp.Pricebook2Id = Test.getStandardPricebookId();
        insert myOpp;

        Id pricebookId = Test.getStandardPricebookId();

        //Create your panel
        Product2 panel = new Product2(
            Name        = 'Heliene 60M-HBLK 300W',
            ProductCode = 'H300W',
            isActive    = true
        );
        insert panel;

        //Create your inverter
        Product2 firstInverter = new Product2(
            Name        = 'APSystems QS1',
            ProductCode = 'APS',
            isActive    = true
        );
        insert firstInverter;

        //Create your pricebook entry for panel
        PricebookEntry  panelEntry = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id   = panel.Id,
            UnitPrice    = 100.00,
            IsActive     = true
        );
        insert panelEntry;

        //Create your pricebook entry for inverter
        PricebookEntry inverterEntry = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id   = firstInverter.Id,
            UnitPrice    = 100.00,
            IsActive     = true
        );
        insert inverterEntry;

        // add panel to opp products
        OpportunityLineItem oli = new OpportunityLineItem(
            OpportunityId    = myOpp.Id,
            Quantity         = 20,
            PricebookEntryId = panelEntry.Id,
            TotalPrice       = 20 * panelEntry.UnitPrice
        );
        insert oli;

        // add inverter to opp products
        OpportunityLineItem oli2 = new OpportunityLineItem(
            OpportunityId    = myOpp.Id,
            Quantity         = 20,
            PricebookEntryId = inverterEntry.Id,
            TotalPrice       = 20 * inverterEntry.UnitPrice
        );
        insert oli2;

        // Get updated inverter quantity
        OpportunityLineItem inverter = [
            SELECT Quantity
              FROM OpportunityLineItem
             WHERE OpportunityId = :oli.OpportunityId
               AND Id = :oli2.Id
             LIMIT 1
        ];

        System.assertEquals(5, inverter.Quantity);

        // Update quantity of panels
        oli.Quantity = 40;
        update oli;

        // Get updated quantity of inverters
        OpportunityLineItem updatedInverter = [
            SELECT Quantity
              FROM OpportunityLineItem
             WHERE OpportunityId = :oli.OpportunityId
               AND Id = :oli2.Id
             LIMIT 1
        ];

        System.assertEquals(10, updatedInverter.Quantity);
    }
}

Hi everyone, I have a trigger that should fire but it doesn't seem to work. Basically, if the Opportunity Product being added is an inverter, the trigger should check to see if there is a panel in the Opportunity Products and then set the quanitity of the inverter being added to a number based on the quantity of the panel.

The trigger worked for me when I hardcoded the Product2Id's but I want to avoid doing that. When I changed the Product2Id to Product2.Name the trigger stopped working.

Here is my code, any help is greatly appreciated!

trigger setInverterQuantity on OpportunityLineItem (before insert) {
    for (OpportunityLineItem oli : Trigger.new) {
        if (oli.Product2.Name == 'APSystems QS1' || oli.Product2.Name == 'Y6COO') { // inverter 1 or inverter 2
            // get quantity of panels
            List<OpportunityLineItem> panels = [
                SELECT Id, Quantity, Product2.Name
                  FROM OpportunityLineItem
                 WHERE OpportunityId = :oli.OpportunityId
                   AND (Product2.Name = 'Heliene 60M-BLK 310W'
                    OR Product2.Name = 'Heliene 60M-HBLK 300W') // panel 1 or panel 2
                 LIMIT 1
            ];

            if (!panels.isEmpty()) {
                // set inverter quantity formulas
                Decimal inverterQuantityOne = (panels.get(0).Quantity / 4);
                Decimal inverterQuantityTwo = (panels.get(0).Quantity / 2);
                inverterQuantityOne = inverterQuantityOne.round(System.RoundingMode.CEILING);
                inverterQuantityTwo = inverterQuantityTwo.round(System.RoundingMode.CEILING);

                // set quantity of inverter
                if (oli.Product2.Name == 'APSystems QS1') { // inverter 1
                    oli.Quantity = inverterQuantityOne;
                } else if (oli.Product2.Name == 'Y6COO') { // inverter 2
                    oli.Quantity = inverterQuantityTwo;
                }
            }
        }
    }
}