• Donald Blay
  • NEWBIE
  • 60 Points
  • Member since 2015
  • Sr Software Developer
  • OpFocus

  • Chatter
    Feed
  • 2
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 13
    Replies
Hello!

I have a trigger that is creating a contact role on the Opportunity when a lookup field is populated but I cannot get my code coverage above 66%!
What can I do to get better coverage?!
Trigger:
trigger addContactRole on Opportunity (after Insert, after update) {

List<OpportunityContactRole> newContactRoleList=new List<OpportunityContactRole>();
List<OpportunityContactRole> oldContactRoleList=new List<OpportunityContactRole>();
Set<Id> OppId=new Set<Id>();
Set<Id> ContactId=new Set<Id>();

for(Opportunity oppObj: Trigger.new)
{
//Insert condition
if(Trigger.isInsert)
{
if(oppObj.Investor__c!=null && Trigger.oldMap.get(oppObj.Id).Investor__c==null)
{
//Creating new contact role
newContactRoleList.add(new OpportunityContactRole (ContactId=oppObj.Investor__c,OpportunityId=oppObj.Id, Role='Decision Maker', IsPrimary = true));

}
}
else
{

if(oppObj.Investor__c==null && Trigger.oldMap.get(oppObj.Id).Investor__c!=null)
{
//Getting the contact and oppty Id from old values and adding this in set
Opportunity OldoppObj=Trigger.oldMap.get(oppObj.Id);
OppId.add(OldoppObj.id);
ContactId.add(OldoppObj.Investor__c);

}
else if(oppObj.Investor__c!=null && Trigger.oldMap.get(oppObj.Id).Investor__c==null)
{
//Creating new contact role
newContactRoleList.add(new OpportunityContactRole (ContactId=oppObj.Investor__c, OpportunityId=oppObj.Id, Role='Decision Maker', IsPrimary = true));
}
}
}


try
{
//inserting new Contacts
if(newContactRoleList.size()>0) insert newContactRoleList;

//Selecting old contact roles
if (OppId.size()>0) oldContactRoleList=[Select Id from OpportunityContactRole where ContactId in : ContactId and OpportunityId in : OppId];

//Deleting old contact roles
if (oldContactRoleList.size()>0) delete oldContactRoleList;
}
catch(Exception e)
{
System.debug(e);
trigger.new[0].addError('Technical error occurred. Please contact to your system administrator or try after some time.');

}




}

Test Class:
 
@isTest(SeeAllData=true)

public class YourTriggerTestClass{
    static testMethod void yourTestMethod(){
    
        //fill all the required field and make sure your data passes the validation rules.
        //insert account
        account acc = new account(
        Name='test account');
        
        insert acc;
        
        //insert contact
        contact ct2 = new Contact(
        LastName='Tester',
        AccountId=acc.id);
        
        insert ct2;
        
        contact ct = new Contact(
        LastName='Tester',
        AccountId=acc.id);
        
        insert ct;
        
        //insert propert
        Property__c p = new Property__c(
        Name='test');
        
        insert p;
        
        //insert campaign
        Campaign camp = new Campaign(
        Name='test camp',
        IsActive=True,
        Property__c=p.Id);
        
        insert camp;
        
        
        //Insert Opportunity
        opportunity opp = new opportunity(
        Name = 'Test oppty',
        
        CampaignId=camp.id,
        StageName='Prospecting',
        CloseDate=system.today(),
        AccountId=acc.id
        );
        
        insert opp;
        
        opp.Investor__r=ct2;
        
        update opp;
        
        
        
        }
        }

 
Hello All,

 I am trying to pass the Input field value from Visualforce page to apex controller class.
 Please let me know how to capture the value.

 Below is the code.

<apex:page standardController="Activity_Tracker__c" extensions="dependentPicklistController"> 
  <apex:messages style="color:red"></apex:messages>
    <apex:form >
        <apex:pageblock >
            <apex:pageblockSection >
               <apex:inputField value="{!Activity_Tracker__c.Category__c}" />
               <apex:inputField value="{!Activity_Tracker__c.Sub_Customers__c}" />
            </apex:pageblockSection>
        </apex:pageblock>
        

         <apex:commandButton action="{!search}" value="search"/>
         <apex:pageBlock >
         <apex:pageBlockTable value="{!records}" var="item">
                <apex:column value="{!item.Category__c}"/>
                <apex:column value="{!item.Sub_Customers__c}"/>
                <apex:column value="{!item.Status__c }"/>
           </apex:pageBlockTable>
         </apex:pageBlock>
    </apex:form>
</apex:page>


Controller --
public with sharing class dependentPicklistController{
    public List<Activity_Tracker__c> records{get;set;}
    public Activity_Tracker__c  Category__c{get; set;}
 
    public pagereference search()
    {
       records=[Select ID, Category__c, Sub_Customers__c, Status__c From Activity_Tracker__c   ];
       system.debug('11111' +Query);
       return null;
    }
}

In this I am trying to capture Category__c, Sub_Customers__c (Dependent Picklists) in the Apex Class.

Thanks... 
Hello Team,

I have an APEX trigger but would like to get the LAST CALL DATE based on user.  So there could be 100 calls but if I called them 1/1/9015 and since I'm the user it will show the last call date of my activity.  I know it will change for everyone.  I create a field call My_Last_Call_Date__c.

Thoughts?
 
trigger CallDate on Task (after insert, after update, after delete) 
{
    Set<Id> con_set = new Set<Id>();
    List<Contact> con_list = new List<Contact>();
    for( Task T: Trigger.new )
    {
        if(String.valueof(T.whoid).startsWith('003') && T.Status=='Completed' && T.Subject=='Call' )
        {
            con_set.add(T.whoid);
        }
    }
     
     for(AggregateResult aggregateResult:[SELECT max(createdDate)MaxCDate,whoid FROM Task WHERE whoid IN: con_set AND Status ='Completed' AND (subject LIKE 'call%' OR subject LIKE 'outbound%') group By whoid])
     {
        con_list.add(new Contact(Id=(id)aggregateResult.get('whoid'),Last_Call__c=date.valueof(aggregateResult.get('MaxCDate'))));
     }
     
    try
    {
     
         if(con_list !=null && con_list.size()>0)
         {
             update con_list;
         }
     
    }Catch(Exception e){
         system.debug('Exception ***'+e.getMessage());
      
     }

}

thanks in advance
Hello Team,

I have 2 sample subject when an activity is being created. 
Sample 1: Manually create a call log and the word "Call" will automatically be in the subject.
Sample 2: When we do a "click to call" and it will automatically create a call log with "Call 12/29/2015 14:18:23 - Outbound" in the subject.  where date and time of course is based on the time called.

I have an Apex trigger but unfortunately it only work when a call log is manually entered. What code should I insert for the second sample to work?  

Thanks in advance
 
trigger CallDate on Task (after insert, after update, after delete) 
{
    Set<Id> con_set = new Set<Id>();
    List<Contact> con_list = new List<Contact>();
    for( Task T: Trigger.new )
    {
        if(String.valueof(T.whoid).startsWith('003') && T.Status=='Completed' && T.Subject=='Call' )
        {
            con_set.add(T.whoid);
        }
    }
     
     for(AggregateResult aggregateResult:[SELECT max(createdDate)MaxCDate,whoid FROM Task WHERE whoid IN: con_set AND Status ='Completed' AND (subject LIKE 'call%' OR subject LIKE 'outbound%') group By whoid])
     {
        con_list.add(new Contact(Id=(id)aggregateResult.get('whoid'),Last_Call__c=date.valueof(aggregateResult.get('MaxCDate'))));
     }
     
    try
    {
     
         if(con_list !=null && con_list.size()>0)
         {
             update con_list;
         }
     
    }Catch(Exception e){
         system.debug('Exception ***'+e.getMessage());
      
     }

}


These are the fields on my custom object
AUTO_ID(auto number) | version (text)
           1                           8.3.1
           2                           8.3.2
           3                           8.3.3
           4                           8.4.1

How can I display all the versions between 8.3.1 to 8.4.1.

As both the AUTO_ID and versions are of type string. Please help or suggest a best way to solve this problem.

 
  • September 24, 2015
  • Like
  • 0
How can i specify Multiple SetWhatID's ?
my intention is to send an email template which is using muliple fields from multiple Sobjects using Apex class.So inorder to do so, I have to use :setwhatId(Id of the sobject  whose fields are being using in template);
but as i said iam using multiple fileds from different Sobjects, So how to solve this issue ?
please help me out 
Hi,

I would like to know is there a way in salesforce to normalize a large, user-generated data-set of company names

We have user generated names of employers that come in all variations. For example

Google Google, Inc. Google Inc. Google inc

Does anyone have suggestions on how to normalize the existing entries, and also how to maintain we do it for all incoming names as well?
I am facing some issue reading CSV file from email attachment:

CSV attachments containing any special character gives me invalid BLOB error. ( My attachments contains  " é "  French character  and similar keywords.)
 
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();     
        if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {        
            for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
                Blob fileContent = email.binaryAttachments[i].body;
               String[] elements = fileContent.toString().split(';|\n');

 
Exception:    StringException: BLOB is not a valid UTF-8 string

Thanks,
Rahul
Hello Everyone,
I have a custom object which is related to case via lookup relation.
when case is created the status will be "New" .if the custom object has alteast one records,when the user should be able to change the status to "Ready ".if he tries to chnage with out any record it should prompt the error .

Thanks

Regards,
Jyo

 
Hello!

I have a trigger that is creating a contact role on the Opportunity when a lookup field is populated but I cannot get my code coverage above 66%!
What can I do to get better coverage?!
Trigger:
trigger addContactRole on Opportunity (after Insert, after update) {

List<OpportunityContactRole> newContactRoleList=new List<OpportunityContactRole>();
List<OpportunityContactRole> oldContactRoleList=new List<OpportunityContactRole>();
Set<Id> OppId=new Set<Id>();
Set<Id> ContactId=new Set<Id>();

for(Opportunity oppObj: Trigger.new)
{
//Insert condition
if(Trigger.isInsert)
{
if(oppObj.Investor__c!=null && Trigger.oldMap.get(oppObj.Id).Investor__c==null)
{
//Creating new contact role
newContactRoleList.add(new OpportunityContactRole (ContactId=oppObj.Investor__c,OpportunityId=oppObj.Id, Role='Decision Maker', IsPrimary = true));

}
}
else
{

if(oppObj.Investor__c==null && Trigger.oldMap.get(oppObj.Id).Investor__c!=null)
{
//Getting the contact and oppty Id from old values and adding this in set
Opportunity OldoppObj=Trigger.oldMap.get(oppObj.Id);
OppId.add(OldoppObj.id);
ContactId.add(OldoppObj.Investor__c);

}
else if(oppObj.Investor__c!=null && Trigger.oldMap.get(oppObj.Id).Investor__c==null)
{
//Creating new contact role
newContactRoleList.add(new OpportunityContactRole (ContactId=oppObj.Investor__c, OpportunityId=oppObj.Id, Role='Decision Maker', IsPrimary = true));
}
}
}


try
{
//inserting new Contacts
if(newContactRoleList.size()>0) insert newContactRoleList;

//Selecting old contact roles
if (OppId.size()>0) oldContactRoleList=[Select Id from OpportunityContactRole where ContactId in : ContactId and OpportunityId in : OppId];

//Deleting old contact roles
if (oldContactRoleList.size()>0) delete oldContactRoleList;
}
catch(Exception e)
{
System.debug(e);
trigger.new[0].addError('Technical error occurred. Please contact to your system administrator or try after some time.');

}




}

Test Class:
 
@isTest(SeeAllData=true)

public class YourTriggerTestClass{
    static testMethod void yourTestMethod(){
    
        //fill all the required field and make sure your data passes the validation rules.
        //insert account
        account acc = new account(
        Name='test account');
        
        insert acc;
        
        //insert contact
        contact ct2 = new Contact(
        LastName='Tester',
        AccountId=acc.id);
        
        insert ct2;
        
        contact ct = new Contact(
        LastName='Tester',
        AccountId=acc.id);
        
        insert ct;
        
        //insert propert
        Property__c p = new Property__c(
        Name='test');
        
        insert p;
        
        //insert campaign
        Campaign camp = new Campaign(
        Name='test camp',
        IsActive=True,
        Property__c=p.Id);
        
        insert camp;
        
        
        //Insert Opportunity
        opportunity opp = new opportunity(
        Name = 'Test oppty',
        
        CampaignId=camp.id,
        StageName='Prospecting',
        CloseDate=system.today(),
        AccountId=acc.id
        );
        
        insert opp;
        
        opp.Investor__r=ct2;
        
        update opp;
        
        
        
        }
        }

 
Hi Guys when I upsert records using data loader which consists of double quotes Phone__c="1111111111" the field is populating without ignoring the doublequotes i.e User-added image   Is it any way to ignore double quotes and load only the value which is in between the double quotes?
I have this lookup field created, but I cannot save due to the name field being required. How can I work around this? Looks like it may need a apex trigger using before update possibly?
Hi,

For one of the user for some reason there is an apostrophe in the dollar amount. Could you please help us where we need to make the change to show comma instead of apostrophe 

Thanks,
Basa
Hello All,

 I am trying to pass the Input field value from Visualforce page to apex controller class.
 Please let me know how to capture the value.

 Below is the code.

<apex:page standardController="Activity_Tracker__c" extensions="dependentPicklistController"> 
  <apex:messages style="color:red"></apex:messages>
    <apex:form >
        <apex:pageblock >
            <apex:pageblockSection >
               <apex:inputField value="{!Activity_Tracker__c.Category__c}" />
               <apex:inputField value="{!Activity_Tracker__c.Sub_Customers__c}" />
            </apex:pageblockSection>
        </apex:pageblock>
        

         <apex:commandButton action="{!search}" value="search"/>
         <apex:pageBlock >
         <apex:pageBlockTable value="{!records}" var="item">
                <apex:column value="{!item.Category__c}"/>
                <apex:column value="{!item.Sub_Customers__c}"/>
                <apex:column value="{!item.Status__c }"/>
           </apex:pageBlockTable>
         </apex:pageBlock>
    </apex:form>
</apex:page>


Controller --
public with sharing class dependentPicklistController{
    public List<Activity_Tracker__c> records{get;set;}
    public Activity_Tracker__c  Category__c{get; set;}
 
    public pagereference search()
    {
       records=[Select ID, Category__c, Sub_Customers__c, Status__c From Activity_Tracker__c   ];
       system.debug('11111' +Query);
       return null;
    }
}

In this I am trying to capture Category__c, Sub_Customers__c (Dependent Picklists) in the Apex Class.

Thanks... 

 

I need to have all attachments on case emails copied onto the case itself. 

 

Incoming emails with attachments that come in via Email-to-Case are dealt with by a trigger on attachment after insert.  This is working fine. 

 

However, outgoing emails with attachments that are sent from the case's 'Send an Email' button never trip the attachment trigger.  I have tried using a trigger on EmailAttachment, but it seems like the attachment isn't inserted until later, or is somehow otherwise unavailable.

 

As a simple test, I disabled everything but this:

 

trigger attachmentTest on Attachment (after insert)

{

    for (Attachment att : trigger.new)

    {

         System.debug('\n\n' + 'attachmentTest fired with Id ' + att.Id + '\n\n');

    }

}

 

I see the debug message for the incoming email with attachment, but never see it for the outgoing email with attachment.  Does anyone know a reason for this behavior?  Is there a workaround?

 

Thanks,

Steven

 

  • September 01, 2010
  • Like
  • 0