• Kon Dele
  • NEWBIE
  • 130 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 20
    Questions
  • 17
    Replies
How can I via apex automatically @mention the case owner or case contact on the case feed in chatter? 
Using the code example in the developer's guide, here's my class:
public with sharing class AtMentionsUtility {  

    public static void mentionedUser(){           
        
        Case c = new Case();
        ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
        ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
        ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
        ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();

        messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

        mentionSegmentInput.id = c.OwnerId;
        messageBodyInput.messageSegments.add(mentionSegmentInput);

        textSegmentInput.text = '';
        messageBodyInput.messageSegments.add(textSegmentInput);

        feedItemInput.body = messageBodyInput;
        feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
        feedItemInput.subjectId = c.Id;

        ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput, null);
    }
}

 
I am trying to copy multi-select picklist values into a Long Text Area field. The issue I am running into is I'm able to copy over a single value, but when multiple values are selected it throws an error.
User-added image
How can I split the String value so that the selection is copied into the Long Text Area with line breaks as follows:
Germany
Kentucky

Here's my code:
public class updateMultiSelectPicklistField {
    public static void updateMSelectPickField (ContentVersion[] ContentVersion1){
        for(ContentVersion c: ContentVersion1){        
            c.Main_Offices__c = c.Office__c;  
        }
    }
}
and trigger:
trigger updatefields on ContentVersion (before insert, before update) {
    ContentVersion[] ContentVersion1 = Trigger.new;
    updateMultiSelectPicklistField.updateMSelectPickField(ContentVersion1);
}


 
Hely yall,
We have a custom VF QuoteLineItem page that automatically calculates Total price based on Discount and Unit Price entered. Is there a way to lock down the UnitPrice  field so that it's only editable by certain profiles? It's an editable apex:inputField. I've tried using an outputField, which displays the UnitPrice but can no longer calculate the Total price. Here's a screenshot and code snippet.User-added image
<apex:inputField value="{!item.Item.UnitPrice}" rendered="{!IF(($Profile.Name =='System Administrator'), true , false)}"/>


 
I've written a trigger that updates some fields on the Opportunity Product object with Product values when the Opportunity reaches 95% probability.
The trigger works when I edit and save the OpportunityLineItem but does not update if I just change the Opportunity stage to 95%.
What adjustments do i need to make so that the fields are updated as soon as the Opportunity reaches 95%?
trigger OppProductNetValue on OpportunityLineItem (before insert, before update) {
 
  Map<Id,Opportunity> parentopps = new Map<Id, Opportunity>();

  Set<Id> pbeIds = new Set<Id>();
  
     for(OpportunityLineItem oli : trigger.new) {
        if(oli.PriceBookEntryId != null){
            pbeIds.add(oli.PriceBookEntryId);
            parentopps.put(oli.OpportunityId,null);
       }
    }
    Map<id, PriceBookEntry> pbeMap = new Map<id, PriceBookEntry>(
          [SELECT id, Product2.id, Product2.True_Price__c
         FROM PriceBookEntry WHERE id in :pbeIds]);
    parentopps.putall([select Id, Probability, Order__c from Opportunity where Id in :parentopps.keyset()]);
    
     for(OpportunityLineItem oli : trigger.new) {
        if((pbeMap.containsKey(oli.PriceBookEntryId))&&(parentopps.get(oli.OpportunityId).Order__c == 'Sales Line')&&(parentopps.get(oli.OpportunityId).Probability == 95)){
           oli.Net_Value__c = pbeMap.get(oli.PriceBookEntryId).Product2.True_Price__c*oli.Quantity;      
      }else{
           oli.Net_Value__c = (1-oli.Discount*.01)*(oli.Quantity)*(oli.UnitPrice);
   }  
  }
}

 
I am trying to create a custom button on the Quote page that creates a Sales Request record when clicked.
The Sales Request is a child object and has a lookup to Quote & Opportunity fields.
I keep getting this error: 'Cannot set property 'Id' of undefined'
Any direction would be appreciated. Here's what I have so far:

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

var srq = new sforce.SObject("Sales_Request__c");
  srq.Id = "{!Sales_Request__c.Id}";
  srq.Quote__r.Id = "{!Quote.Id}";
  srq.Quote__r.OpportunityId = "{!Opportunity.Id}";
  srq.Status_c = "New";
  srq.Type__c = "New";
 
  var result = sforce.connection.create([srq]);
    
   if(result[0].getBoolean("success")){
   window.location = "/" + result[0].id + "/e";
    }else{
    alert('Could not create record '+result);
}


I'm trying to write a test class for the following trigger, but I'm only covering 28% of the lines. How can I edit it to cover the 4 if/else conditions?

My Trigger:
 
trigger AccountNumberIndex on Opportunity(before insert,before update){
    
    List<Id> accIds = new List<Id>();
    
    for(Opportunity opp:trigger.new){

        if(opp.AccountId!=null){

            accIds.add(opp.AccountId);
           }
       }
    
       Map<Id,Account> accMap = new Map<Id,Account>([select id,LC_Region__c,AccountNumber,Type 
                                                     from Account where id in:accIds]);
        
       Counterr__c value = new Counterr__c();
                    
       for(Counterr__c record:[SELECT Id,Ones__c,Threes__c,Eights__c,Nines__c FROM Counterr__c FOR UPDATE]) {
                      
       value = record;

     for(Opportunity opp :trigger.new){
        
         if(!accMap.IsEmpty()){
    
            if((opp.Probability == 90) && (accMap.get(opp.AccountId).AccountNumber == null)){
            
              if((accMap.get(opp.AccountId).LC_Region__c == 'North America')&&(accMap.get(opp.AccountId).Type != 'Prospect')){

                accMap.get(opp.AccountId).AccountNumber = String.valueOf(value.Ones__c);
      
                value.Ones__c+= 1;
                }
               else if ((accMap.get(opp.AccountId).LC_Region__c == 'North America')&&(accMap.get(opp.AccountId).Type == 'Prospect')){

                accMap.get(opp.AccountId).AccountNumber = String.valueOf(value.Threes__c);

               value.Threes__c+= 1;
                }
               else if ((accMap.get(opp.AccountId).LC_Region__c == 'International')&&(accMap.get(opp.AccountId).Type != 'Prospect')){

                accMap.get(opp.AccountId).AccountNumber = String.valueOf(value.Eights__c);
    
               value.Eights__c+= 1;
               }
               else if ((accMap.get(opp.AccountId).LC_Region__c == 'International')&&(accMap.get(opp.AccountId).Type == 'Prospect')){

                accMap.get(opp.AccountId).AccountNumber = String.valueOf(value.Nines__c);
  
                value.Nines__c+= 1;
                }
            update value;
           }
         update accMap.values();
        }
      }            
    }
}

And the test class so far...(The trigger is designed to assign an AccountNumber stored in a custom Counter setting)
@isTest
  Private Class UnitTest_AccountNumberIndex{

static testMethod void LCUnitTest(){

      /*Create Counter*/
      Counter__c Count= new Counter__c();
      Count.Name='Counter'; 
      Count.Ones__c=101000;

      /*Create User/America/Account*/
      Account a1 = new Account();
      a1.name='Test Account1';
      a1.Type= 'Customer-Direct';
      a1.LC_Region__c='North America';
      a1.AccountNumber=null;
      insert a1;
      
      /*Create Opp1*/      
      Opportunity o1=new Opportunity();
      o1.Name='Test Opp1';
      o1.AccountId=a1.Id;
      o1.closeDate=date.today();
      o1.StageName='Perception Analysis';
      o1.Probability=70;
      insert o1;

      o1.StageName='Negotiation/Review';
      o1.Probability=90;
      update o1;

      a1.AccountNumber=String.valueOf(Count.Ones__c);
      update a1;
      System.assertEquals(String.valueOf(Count.Ones__c),a1.AccountNumber);
   
    }
}

Thanks!
Greetings,
I'm new to apex and have been trying to design a trigger that would populate the Account Number field with a value starting at 10000, when the Opportunity Probability moves to 85%. Before I added my 'if' statements, it worked fine, but after that nothing. I even tried writing the trigger on Opportunity and Account objects. 

I created a custom object accNumber__c and custom field Ones__c, where the generated number would be stored. 

Please, please, i need some help on this one. Here is what I have so far:
trigger AccountNumberUpdate on Opportunity(before insert, before update) {
  
                accNumber__c value = new accNumber__c(Name='1',Ones__c=0);
                    for(accNumber__c record:[SELECT Id,Name,Ones__c FROM accNumber__c WHERE Name='1' FOR UPDATE]) {
                      value = record;
  }
  
                 for(Opportunity opps:Trigger.new) {
                        if((opps.Probability>=85)&&(opps.Account.Region__c=='Americas')&&(opps.AccountNumber==null)) {
      opps.Account.AccountNumber ='0'.repeat(math.max(0,0-String.valueOf(value.Ones__c).length()))+String.valueOf(value.Ones__c);
      value.Ones__c+=1;
    }
  }
  update value;
}
The Account object is a lookup on the Opportunity object. Is there another way I can approach this problem? Any help is appreciated.

I'm working on a requirement as follows: Whenever an Opportunity is created or edited, its custom region picklists (Region__c) should be overwritten with the Account region picklists (Region__c).

Trying to develop a trigger that would satisfy this requirement.

Whenever an Opportunity is created or edited, check to see if any of the custom Region__c picklists are NULL. If any are NULL, overwrite all picklist fields with corresponding data from the Account object.

Here is what I have so far:

trigger UpdateOpptyPicklists on Opportunity(Before Insert, Before Update) {
    for(Opportunity o: Trigger.new){
     Account a = [select id,Region__c,Area__c, Division__c from Account where Id=:o.AccountId];
      
        if(o.Region__c == NULL || o.Area__c == NULL || o.Division__c == NULL){
        
          o.Region__c=a.Region__c;
          o.Area__c = a.Area__c;
          o.Division__c=a.Division__c;
       }
  }
}

One major flaw with my trigger is that I have SOQL running inside a loop. How can I bulkify this trigger?

Trying to get at least 75% coverage on this trigger. Here's what i have so far

My trigger:

trigger UpdateOpptyPicklists on Opportunity(Before Insert, Before Update) {
    for(Opportunity o: Trigger.new){
     Account a = [select id,LC_Region__c,LC_Area__c, LC_Division__c from Account where Id=:o.AccountId];
       
        if(o.LC_Region__c == NULL || o.LC_Area__c == NULL || o.LC_Division__c == NULL){
         
          o.LC_Region__c=a.LC_Region__c;
          o.LC_Area__c = a.LC_Area__c;
          o.LC_Division__c=a.LC_Division__c;
       }
  }
 }
  
The test class:

@isTest
private class UnitTests_UpdateOpptyPicklists {

    static testMethod void myUnitTest() {
      Account a = new Account();
      a.Name='TEST ACCOUNT';
      a.Type='Competitor';
      a.LC_Region__c='Americas';
      a.LC_Area__c='West';
      a.LC_Division__c='MA';
      insert a;
     
      Opportunity o=new Opportunity();
      o.Name='TEST';
      o.AccountId=a.Id;
      o.Type='New Customer';
      o.closeDate=date.today();
      o.StageName='Prospecting';
      o.Amount=10000;
      o.LC_Region__c='Americas';
      o.LC_Area__c='East';
      o.LC_Division__c='SE';
      insert o;
     
      RecordType rt=new RecordType();
      try{
       rt=[SELECT Id from RecordType where sObjectType='Opportunity' and isActive=true limit 1];
      }
      catch(Exception e){
       rt=null;
      } 

      if(o.LC_Region__c == NULL || o.LC_Area__c == NULL || o.LC_Division__c == NULL){
      o.LC_Region__c=a.LC_Region__c;
      o.LC_Area__c=a.LC_Region__c;
      o.LC_Division__c=a.LC_Region__c;
      update o;
      }
}    
}

Could someone enlighten me why I'm I getting only 50% coverage
Hi,
How can I write a trigger that would assign a value from a predefined range to the Account number field when: (1) the Opportunity stage moves to 80% (2) the Account Number field is NULL. The trigger should find the highest number in the predefined number range and assign a number +1. So far this is what i have:

trigger AssignAccountNumber on Opportunity (after update){
   for (Opportunity p:Trigger.old){
     if (p.Probability == 80){
    //Should I insert code here that specifies Account number field == 'NULL'
   }
}
}

Need some ideas on how I would start this off. Thanks