• Adda
  • NEWBIE
  • 25 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 14
    Questions
  • 32
    Replies
Hi All,

I have created a custom button on case feed that helps to change owner either to user or queue. Now, I need to include a send email notification checkbox in that visualforce page that will send email when case owner is changed same as standard SFDC Change Owner page.

Please can anyone help? Here is my visualforce and apex code.
 
apex:page tabStyle="Case" standardController="Case" extensions="ChangeCaseOwner" sidebar="false">

<apex:form id="formId">
<apex:includeScript value="/soap/ajax/26.0/connection.js"/>  
<apex:includeScript value="/support/console/26.0/integration.js"/>
<apex:inputHidden id="isInConsole" value="{!isInConsole}" />
<apex:sectionHeader title="Change Case Owner"/>
<!-- <p>This screen allows you to transfer cases from one user or queue to another. When you transfer ownership, the new owner will own:</p>
<ul><li>all open activities (tasks and events) for this case that are assigned to the current owner</li></ul>
<p>Note that completed activities will not be transferred. Open activities will not be transferred when assigning this case to a queue.</p> -->
<apex:pageBlock mode="Edit">
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockButtons location="bottom">
<apex:outputText rendered="{!shouldRedirect}">
                <script type="text/javascript">
                    window.top.location.href = '{!redirectUrl}';
                </script>
</apex:outputText>
<apex:commandButton value="Save" action="{!save}" />
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
<br/><apex:pageBlockSection title="Select New Owner" collapsible="false" columns="3">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Owner"></apex:outputLabel>
<apex:inputField value="{!Case.ownerId}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<script type="text/javascript">
    document.getElementById("j_id0:formId:isInConsole").value = sforce.console.isInConsole();
</script>

</apex:page>

Apex Code
public class ChangeCaseOwner {
    ApexPages.StandardController   controllerInstance;
    public String redirectUrl {public get; private set;}
    public Boolean shouldRedirect {public get; private set;}
    public String isInConsole{get; set;}
    public String browserUrlforRedirection{get;set;}
   
    public ChangeCaseOwner (ApexPages.StandardController controller) {
        controllerInstance = controller;
    } 
    
    public pageReference save () {
        controllerInstance.save();
        case caseRecord = (Case)controllerInstance.getRecord();
        shouldRedirect = true;
        system.debug('correctVal' + isInConsole);
        String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm();
        String url;
        if(isInConsole == 'false') {
            url = baseUrl + '/' + caseRecord.Id ;
            system.debug('inside false');
        }
        else{
            url = baseUrl + '/console' ;
        }
        system.debug('final url' + url);
        redirectUrl  = url;
        system.debug(redirectUrl);
        //redirectUrl = 'https://cs30.salesforce.com/console';
        return null;

    } 
    
    public pageReference cancel () {
        case caseRecord = (Case)controllerInstance.getRecord();
        shouldRedirect = true;
        system.debug('correctVal' + isInConsole);
        String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm();
        String url;
        if(isInConsole == 'false') {
            url = baseUrl + '/' + caseRecord.Id ;
        }
        else{
            url = baseUrl + '/console' ;
        }
        redirectUrl  = url;
        return null;

    } 

}


 
  • June 17, 2015
  • Like
  • 0
Hi All,

I am struggling with a apex class code coverage to get the code coverage upto 90%. The problem it is using a Case topic assignement functionality and sends email to different people when a topic is assigned to a case. The problem with code coverage I harded coded all the topic and wrote test class and Salesforce doesn't allow more than 10 email to fire for test class. I need to get this dynamic code coverage so that anytime I added a new topic is the code it doesn't need to hard coded the topic in the
Apex Class:

**/
public class TopicAssignmentTriggerHandler {

    
    public void sendEmailForTestimonial(List<TopicAssignment> topicAssignmentListNew) 
    {
    
        Map<Id, Topic> testimonialTopicIdsMappedTo = new Map<Id, Topic>([select Id,Name from Topic where (Name='Testimonial Feedback' OR 
        Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR
        Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' 
        OR Name='PR' OR Name='Employment Verification')]);        
        System.debug('testimonialTopicIdsMappedTo'+testimonialTopicIdsMappedTo  );
        //Create a master list to hold the emails we'll send
        List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
        Map<Id, Id> caseIdMappedToContactId = new Map<Id, Id>();
        Map<Id, Id> caseIdMappedWithTopicAssignmentId = new Map<Id, Id>();
        Map<Id, Contact> contactIdMappedToRecord = new Map<Id, Contact>();
        Set<ID> caseIdSet = new Set<ID>();
       
        if(testimonialTopicIdsMappedTo.size() > 0 ) 
        {
         for(TopicAssignment topicAssignment :topicAssignmentListNew)
         {
          if(testimonialTopicIdsMappedTo.Keyset().contains(topicAssignment.TopicId) && topicAssignment.EntityKeyPrefix=='500') {
            caseIdSet.add(topicAssignment.EntityId);
             if(caseIdMappedWithTopicAssignmentId.keyset() != null && !caseIdMappedWithTopicAssignmentId.keyset().contains(topicAssignment.EntityId)) 
                     {
                         caseIdMappedWithTopicAssignmentId.put(topicAssignment.EntityId,topicAssignment.TopicId);
                     }                     
                }    
            }  
        }
        
        for(case caseRecordForContact : [select contactId, Id from case where ID In:caseIdSet and contactId != null]) 
        {
            caseIdMappedToContactId.put(caseRecordForContact.Id, caseRecordForContact.ContactId); 
        }
        
        for(Contact contactRecord: [select Id, Name, Email from contact where Id In:caseIdMappedToContactId.values()]) 
        {
           contactIdMappedToRecord.put(contactRecord.Id,contactRecord); 
        }
        
        for(Case caseRecord : [select Id, CaseNumber,Description,Subject,ContactId from Case where Id IN:caseIdSet]) 
        {
           contact contactRec = new contact();
           contactRec =contactIdMappedToRecord.get(caseRecord.ContactId);
           System.debug('contactRec'+contactRec);
           Id TopicId = caseIdMappedWithTopicAssignmentId.get(caseRecord.Id);
           Topic TopicRecord = testimonialTopicIdsMappedTo.get(TopicId);
           System.debug('TopicRecord '+TopicRecord);
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();    
           String caselink = System.URL.getSalesforceBaseUrl().toExternalForm() +'/' +caseRecord.id;
           //Set list of people who should get the email
           List<String> sendTo = new List<String>();
           List<String> EmailList= new List<String>();
           //Dummy contact to avoid sending mails to Contact attached to Case
           Contact con = new Contact();
           
           if(TopicRecord.Name.Contains('Testimonial') || TopicRecord.Name.Contains('Testimonial Feedback') || 
           TopicRecord.Name.Contains('Author Feedback') || TopicRecord.Name.Contains('Testimonial') || 
           TopicRecord.Name.Contains('Course Feedback') || TopicRecord.Name.Contains('General Feedback') || 
           TopicRecord.Name.Contains('Course Request') || TopicRecord.Name.Contains('Feature Request')  )
            {    system.debug('###############Topic record'+TopicRecord);   
                 if(TopicRecord.Name.Equals('Testimonial'))
                 {     
                     Testimonial_Topic_Assignment_EmailId__c testimonialEmailIdRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Testimonial');
                     String e=testimonialEmailIdRecord.EmailAddress__c;
                     EmailList=SplitEmail(e);
                     for(String s:EmailList)
                     {
                        sendTo.add(s);
                     }
                     System.debug('sendTo'+sendTo);
                  }
                 else 
                    {
                        Testimonial_Topic_Assignment_EmailId__c FeedbackRequestRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Feedback/Request');
                        String e=FeedbackRequestRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                 
                
                 //Set email contents - you can use variables!
            mail.setSubject(''+ TopicRecord.Name +' Topic Added To Case Number ' + caseRecord.CaseNumber + '');
            
            String body = 'Hi ,' + '<br/><br/>';
            
            body += 'Case topic has been added to the following case.'+'<br/><br/>';
            body += 'Topic Name : ' + TopicRecord.Name + '<br/>';
            body += 'Case Number : ' + caseRecord.CaseNumber + '<br/>';
            
            if(contactRec != null) 
            {
                body += '&nbsp;&nbsp;&nbsp;&nbsp;Case Contact Name : ' + (contactRec.Name==null?'':contactRec.Name) + '<br/>';  
                body += '&nbsp;&nbsp;&nbsp;&nbsp;Contact Email Id : ' + (contactRec.Email==null?'':contactRec.Email) + '<br/>'; 
            }
            
            
          //  body += '&nbsp;&nbsp;&nbsp;&nbsp;Case CaseNumber: ' + (caseRecord.CaseNumber==null?'':caseRecord.CaseNumber) + '<br/>'; 
            body += 'Case Subject : ' + (caseRecord.Subject==null?'':caseRecord.Subject) + '<br/>';
            body += 'Case Description : ' + (caseRecord.Description==null?'':caseRecord.Description) + '</br><br/>';
            body += 'Case Link :' + '<a href="' + caselink  + '">Case Link</a>'  + '<br/></br>';
          
                               
            
            body += 'Thanks,' + '</br></br/>'; 
            body += 'Lynda.com' + '</br></br>';
            
             mail.setHtmlBody(body);
             mail.setToAddresses(sendTo);
             mails.add(mail);
            }
          
            else if(TopicRecord.Name.Contains('Community Relations') || TopicRecord.Name.Contains('Legal') 
            || TopicRecord.Name.Contains('Remove From Newsletter') || TopicRecord.Name.Contains('Piracy') 
            || TopicRecord.Name.Contains('PR') || TopicRecord.Name.Contains('Employment Verification') 
            || TopicRecord.Name.Contains('Careers') )
            {    system.debug('###############Topic record'+TopicRecord);    
                 List<Testimonial_Topic_Assignment_EmailId__c> AllEmailIdRecordList= Testimonial_Topic_Assignment_EmailId__c.getall().values();
                 
                 System.debug('AllEmailIdRecordList'+AllEmailIdRecordList);
                    if(TopicRecord.Name.Contains('Community Relations'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c communityRelationRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Community Relations');
                        String e=communityRelationRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Legal'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c LegalRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Legal');
                        String e=LegalRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Remove From Newsletter'))
                    {
                        System.debug('TopicRecord.Name'+TopicRecord.Name);
                        Testimonial_Topic_Assignment_EmailId__c RemovefromnewslettersRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Remove From Newsletter');
                        System.debug('RemovefromnewslettersRecord'+RemovefromnewslettersRecord);
                        String e=RemovefromnewslettersRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Piracy'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c PiracyRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Piracy');
                        String e=PiracyRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('PR'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c PRRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('PR');
                        String e=PRRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Employment Verification'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c EmploymentverificationRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Employment Verification');
                        String e=EmploymentverificationRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Careers'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c CareersRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Careers');
                        String e=CareersRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    
                
                 mail.setToAddresses(sendTo);
                
            system.debug(sendTo);  
            //Set email contents
            mail.setSubject('Case Alert from Salesforce');   
            String body = 'Hello ,' + '<br/><br/>'; 
            body += 'Customer Service received the email below, and we wanted to send it your way for review. The case is now closed, but please let us know if we may be of assistance regarding this issue.'+'<br/><br/>' ;
            body += 'Thank you, and have a great day!'+'<br/><br/>'; 
            body += 'Best Regards,'+'<br/>';
            body += 'Customer Service Team '+'<br/><br/>';

            body += 'Topic Name : ' + TopicRecord.Name + '<br/>';
            body += 'Case Number : ' + caseRecord.CaseNumber + '<br/>';
            body += 'Email Subject : ' + caseRecord.Subject + '<br/>';
            body += 'Email Description : ' + caseRecord.Description + '<br/>';
            body += '<a href="' + caselink  + '">Case Link</a>'  + '<br/>';
                              
            mail.setHtmlBody(body);
            //Add your email to the master list
            mails.add(mail);  
        }
        system.debug('total Emails' + mails);
        
        }
        Messaging.sendEmail(mails);
        }
        public List<String> SplitEmail(String Emails){
        List<String> EmailIdList =Emails.split(',');
        System.debug('EmailIdList '+EmailIdList );
        return EmailIdList;
    }
    
}
 
Test Class:

  

@isTest(SeeAllData=true)
public class TopicAssignmentTriggerHandlerTest{

    public static testmethod void AssignmentTriggerHandlertest(){
        Test.startTest();
          
          List <Topic> TopicList=[Select Id,Name from Topic where (Name='Testimonial Feedback' OR Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' OR Name='PR' OR Name='Employment Verification')];          
          List<TopicAssignment> TopicAssignmentList=new List<TopicAssignment>();
          List<Case> CaseList=new List<Case>();
          
         //create test Account
          Account testAcc = new Account(
          Name = 'testAccount',
          BillingCountryCode = 'US',
          BillingState = 'California',
          BillingStreet = '6410 via Real',
          BillingPostalCode = '93013',
          BillingCity = 'Carpinteria',
          Industry = 'Construction',
          LDC_ID__c =  1234);
          insert testAcc ;
          System.Assertequals(testAcc.Name,'testAccount');
          
          //Create contact data
          Contact c1 = new Contact(AccountId = testAcc.id, FirstName = 'Test', LastName = 'Contact', Email='c-aray@lynda.com');
          c1.MailingState = 'California';
          c1.MailingCountry = 'United States';
          insert c1;
          System.Assertequals(c1.FirstName,'Test');
          System.Assertequals(c1.LastName,'Contact');
          
                   
          //create case
          Case testcas1 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas1;
          TopicAssignment ta1 = new TopicAssignment(Topicid = TopicList.get(0).id, EntityId = testcas1.id);
          insert ta1;
          
          Case testcas2 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas2;
          TopicAssignment ta2 = new TopicAssignment(Topicid = TopicList.get(1).id, EntityId = testcas2.id);
          insert ta2;
          
          Case testcas3 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas3;
          TopicAssignment ta3 = new TopicAssignment(Topicid = TopicList.get(2).id, EntityId = testcas3.id);
          insert ta3;
          
/*          Case testcas4 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas4;
          TopicAssignment ta4 = new TopicAssignment(Topicid = TopicList.get(3).id, EntityId = testcas4.id);
          insert ta4;
          
          Case testcas5 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas5;
          TopicAssignment ta5 = new TopicAssignment(Topicid = TopicList.get(4).id, EntityId = testcas5.id);
          insert ta5;
         
          Case testcas6 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas6;
          TopicAssignment ta6 = new TopicAssignment(Topicid = TopicList.get(5).id, EntityId = testcas6.id);
          insert ta6;
          
          Case testcas7 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas7;
          TopicAssignment ta7 = new TopicAssignment(Topicid = TopicList.get(6).id, EntityId = testcas7.id);
          insert ta7;
          
          Case testcas8 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas8;
          TopicAssignment ta8 = new TopicAssignment(Topicid = TopicList.get(7).id, EntityId = testcas8.id);
          insert ta8;
          
          Case testcas9 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service' );
          insert testcas9 ;
          TopicAssignment ta9 = new TopicAssignment(Topicid = TopicList.get(8).id, EntityId = testcas9.id);
          insert ta9;
          
          Case testcas10 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas10 ;
          TopicAssignment ta10 = new TopicAssignment(Topicid = TopicList.get(9).id, EntityId = testcas10.id);
          insert ta10;
        
  */             
        
          
          
         Test.stopTest();
    }
}

test class. Can anyone please help? I am posting apex class and test class.

 
  • June 10, 2015
  • Like
  • 0
Hi,

I have a javascript which gets the next case assigned to user who is logged in. If the case owner is a queue, by clicking the get next case, the case is assigned to that user. What I want is to prevent the user to change the owner from queue to him/her if the condition says the user is out of office = True. Here is my code. I don't how to insert the ischange(owner) function in javascript.
{!REQUIRESCRIPT("/soap/ajax/18.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/18.0/apex.js")} 
{!REQUIRESCRIPT("/xdomain/xdomain.js")}
{!REQUIRESCRIPT("/support/console/30.0/integration.js")} 

var id = sforce.apex.execute("RetrieveNextUtils","retrieveNextCase",{userId:""}); 

// Prevent the user to change the owner from queue to him/her if the condition says the user is out of office = True//
if ( '{!Case.Agent_Out_of_Office__c}' == true ){
   alert ('Next case cannot be assigned because you are marked as out of office.');
     }

else if (id!='') { 
var querystringParams = ""; 
if (window.location.href.indexOf("isdtp=mn")!=-1) { 
querystringParams = "?isdtp=mn"; 
} else if (window.location.href.indexOf("isdtp=vw")!=-1) { 
querystringParams = "?isdtp=vw"; 
} 
//We have successfully retrieved a case 
if (sforce.console.isInConsole()) { 
sforce.console.openPrimaryTab(null, '/'+id + querystringParams, true, null, null, null);
} else { 
navigateToUrl('/'+id + querystringParams);
}
} else { 
alert('No cases are available at this time.'); 
}

Can anyone help please?
  • June 04, 2015
  • Like
  • 0
Hi, 

I need to pass one list view called "my open cases"  in the visuaforce page that will ony show open cases of the who logged in. I mean if I login I should see only my open cases but if bob logs in, he should see only his open case. I started with something but couldn't figure it out. Here is my VFP and apex code. Please help

VFP:
<apex:page controller="retrieveCase" tabStyle="Case">
    <apex:pageBlock >
        My Open Cases
        <apex:pageBlockTable value="{!cases}" var="c" rows="50" id="cases_table" > 
            <apex:column >
              <a target="_blank" href="/{!c.id}">{!c.casenumber}</a>
              <apex:facet name="header">Case Number</apex:facet>
          </apex:column>     
          <apex:column >
             <a target="_blank" href="/{!c.accountid}">{!c.account.name}</a>
             <apex:facet name="header">Account Name</apex:facet>
          </apex:column>        
            <apex:column value="{!c.status}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Apex Class:
 
public class retrieveCase {

    public String getContactName() {
        return 'My Open';
    }

    public List<Case> getCases() {
        return [SELECT casenumber, account.name,status FROM Case
                WHERE Owner.name = 'Username' AND status != 'Open' limit 20];
    }
}

 
  • May 06, 2015
  • Like
  • 0
I am recieving the following error when I try to deploy my apex class and test class in production.
Error message: TopicAssignmentTriggerHandlerTest.AssignmentTriggerHandlertest(), Details: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TriggeronTopicAssignment: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Class.TopicAssignmentTriggerHandler.sendEmailForTestimonial: line 84, column 1 Trigger.TriggeronTopicAssignment: line 11, column 1: [] Class.TopicAssignmentTriggerHandlerTest.AssignmentTriggerHandlertest: line 47, column 1

How I can fix it? I am attaching my code. Please help
Trigger:

trigger TriggeronTopicAssignment on TopicAssignment (after insert)
{    
    if(Trigger.isAfter && Trigger.isInsert) {
        TopicAssignmentTriggerHandler TopicAssignmentTriggerHandlerInstance = new TopicAssignmentTriggerHandler();
        TopicAssignmentTriggerHandlerInstance.sendEmailForTestimonial(Trigger.New);
    }
}

Apex Class:
/**
 **
 ** This helper class sends mail for the testimonial topic to certain user when any topic assignment on case is created
**/
public class TopicAssignmentTriggerHandler {

    
    public void sendEmailForTestimonial(List<TopicAssignment> topicAssignmentListNew) 
    {
    
        Map<Id, Topic> testimonialTopicIdsMappedTo = new Map<Id, Topic>([select Id,Name from Topic where (Name='Testimonial Feedback' OR 
        Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR
        Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' 
        OR Name='PR' OR Name='Employment Verification')]);        
        System.debug('testimonialTopicIdsMappedTo'+testimonialTopicIdsMappedTo  );
        //Create a master list to hold the emails we'll send
        List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
        Map<Id, Id> caseIdMappedToContactId = new Map<Id, Id>();
        Map<Id, Id> caseIdMappedWithTopicAssignmentId = new Map<Id, Id>();
        Map<Id, Contact> contactIdMappedToRecord = new Map<Id, Contact>();
        Set<ID> caseIdSet = new Set<ID>();
       
        if(testimonialTopicIdsMappedTo.size() > 0 ) 
        {
         for(TopicAssignment topicAssignment :topicAssignmentListNew)
         {
          if(testimonialTopicIdsMappedTo.Keyset().contains(topicAssignment.TopicId) && topicAssignment.EntityKeyPrefix=='500') {
            caseIdSet.add(topicAssignment.EntityId);
             if(caseIdMappedWithTopicAssignmentId.keyset() != null && !caseIdMappedWithTopicAssignmentId.keyset().contains(topicAssignment.EntityId)) 
                     {
                         caseIdMappedWithTopicAssignmentId.put(topicAssignment.EntityId,topicAssignment.TopicId);
                     }                     
                }    
            }  
        }
        
        for(case caseRecordForContact : [select contactId, Id from case where ID In:caseIdSet and contactId != null]) 
        {
            caseIdMappedToContactId.put(caseRecordForContact.Id, caseRecordForContact.ContactId); 
        }
        
        for(Contact contactRecord: [select Id, Name, Email from contact where Id In:caseIdMappedToContactId.values()]) 
        {
           contactIdMappedToRecord.put(contactRecord.Id,contactRecord); 
        }
        
        for(Case caseRecord : [select Id, CaseNumber,Description,Subject,ContactId from Case where Id IN:caseIdSet]) 
        {
           contact contactRec = new contact();
           contactRec =contactIdMappedToRecord.get(caseRecord.ContactId);
           System.debug('contactRec'+contactRec);
           Id TopicId = caseIdMappedWithTopicAssignmentId.get(caseRecord.Id);
           Topic TopicRecord = testimonialTopicIdsMappedTo.get(TopicId);
           System.debug('TopicRecord '+TopicRecord);
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();    
           String caselink = System.URL.getSalesforceBaseUrl().toExternalForm() +'/' +caseRecord.id;
           //Set list of people who should get the email
           List<String> sendTo = new List<String>();
           List<String> EmailList= new List<String>();
           //Dummy contact to avoid sending mails to Contact attached to Case
           Contact con = new Contact();
           
           if(TopicRecord.Name.Contains('Testimonial') || TopicRecord.Name.Contains('Testimonial Feedback') || 
           TopicRecord.Name.Contains('Author Feedback') || TopicRecord.Name.Contains('Testimonial') || 
           TopicRecord.Name.Contains('Course Feedback') || TopicRecord.Name.Contains('General Feedback') || 
           TopicRecord.Name.Contains('Course Request') || TopicRecord.Name.Contains('Feature Request')  )
            {    system.debug('###############Topic record'+TopicRecord);   
                 if(TopicRecord.Name.Equals('Testimonial'))
                 {     
                     Testimonial_Topic_Assignment_EmailId__c testimonialEmailIdRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Testimonial');
                     String e=testimonialEmailIdRecord.EmailAddress__c;
                     EmailList=SplitEmail(e);
                     for(String s:EmailList)
                     {
                        sendTo.add(s);
                     }
                     System.debug('sendTo'+sendTo);
                  }
                 else 
                    {
                        Testimonial_Topic_Assignment_EmailId__c FeedbackRequestRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Feedback/Request');
                        String e=FeedbackRequestRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                 
                
                 //Set email contents - you can use variables!
            mail.setSubject(''+ TopicRecord.Name +' Topic Added To Case Number ' + caseRecord.CaseNumber + '');
            
            String body = 'Hi ,' + '<br/><br/>';
            
            body += 'Case topic has been added to the following case.'+'<br/><br/>';
            body += 'Topic Name : ' + TopicRecord.Name + '<br/>';
            body += 'Case Number : ' + caseRecord.CaseNumber + '<br/>';
            
            if(contactRec != null) 
            {
                body += '&nbsp;&nbsp;&nbsp;&nbsp;Case Contact Name : ' + (contactRec.Name==null?'':contactRec.Name) + '<br/>';  
                body += '&nbsp;&nbsp;&nbsp;&nbsp;Contact Email Id : ' + (contactRec.Email==null?'':contactRec.Email) + '<br/>'; 
            }
            
            
          //  body += '&nbsp;&nbsp;&nbsp;&nbsp;Case CaseNumber: ' + (caseRecord.CaseNumber==null?'':caseRecord.CaseNumber) + '<br/>'; 
            body += 'Case Subject : ' + (caseRecord.Subject==null?'':caseRecord.Subject) + '<br/>';
            body += 'Case Description : ' + (caseRecord.Description==null?'':caseRecord.Description) + '</br>';
            body += '<a href="' + caselink  + '">Case Link</a>'  + '<br/><br/>';
            
            
            body += 'Thanks,' + '</br>'; 
            body += 'Lynda.com';
            
             mail.setHtmlBody(body);
             mail.setToAddresses(sendTo);
             mails.add(mail);
            }
          
            else if(TopicRecord.Name.Contains('Community Relations') || TopicRecord.Name.Contains('Legal') 
            || TopicRecord.Name.Contains('Remove From Newsletter') || TopicRecord.Name.Contains('Piracy') 
            || TopicRecord.Name.Contains('PR') || TopicRecord.Name.Contains('Employment Verification') 
            || TopicRecord.Name.Contains('Careers') )
            {    system.debug('###############Topic record'+TopicRecord);    
                 List<Testimonial_Topic_Assignment_EmailId__c> AllEmailIdRecordList= Testimonial_Topic_Assignment_EmailId__c.getall().values();
                 
                 System.debug('AllEmailIdRecordList'+AllEmailIdRecordList);
                    if(TopicRecord.Name.Contains('Community Relations'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c communityRelationRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Community Relations');
                        String e=communityRelationRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Legal'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c LegalRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Legal');
                        String e=LegalRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Remove From Newsletter'))
                    {
                        System.debug('TopicRecord.Name'+TopicRecord.Name);
                        Testimonial_Topic_Assignment_EmailId__c RemovefromnewslettersRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Remove From Newsletter');
                        System.debug('RemovefromnewslettersRecord'+RemovefromnewslettersRecord);
                        String e=RemovefromnewslettersRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Piracy'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c PiracyRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Piracy');
                        String e=PiracyRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('PR'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c PRRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('PR');
                        String e=PRRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Employment Verification'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c EmploymentverificationRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Employment Verification');
                        String e=EmploymentverificationRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Careers'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c CareersRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Careers');
                        String e=CareersRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    
                
                 mail.setToAddresses(sendTo);
                
            system.debug(sendTo);  
            //Set email contents
            mail.setSubject('Case Alert from Salesforce');   
            String body = 'Hello ,' + '<br/><br/>'; 
            body += 'Customer Service received the email below, and we wanted to send it your way for review. The case is now closed, but please let us know if we may be of assistance regarding this issue.'+'<br/><br/>' ;
            body += 'Thank you, and have a great day!'+'<br/><br/>'; 
            body += 'Best Regards,'+'<br/>';
            body += 'Customer Service Team '+'<br/><br/>';

            body += 'Topic Name : ' + TopicRecord.Name + '<br/>';
            body += 'Case Number : ' + caseRecord.CaseNumber + '<br/>';
            body += 'Email Subject : ' + caseRecord.Subject + '<br/>';
            body += 'Email Description : ' + caseRecord.Description + '<br/>';
            body += '<a href="' + caselink  + '">Case Link</a>'  + '<br/>';
                              
            mail.setHtmlBody(body);
            //Add your email to the master list
            mails.add(mail);  
        }
        system.debug('total Emails' + mails);
        
        }
        Messaging.sendEmail(mails);
        }
        public List<String> SplitEmail(String Emails){
        List<String> EmailIdList =Emails.split(',');
        System.debug('EmailIdList '+EmailIdList );
        return EmailIdList;
    }
    
}

Apex Test Class:
 
/**}
 ** @author JadeGlobal Inc
 ** @created 4-Feb/2015
 **
 ** Test class for TopicAssignmentTriggerHandler.cls
**/


  

@isTest(SeeAllData=true)
public class TopicAssignmentTriggerHandlerTest{

    public static testmethod void AssignmentTriggerHandlertest(){
        Test.startTest();
          
          List <Topic> TopicList=[Select Id,Name from Topic where (Name='Testimonial Feedback' OR Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' OR Name='PR' OR Name='Employment Verification')];          
          List<TopicAssignment> TopicAssignmentList=new List<TopicAssignment>();
          List<Case> CaseList=new List<Case>();
          
         //create test Account
          Account testAcc = new Account(
          Name = 'testAccount',
          BillingCountryCode = 'US',
          BillingState = 'California',
          BillingStreet = '6410 via Real',
          BillingPostalCode = '93013',
          BillingCity = 'Carpinteria',
          Industry = 'Construction',
          LDC_ID__c =  1234);
          insert testAcc ;
          System.Assertequals(testAcc.Name,'testAccount');
          
          //Create contact data
          Contact c1 = new Contact(AccountId = testAcc.id, FirstName = 'Test', LastName = 'Contact', Email='adda@gmail.com');
          c1.MailingState = 'California';
          c1.MailingCountry = 'United States';
          insert c1;
          System.Assertequals(c1.FirstName,'Test');
          System.Assertequals(c1.LastName,'Contact');
          
                   
          //create case
          Case testcas1 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas1;
          TopicAssignment ta1 = new TopicAssignment(Topicid = TopicList.get(0).id, EntityId = testcas1.id);
          insert ta1;
          
          Case testcas2 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas2;
          TopicAssignment ta2 = new TopicAssignment(Topicid = TopicList.get(1).id, EntityId = testcas2.id);
          insert ta2;
          
          Case testcas3 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas3;
          TopicAssignment ta3 = new TopicAssignment(Topicid = TopicList.get(2).id, EntityId = testcas3.id);
          insert ta3;
          
          Case testcas4 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas4;
          TopicAssignment ta4 = new TopicAssignment(Topicid = TopicList.get(3).id, EntityId = testcas4.id);
          insert ta4;
          
          Case testcas5 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas5;
          TopicAssignment ta5 = new TopicAssignment(Topicid = TopicList.get(4).id, EntityId = testcas5.id);
          insert ta5;
         
          Case testcas6 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas6;
          TopicAssignment ta6 = new TopicAssignment(Topicid = TopicList.get(5).id, EntityId = testcas6.id);
          insert ta6;
          
          Case testcas7 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas7;
          TopicAssignment ta7 = new TopicAssignment(Topicid = TopicList.get(6).id, EntityId = testcas7.id);
          insert ta7;
          
          Case testcas8 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas8;
          TopicAssignment ta8 = new TopicAssignment(Topicid = TopicList.get(7).id, EntityId = testcas8.id);
          insert ta8;
          
          Case testcas9 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas9 ;
          TopicAssignment ta9 = new TopicAssignment(Topicid = TopicList.get(8).id, EntityId = testcas9.id);
          insert ta9;
          
          Case testcas10 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas10 ;
          TopicAssignment ta10 = new TopicAssignment(Topicid = TopicList.get(9).id, EntityId = testcas10.id);
          insert ta10;
        
               
        
          
          
         Test.stopTest();
    }
}

 
  • May 06, 2015
  • Like
  • 0
Hi, I have written a trigger with apex on Case Topic assignment but I only managed to get the code coverage upto 80%. Please help in getting the code coverage upto 90% although the code is working fine. 
/**}

 ** Test class for TopicAssignmentTriggerHandler.cls
**/


  

@isTest
public class TopicAssignmentTriggerHandlerTest{

    public static testmethod void AssignmentTriggerHandlertest(){
        Test.startTest();
          
          List <Topic> TopicList=[Select Id,Name from Topic where (Name='Testimonial Feedback' OR Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' OR Name='PR' OR Name='Employment Verification')];          
          List<TopicAssignment> TopicAssignmentList=new List<TopicAssignment>();
          List<Case> CaseList=new List<Case>();
          
         //create test Account
          Account testAcc = new Account(
          Name = 'testAccount',
          BillingCountryCode = 'US',
          BillingState = 'California',
          BillingStreet = '6410 via Real',
          BillingPostalCode = '93013',
          BillingCity = 'Carpinteria',
          Industry = 'Construction',
          LDC_ID__c =  1234);
          insert testAcc ;
          System.Assertequals(testAcc.Name,'testAccount');
          
          //Create contact data
          Contact c1 = new Contact(AccountId = testAcc.id, FirstName = 'Test', LastName = 'Contact', Email='c-aray@lynda.com');
          c1.MailingState = 'California';
          c1.MailingCountry = 'United States';
          insert c1;
          System.Assertequals(c1.FirstName,'Test');
          System.Assertequals(c1.LastName,'Contact');
          
          //create case
          Case testcas1 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas1;
        
          TopicAssignment ta1 = new TopicAssignment(Topicid = TopicList.get(0).id, EntityId = testcas1.id);
          insert ta1;
          Case testcas2 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas2;
          TopicAssignment ta2 = new TopicAssignment(Topicid = TopicList.get(1).id, EntityId = testcas2.id);
          insert ta2;
          Case testcas3 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas3;
          TopicAssignment ta3 = new TopicAssignment(Topicid = TopicList.get(2).id, EntityId = testcas3.id);
          insert ta3;
          
          Case testcas4 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas4;
          TopicAssignment ta4 = new TopicAssignment(Topicid = TopicList.get(3).id, EntityId = testcas4.id);
          insert ta4;
          Case testcas5 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas5;
          TopicAssignment ta5 = new TopicAssignment(Topicid = TopicList.get(4).id, EntityId = testcas5.id);
          insert ta5;
         
          Case testcas6 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas6;
          TopicAssignment ta6 = new TopicAssignment(Topicid = TopicList.get(5).id, EntityId = testcas6.id);
          insert ta6;
          Case testcas7 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas7;
          TopicAssignment ta7 = new TopicAssignment(Topicid = TopicList.get(6).id, EntityId = testcas7.id);
          insert ta7;
          
          Case testcas8 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas8;
          TopicAssignment ta8 = new TopicAssignment(Topicid = TopicList.get(7).id, EntityId = testcas8.id);
          insert ta8;
          Case testcas9 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas9 ;
          TopicAssignment ta9 = new TopicAssignment(Topicid = TopicList.get(8).id, EntityId = testcas9.id);
          insert ta9;
          
          Case testcas10 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas10 ;
          TopicAssignment ta10 = new TopicAssignment(Topicid = TopicList.get(9).id, EntityId = testcas10.id);
          insert ta10;
          
          
         Test.stopTest();
    }
}
  • April 29, 2015
  • Like
  • 0
Hi All,

I have created a button that passes value from Case to Contact when we try create a new contact from case. I am able to pass value like account info, phone number from case to phone number of contact as it is one to one maping. But I am not able to pass Case ID from Case to Contact. Case related list. Bascially, when I create a contact from case automatically my button should attached that case in the contact. Here is my URL formula.

https://cs30.salesforce.com/003/e?retURL=%2F003n0000006N1WG&con4={!Case.Account}&con10={!Account.Phone}&{!Case.ContactId}={!Case.Id}&RecordType=012n00000008Wq0&ent=Case

Please help.

Thanks 
Adda
 
  • April 15, 2015
  • Like
  • 0
Hi, 

I have a unqiue problem. We are using Case Feed. When the account and contact is update on case we need a functionality to automate. Whenever a account is selected on lookup and user goes to contact lookup and if they don't find the contact they create a new contact. But nothing is prepopulated from account to contact. So, I am creating a formula field and making it available on lookup dialog. 

But the problem is I want to prepopulate some of the field like account name, account id into the new contact record using the formula as a URL hack. But I am not able to get the formula right. This is what I came up with.

HYPERLINK("https://cs30.salesforce.com/003/e?retURL=%%F001%2Fo&accid=Case.AccountId", "Create a Contact")

Please help. I am sorry I don't make this requirement understandable.

Thanks
Adda
  • April 08, 2015
  • Like
  • 0
Hi,

On the Case Feed view, user has to navigate to the Details view to be able to create a new article, but they should be able to create a new article from within the articles tool on the case feed view. Is this possible? If yes can you give me some pointer either using some action button or some URL hacking. 

Thanks
Adda
  • April 08, 2015
  • Like
  • 0
Hi,

I have written a fucntionality to convert lead to case. The only problem is I have a field on Lead as Web-to-Lead Notes which is Text Area long and that is being popualated on Case Subject on Conversion. It gives error if the length is big. So I need to truncate the length of Lead to Web Notes field to 50 to properly fit in the Case Subject. Please help. Here is my code VFP and Apex:

Visualforce Page:
<apex:page standardController="Lead" extensions="CaseConverter">
    <apex:form >
        <apex:pageBlock title="Convert information to a new Case.">
            <apex:pageBlockButtons >
                <apex:commandbutton action="{!createCase}" value="Finish"/>
                <apex:commandbutton action="{!returnToLead}" value="Previous"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Lead content Section">            
                <tr><th class="labelCol vfLabelColTextWrap " scope="row">Lead Record Type</th><td class="dataCol "><span id="j_id0:j_id1:j_id2:j_id6:nameId">{!recordTypeName}</span></td></tr>
                <apex:outputText value="{!lead.Web_To_Lead_Notes__c} " rendered="true"/>
                <apex:outputField value="{!lead.Name}" id="nameId"/>
                <apex:outputField value="{!lead.Company}" id="companyId"/>
                <apex:outputField value="{!lead.Phone}" id="phoneid"/>
                <apex:outputField value="{!lead.City}" id="cityId"/>
                <apex:outputField value="{!lead.Street}" id="streetId"/>
                <apex:outputField value="{!lead.Country}" id="countryid"/>
                <apex:outputField value="{!lead.Email}" id="emailid"/>
                <apex:outputField value="{!lead.PostalCode}" id="postalId"/>
                <apex:outputField value="{!lead.MobilePhone}" id="mobileId"/>
                <apex:outputField value="{!lead.Website}" id="websiteId"/>
                <apex:outputField value="{!lead.Description}" id="descriptionId"/>
                <apex:outputField value="{!lead.Industry}" id="industryId"/>                           
            </apex:pageBlockSection>
            
         
   Apex Class:
public with sharing class CaseConverter {
    public Lead curLead{set;get;}
    private final String name;
    private final String company;
    private final String phone;
    private final String city;
    private final String street;
    private final String country;
    private final String email;
    private final String postal;
    private final String mobile;
    private final String website;
    private final String description;
    private final String industry;
    private final String leadId;
    public String Web_To_Lead_Notes_Case;
    public string accountOwnerId;
    
    //getter,setter for the selected list values
    public String statusCaseSelected{get;set;}
    public String originSelected{get;set;}
    
    //getter,setter for the input fields
    public String subjectInput{get;set;}
    
    //getter,setter for the input fields
    public String recordTypeName{get;set;}
    
    //getter setter for the checkboxes
    public Boolean createAccount{get;set;}
    
    /*get the select options from the case system schema for the status field
    * Input: nothing
    * Output: List of the select options from case status
    */
    public List<Selectoption> getStatusCaseItems(){
        List<Selectoption> statusValues = new List<Selectoption>();
        Schema.Describefieldresult systemCaseStatus = Case.Status.getDescribe();
        for(Schema.Picklistentry plEntry : systemCaseStatus.getPicklistValues()){
            statusValues.add(
                new Selectoption(
                    plEntry.getValue(),
                    plEntry.getLabel()
                )
            );
        }
        return statusValues;
    }
    
    /*get the select options from the case system schema for the origin field
    * Input: nothing
    * Output: List of the select options from case origin
    */
    public List<Selectoption> getOriginItems(){
        List<Selectoption> originValues = new List<Selectoption>();
                 originValues.add(
                       new Selectoption(
                                'Customer Service','Customer Service'
                )
           );
                
        
        return originValues;
    }

    //constructor, this page will be called from the LeadToCase Page
    public CaseConverter(ApexPages.StandardController controller) {
        this.curLead= (Lead)controller.getRecord();
        List<QueueSobject> lstQueues = [SELECT Id,queue.Name, QueueId FROM QueueSobject WHERE SobjectType = 'Case'and queue.Name = 'Customer Service Cases'];
        curLead.OwnerId = lstQueues[0].QueueId;
        
        List<recordType> recordTypeList = [select Name from recordType where Id = :curlead.recordTypeId];
        if(recordTypeList.Size() > 0 ) {
            recordTypeName = recordTypeList[0].Name;
        } 
        this.name = curLead.FirstName + ' ' + curLead.LastName;
        this.company = curLead.Company;
        this.phone = curLead.Phone;
        this.city = curLead.City;
        this.street = curLead.Street;
        this.country = curLead.Country;
        this.email = curLead.Email;
        this.postal = curLead.PostalCode;
        this.mobile = curLead.MobilePhone;
        this.website = curLead.Website;
        this.description = curLead.Description;
        this.industry = curLead.Industry;
        this.leadId = curLead.Id; 
        this.Web_To_Lead_Notes_Case = curLead.Web_To_Lead_Notes__c;
        subjectInput = curLead.Web_To_Lead_Notes__c; 
       
    }
    
    
    
  • April 03, 2015
  • Like
  • 0
Hi All,

I am skeptical if it is possible. Account has many cases. Is there any way if under a case a article is attached, can we show that article on account as a related list. For example Account A has Case 1,2 and 3 and if Case 1 has article as a,b and case 2 has article as x,y. Then account show related list of article as a,b,x and y. Is it possible as it doesn't seem there is a relationship between  account and article? Let me know 

Thanks
A
  • March 30, 2015
  • Like
  • 0
HI All,

I have 2 requirement. First is to create a button that converts a lead to case. I completed this requirement. Second requirement is related to same lead to case conversion. Customers needs to perform the same activity as a mass Lead to case conversion. As per SFDC standard, mass lead conversion to account, contact and oppty is not possible. But can we do mass coversion from lead to case. Did anyone do that? Please send me the code if you can?

Thanks
~ A
  • March 18, 2015
  • Like
  • 0
Hi All,

I need a help on this requirement when a customer sends an attachment to an email to case, the attachment remains in the email section but the client wants that the attachments should be automatically uploaded as a Case Attachment & not just related to the email message. Can we do that?

If yes, can you please  send me a working code? Thanks for help.

~ Abhi
  • March 18, 2015
  • Like
  • 0
Hi, I am getting error as ''System.AssertException: Assertion Failed'. Please help.

Apex Trigger: 

  trigger FirstActivityDate on Task (before insert, before update) {

    List<Id> leadIds=new List<Id>();
    List<Id> ContatcIds=new List<Id>();
    for(Task t:trigger.new){
    
    // Checking the condition for the trigger to fire 
    
        if(t.Status=='Completed' && t.Created_by_Role__c == TRUE && (t.whoId != null)){
            if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){
                leadIds.add(t.whoId);
            }
            if(String.valueOf(t.whoId).startsWith('003')==TRUE){
                ContatcIds.add(t.whoId);
            }
        }
    }
    
    //Querying the related Lead and Contact based on whereId on Task
    
    List<Lead> leadsToUpdate=[SELECT Id, First_Activity_Date__c FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE AND First_Activity_Date__c=NULL];
    List<Contact> contactToUpdate=[SELECT Id, First_Activity_Date__c FROM Contact WHERE Id IN :ContatcIds AND First_Activity_Date__c=NULL];
    
     // updating the Lead and contact First Activity Date field
     
    For (Lead l:leadsToUpdate){
        l.First_Activity_Date__c = date.today();
    }
    For (Contact c:contactToUpdate){
        c.First_Activity_Date__c = date.today();
    }
    
   
    
    try{
        update leadsToUpdate;
          update contactToUpdate;
            }catch(DMLException e){
        system.debug('Leads were not all properly updated.  Error: '+e);
    }
}



Apex Class :

@isTest
public class FirstActivityDateTest {
    static testMethod void myUnitTest() {
        //Create a test Lead
        Lead ld = new Lead();
            ld.FirstName = 'TestFirstName';
            ld.LastName = 'TestLastName';
            ld.Company = 'TestCompany';
            ld.CountryCode = 'US';
        insert ld;
        //Assert that First_Activity_Date__c = Null
        System.assertEquals(ld.First_Activity_Date__c, null);

        //Create a test user
        list<Profile> p1 = [SELECT Id FROM Profile WHERE Name like 'System Administrator'];
        
        User u1 = new User();
            u1.FirstName = 'fName';
            u1.LastName = 'lName';
            u1.Email = 'Test1234567@mail.com';
            u1.Username = 'Test1234567@mail.com';
            u1.Alias ='Test123';
            u1.CommunityNickname = 't123';
            u1.TimeZoneSidKey ='America/New_York';
            u1.LocaleSidKey = 'en_US';
            u1.EmailEncodingKey = 'UTF-8';
            u1.ProfileId = p1[0].id;
            u1.LanguageLocaleKey = 'en_US';
            u1.Country = 'United Kingdom';
        insert u1;

        
        //Create a test Contact
        Contact ct = new Contact();
            ct.FirstName = 'TestFirstName';
            ct.LastName = 'TestLastName';
        insert ct;
       //Assert that First_Activity_Date__c = Null
        System.assertEquals(ct.First_Activity_Date__c, null);
 

        //Create a test Task
        Task tk = new Task();
            tk.Subject = 'TestSubject';
            tk.ActivityDate = date.today();
            tk.OwnerId = u1.Id;
            tk.Status = 'Completed';
            tk.WhoId = ld.Id;
        insert tk;
        
         //Create a test Task
        Task t= new Task();
            t.Subject = 'TestSubject';
            t.ActivityDate = date.today();
            t.OwnerId = u1.Id;
            t.Status = 'Completed';
            t.WhoId = ct.Id;
        insert t;

        //Assert that First_Activity_Date__c != null
        ld = [SELECT Id, First_Activity_Date__c FROM Lead WHERE Id = :ld.Id limit 1];
        System.assertEquals(ld.First_Activity_Date__c != null);
        
       
    }
}
  • December 22, 2014
  • Like
  • 0
Hi All,

I have created a custom button on case feed that helps to change owner either to user or queue. Now, I need to include a send email notification checkbox in that visualforce page that will send email when case owner is changed same as standard SFDC Change Owner page.

Please can anyone help? Here is my visualforce and apex code.
 
apex:page tabStyle="Case" standardController="Case" extensions="ChangeCaseOwner" sidebar="false">

<apex:form id="formId">
<apex:includeScript value="/soap/ajax/26.0/connection.js"/>  
<apex:includeScript value="/support/console/26.0/integration.js"/>
<apex:inputHidden id="isInConsole" value="{!isInConsole}" />
<apex:sectionHeader title="Change Case Owner"/>
<!-- <p>This screen allows you to transfer cases from one user or queue to another. When you transfer ownership, the new owner will own:</p>
<ul><li>all open activities (tasks and events) for this case that are assigned to the current owner</li></ul>
<p>Note that completed activities will not be transferred. Open activities will not be transferred when assigning this case to a queue.</p> -->
<apex:pageBlock mode="Edit">
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlockButtons location="bottom">
<apex:outputText rendered="{!shouldRedirect}">
                <script type="text/javascript">
                    window.top.location.href = '{!redirectUrl}';
                </script>
</apex:outputText>
<apex:commandButton value="Save" action="{!save}" />
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
<br/><apex:pageBlockSection title="Select New Owner" collapsible="false" columns="3">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Owner"></apex:outputLabel>
<apex:inputField value="{!Case.ownerId}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<script type="text/javascript">
    document.getElementById("j_id0:formId:isInConsole").value = sforce.console.isInConsole();
</script>

</apex:page>

Apex Code
public class ChangeCaseOwner {
    ApexPages.StandardController   controllerInstance;
    public String redirectUrl {public get; private set;}
    public Boolean shouldRedirect {public get; private set;}
    public String isInConsole{get; set;}
    public String browserUrlforRedirection{get;set;}
   
    public ChangeCaseOwner (ApexPages.StandardController controller) {
        controllerInstance = controller;
    } 
    
    public pageReference save () {
        controllerInstance.save();
        case caseRecord = (Case)controllerInstance.getRecord();
        shouldRedirect = true;
        system.debug('correctVal' + isInConsole);
        String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm();
        String url;
        if(isInConsole == 'false') {
            url = baseUrl + '/' + caseRecord.Id ;
            system.debug('inside false');
        }
        else{
            url = baseUrl + '/console' ;
        }
        system.debug('final url' + url);
        redirectUrl  = url;
        system.debug(redirectUrl);
        //redirectUrl = 'https://cs30.salesforce.com/console';
        return null;

    } 
    
    public pageReference cancel () {
        case caseRecord = (Case)controllerInstance.getRecord();
        shouldRedirect = true;
        system.debug('correctVal' + isInConsole);
        String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm();
        String url;
        if(isInConsole == 'false') {
            url = baseUrl + '/' + caseRecord.Id ;
        }
        else{
            url = baseUrl + '/console' ;
        }
        redirectUrl  = url;
        return null;

    } 

}


 
  • June 17, 2015
  • Like
  • 0
Hi All,

I am struggling with a apex class code coverage to get the code coverage upto 90%. The problem it is using a Case topic assignement functionality and sends email to different people when a topic is assigned to a case. The problem with code coverage I harded coded all the topic and wrote test class and Salesforce doesn't allow more than 10 email to fire for test class. I need to get this dynamic code coverage so that anytime I added a new topic is the code it doesn't need to hard coded the topic in the
Apex Class:

**/
public class TopicAssignmentTriggerHandler {

    
    public void sendEmailForTestimonial(List<TopicAssignment> topicAssignmentListNew) 
    {
    
        Map<Id, Topic> testimonialTopicIdsMappedTo = new Map<Id, Topic>([select Id,Name from Topic where (Name='Testimonial Feedback' OR 
        Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR
        Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' 
        OR Name='PR' OR Name='Employment Verification')]);        
        System.debug('testimonialTopicIdsMappedTo'+testimonialTopicIdsMappedTo  );
        //Create a master list to hold the emails we'll send
        List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
        Map<Id, Id> caseIdMappedToContactId = new Map<Id, Id>();
        Map<Id, Id> caseIdMappedWithTopicAssignmentId = new Map<Id, Id>();
        Map<Id, Contact> contactIdMappedToRecord = new Map<Id, Contact>();
        Set<ID> caseIdSet = new Set<ID>();
       
        if(testimonialTopicIdsMappedTo.size() > 0 ) 
        {
         for(TopicAssignment topicAssignment :topicAssignmentListNew)
         {
          if(testimonialTopicIdsMappedTo.Keyset().contains(topicAssignment.TopicId) && topicAssignment.EntityKeyPrefix=='500') {
            caseIdSet.add(topicAssignment.EntityId);
             if(caseIdMappedWithTopicAssignmentId.keyset() != null && !caseIdMappedWithTopicAssignmentId.keyset().contains(topicAssignment.EntityId)) 
                     {
                         caseIdMappedWithTopicAssignmentId.put(topicAssignment.EntityId,topicAssignment.TopicId);
                     }                     
                }    
            }  
        }
        
        for(case caseRecordForContact : [select contactId, Id from case where ID In:caseIdSet and contactId != null]) 
        {
            caseIdMappedToContactId.put(caseRecordForContact.Id, caseRecordForContact.ContactId); 
        }
        
        for(Contact contactRecord: [select Id, Name, Email from contact where Id In:caseIdMappedToContactId.values()]) 
        {
           contactIdMappedToRecord.put(contactRecord.Id,contactRecord); 
        }
        
        for(Case caseRecord : [select Id, CaseNumber,Description,Subject,ContactId from Case where Id IN:caseIdSet]) 
        {
           contact contactRec = new contact();
           contactRec =contactIdMappedToRecord.get(caseRecord.ContactId);
           System.debug('contactRec'+contactRec);
           Id TopicId = caseIdMappedWithTopicAssignmentId.get(caseRecord.Id);
           Topic TopicRecord = testimonialTopicIdsMappedTo.get(TopicId);
           System.debug('TopicRecord '+TopicRecord);
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();    
           String caselink = System.URL.getSalesforceBaseUrl().toExternalForm() +'/' +caseRecord.id;
           //Set list of people who should get the email
           List<String> sendTo = new List<String>();
           List<String> EmailList= new List<String>();
           //Dummy contact to avoid sending mails to Contact attached to Case
           Contact con = new Contact();
           
           if(TopicRecord.Name.Contains('Testimonial') || TopicRecord.Name.Contains('Testimonial Feedback') || 
           TopicRecord.Name.Contains('Author Feedback') || TopicRecord.Name.Contains('Testimonial') || 
           TopicRecord.Name.Contains('Course Feedback') || TopicRecord.Name.Contains('General Feedback') || 
           TopicRecord.Name.Contains('Course Request') || TopicRecord.Name.Contains('Feature Request')  )
            {    system.debug('###############Topic record'+TopicRecord);   
                 if(TopicRecord.Name.Equals('Testimonial'))
                 {     
                     Testimonial_Topic_Assignment_EmailId__c testimonialEmailIdRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Testimonial');
                     String e=testimonialEmailIdRecord.EmailAddress__c;
                     EmailList=SplitEmail(e);
                     for(String s:EmailList)
                     {
                        sendTo.add(s);
                     }
                     System.debug('sendTo'+sendTo);
                  }
                 else 
                    {
                        Testimonial_Topic_Assignment_EmailId__c FeedbackRequestRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Feedback/Request');
                        String e=FeedbackRequestRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                 
                
                 //Set email contents - you can use variables!
            mail.setSubject(''+ TopicRecord.Name +' Topic Added To Case Number ' + caseRecord.CaseNumber + '');
            
            String body = 'Hi ,' + '<br/><br/>';
            
            body += 'Case topic has been added to the following case.'+'<br/><br/>';
            body += 'Topic Name : ' + TopicRecord.Name + '<br/>';
            body += 'Case Number : ' + caseRecord.CaseNumber + '<br/>';
            
            if(contactRec != null) 
            {
                body += '&nbsp;&nbsp;&nbsp;&nbsp;Case Contact Name : ' + (contactRec.Name==null?'':contactRec.Name) + '<br/>';  
                body += '&nbsp;&nbsp;&nbsp;&nbsp;Contact Email Id : ' + (contactRec.Email==null?'':contactRec.Email) + '<br/>'; 
            }
            
            
          //  body += '&nbsp;&nbsp;&nbsp;&nbsp;Case CaseNumber: ' + (caseRecord.CaseNumber==null?'':caseRecord.CaseNumber) + '<br/>'; 
            body += 'Case Subject : ' + (caseRecord.Subject==null?'':caseRecord.Subject) + '<br/>';
            body += 'Case Description : ' + (caseRecord.Description==null?'':caseRecord.Description) + '</br><br/>';
            body += 'Case Link :' + '<a href="' + caselink  + '">Case Link</a>'  + '<br/></br>';
          
                               
            
            body += 'Thanks,' + '</br></br/>'; 
            body += 'Lynda.com' + '</br></br>';
            
             mail.setHtmlBody(body);
             mail.setToAddresses(sendTo);
             mails.add(mail);
            }
          
            else if(TopicRecord.Name.Contains('Community Relations') || TopicRecord.Name.Contains('Legal') 
            || TopicRecord.Name.Contains('Remove From Newsletter') || TopicRecord.Name.Contains('Piracy') 
            || TopicRecord.Name.Contains('PR') || TopicRecord.Name.Contains('Employment Verification') 
            || TopicRecord.Name.Contains('Careers') )
            {    system.debug('###############Topic record'+TopicRecord);    
                 List<Testimonial_Topic_Assignment_EmailId__c> AllEmailIdRecordList= Testimonial_Topic_Assignment_EmailId__c.getall().values();
                 
                 System.debug('AllEmailIdRecordList'+AllEmailIdRecordList);
                    if(TopicRecord.Name.Contains('Community Relations'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c communityRelationRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Community Relations');
                        String e=communityRelationRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Legal'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c LegalRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Legal');
                        String e=LegalRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Remove From Newsletter'))
                    {
                        System.debug('TopicRecord.Name'+TopicRecord.Name);
                        Testimonial_Topic_Assignment_EmailId__c RemovefromnewslettersRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Remove From Newsletter');
                        System.debug('RemovefromnewslettersRecord'+RemovefromnewslettersRecord);
                        String e=RemovefromnewslettersRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Piracy'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c PiracyRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Piracy');
                        String e=PiracyRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('PR'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c PRRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('PR');
                        String e=PRRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Employment Verification'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c EmploymentverificationRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Employment Verification');
                        String e=EmploymentverificationRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Careers'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c CareersRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Careers');
                        String e=CareersRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    
                
                 mail.setToAddresses(sendTo);
                
            system.debug(sendTo);  
            //Set email contents
            mail.setSubject('Case Alert from Salesforce');   
            String body = 'Hello ,' + '<br/><br/>'; 
            body += 'Customer Service received the email below, and we wanted to send it your way for review. The case is now closed, but please let us know if we may be of assistance regarding this issue.'+'<br/><br/>' ;
            body += 'Thank you, and have a great day!'+'<br/><br/>'; 
            body += 'Best Regards,'+'<br/>';
            body += 'Customer Service Team '+'<br/><br/>';

            body += 'Topic Name : ' + TopicRecord.Name + '<br/>';
            body += 'Case Number : ' + caseRecord.CaseNumber + '<br/>';
            body += 'Email Subject : ' + caseRecord.Subject + '<br/>';
            body += 'Email Description : ' + caseRecord.Description + '<br/>';
            body += '<a href="' + caselink  + '">Case Link</a>'  + '<br/>';
                              
            mail.setHtmlBody(body);
            //Add your email to the master list
            mails.add(mail);  
        }
        system.debug('total Emails' + mails);
        
        }
        Messaging.sendEmail(mails);
        }
        public List<String> SplitEmail(String Emails){
        List<String> EmailIdList =Emails.split(',');
        System.debug('EmailIdList '+EmailIdList );
        return EmailIdList;
    }
    
}
 
Test Class:

  

@isTest(SeeAllData=true)
public class TopicAssignmentTriggerHandlerTest{

    public static testmethod void AssignmentTriggerHandlertest(){
        Test.startTest();
          
          List <Topic> TopicList=[Select Id,Name from Topic where (Name='Testimonial Feedback' OR Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' OR Name='PR' OR Name='Employment Verification')];          
          List<TopicAssignment> TopicAssignmentList=new List<TopicAssignment>();
          List<Case> CaseList=new List<Case>();
          
         //create test Account
          Account testAcc = new Account(
          Name = 'testAccount',
          BillingCountryCode = 'US',
          BillingState = 'California',
          BillingStreet = '6410 via Real',
          BillingPostalCode = '93013',
          BillingCity = 'Carpinteria',
          Industry = 'Construction',
          LDC_ID__c =  1234);
          insert testAcc ;
          System.Assertequals(testAcc.Name,'testAccount');
          
          //Create contact data
          Contact c1 = new Contact(AccountId = testAcc.id, FirstName = 'Test', LastName = 'Contact', Email='c-aray@lynda.com');
          c1.MailingState = 'California';
          c1.MailingCountry = 'United States';
          insert c1;
          System.Assertequals(c1.FirstName,'Test');
          System.Assertequals(c1.LastName,'Contact');
          
                   
          //create case
          Case testcas1 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas1;
          TopicAssignment ta1 = new TopicAssignment(Topicid = TopicList.get(0).id, EntityId = testcas1.id);
          insert ta1;
          
          Case testcas2 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas2;
          TopicAssignment ta2 = new TopicAssignment(Topicid = TopicList.get(1).id, EntityId = testcas2.id);
          insert ta2;
          
          Case testcas3 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas3;
          TopicAssignment ta3 = new TopicAssignment(Topicid = TopicList.get(2).id, EntityId = testcas3.id);
          insert ta3;
          
/*          Case testcas4 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas4;
          TopicAssignment ta4 = new TopicAssignment(Topicid = TopicList.get(3).id, EntityId = testcas4.id);
          insert ta4;
          
          Case testcas5 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas5;
          TopicAssignment ta5 = new TopicAssignment(Topicid = TopicList.get(4).id, EntityId = testcas5.id);
          insert ta5;
         
          Case testcas6 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas6;
          TopicAssignment ta6 = new TopicAssignment(Topicid = TopicList.get(5).id, EntityId = testcas6.id);
          insert ta6;
          
          Case testcas7 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas7;
          TopicAssignment ta7 = new TopicAssignment(Topicid = TopicList.get(6).id, EntityId = testcas7.id);
          insert ta7;
          
          Case testcas8 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas8;
          TopicAssignment ta8 = new TopicAssignment(Topicid = TopicList.get(7).id, EntityId = testcas8.id);
          insert ta8;
          
          Case testcas9 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service' );
          insert testcas9 ;
          TopicAssignment ta9 = new TopicAssignment(Topicid = TopicList.get(8).id, EntityId = testcas9.id);
          insert ta9;
          
          Case testcas10 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service');
          insert testcas10 ;
          TopicAssignment ta10 = new TopicAssignment(Topicid = TopicList.get(9).id, EntityId = testcas10.id);
          insert ta10;
        
  */             
        
          
          
         Test.stopTest();
    }
}

test class. Can anyone please help? I am posting apex class and test class.

 
  • June 10, 2015
  • Like
  • 0
Hi,

I have a javascript which gets the next case assigned to user who is logged in. If the case owner is a queue, by clicking the get next case, the case is assigned to that user. What I want is to prevent the user to change the owner from queue to him/her if the condition says the user is out of office = True. Here is my code. I don't how to insert the ischange(owner) function in javascript.
{!REQUIRESCRIPT("/soap/ajax/18.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/18.0/apex.js")} 
{!REQUIRESCRIPT("/xdomain/xdomain.js")}
{!REQUIRESCRIPT("/support/console/30.0/integration.js")} 

var id = sforce.apex.execute("RetrieveNextUtils","retrieveNextCase",{userId:""}); 

// Prevent the user to change the owner from queue to him/her if the condition says the user is out of office = True//
if ( '{!Case.Agent_Out_of_Office__c}' == true ){
   alert ('Next case cannot be assigned because you are marked as out of office.');
     }

else if (id!='') { 
var querystringParams = ""; 
if (window.location.href.indexOf("isdtp=mn")!=-1) { 
querystringParams = "?isdtp=mn"; 
} else if (window.location.href.indexOf("isdtp=vw")!=-1) { 
querystringParams = "?isdtp=vw"; 
} 
//We have successfully retrieved a case 
if (sforce.console.isInConsole()) { 
sforce.console.openPrimaryTab(null, '/'+id + querystringParams, true, null, null, null);
} else { 
navigateToUrl('/'+id + querystringParams);
}
} else { 
alert('No cases are available at this time.'); 
}

Can anyone help please?
  • June 04, 2015
  • Like
  • 0
I am recieving the following error when I try to deploy my apex class and test class in production.
Error message: TopicAssignmentTriggerHandlerTest.AssignmentTriggerHandlertest(), Details: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TriggeronTopicAssignment: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Class.TopicAssignmentTriggerHandler.sendEmailForTestimonial: line 84, column 1 Trigger.TriggeronTopicAssignment: line 11, column 1: [] Class.TopicAssignmentTriggerHandlerTest.AssignmentTriggerHandlertest: line 47, column 1

How I can fix it? I am attaching my code. Please help
Trigger:

trigger TriggeronTopicAssignment on TopicAssignment (after insert)
{    
    if(Trigger.isAfter && Trigger.isInsert) {
        TopicAssignmentTriggerHandler TopicAssignmentTriggerHandlerInstance = new TopicAssignmentTriggerHandler();
        TopicAssignmentTriggerHandlerInstance.sendEmailForTestimonial(Trigger.New);
    }
}

Apex Class:
/**
 **
 ** This helper class sends mail for the testimonial topic to certain user when any topic assignment on case is created
**/
public class TopicAssignmentTriggerHandler {

    
    public void sendEmailForTestimonial(List<TopicAssignment> topicAssignmentListNew) 
    {
    
        Map<Id, Topic> testimonialTopicIdsMappedTo = new Map<Id, Topic>([select Id,Name from Topic where (Name='Testimonial Feedback' OR 
        Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR
        Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' 
        OR Name='PR' OR Name='Employment Verification')]);        
        System.debug('testimonialTopicIdsMappedTo'+testimonialTopicIdsMappedTo  );
        //Create a master list to hold the emails we'll send
        List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
        Map<Id, Id> caseIdMappedToContactId = new Map<Id, Id>();
        Map<Id, Id> caseIdMappedWithTopicAssignmentId = new Map<Id, Id>();
        Map<Id, Contact> contactIdMappedToRecord = new Map<Id, Contact>();
        Set<ID> caseIdSet = new Set<ID>();
       
        if(testimonialTopicIdsMappedTo.size() > 0 ) 
        {
         for(TopicAssignment topicAssignment :topicAssignmentListNew)
         {
          if(testimonialTopicIdsMappedTo.Keyset().contains(topicAssignment.TopicId) && topicAssignment.EntityKeyPrefix=='500') {
            caseIdSet.add(topicAssignment.EntityId);
             if(caseIdMappedWithTopicAssignmentId.keyset() != null && !caseIdMappedWithTopicAssignmentId.keyset().contains(topicAssignment.EntityId)) 
                     {
                         caseIdMappedWithTopicAssignmentId.put(topicAssignment.EntityId,topicAssignment.TopicId);
                     }                     
                }    
            }  
        }
        
        for(case caseRecordForContact : [select contactId, Id from case where ID In:caseIdSet and contactId != null]) 
        {
            caseIdMappedToContactId.put(caseRecordForContact.Id, caseRecordForContact.ContactId); 
        }
        
        for(Contact contactRecord: [select Id, Name, Email from contact where Id In:caseIdMappedToContactId.values()]) 
        {
           contactIdMappedToRecord.put(contactRecord.Id,contactRecord); 
        }
        
        for(Case caseRecord : [select Id, CaseNumber,Description,Subject,ContactId from Case where Id IN:caseIdSet]) 
        {
           contact contactRec = new contact();
           contactRec =contactIdMappedToRecord.get(caseRecord.ContactId);
           System.debug('contactRec'+contactRec);
           Id TopicId = caseIdMappedWithTopicAssignmentId.get(caseRecord.Id);
           Topic TopicRecord = testimonialTopicIdsMappedTo.get(TopicId);
           System.debug('TopicRecord '+TopicRecord);
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();    
           String caselink = System.URL.getSalesforceBaseUrl().toExternalForm() +'/' +caseRecord.id;
           //Set list of people who should get the email
           List<String> sendTo = new List<String>();
           List<String> EmailList= new List<String>();
           //Dummy contact to avoid sending mails to Contact attached to Case
           Contact con = new Contact();
           
           if(TopicRecord.Name.Contains('Testimonial') || TopicRecord.Name.Contains('Testimonial Feedback') || 
           TopicRecord.Name.Contains('Author Feedback') || TopicRecord.Name.Contains('Testimonial') || 
           TopicRecord.Name.Contains('Course Feedback') || TopicRecord.Name.Contains('General Feedback') || 
           TopicRecord.Name.Contains('Course Request') || TopicRecord.Name.Contains('Feature Request')  )
            {    system.debug('###############Topic record'+TopicRecord);   
                 if(TopicRecord.Name.Equals('Testimonial'))
                 {     
                     Testimonial_Topic_Assignment_EmailId__c testimonialEmailIdRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Testimonial');
                     String e=testimonialEmailIdRecord.EmailAddress__c;
                     EmailList=SplitEmail(e);
                     for(String s:EmailList)
                     {
                        sendTo.add(s);
                     }
                     System.debug('sendTo'+sendTo);
                  }
                 else 
                    {
                        Testimonial_Topic_Assignment_EmailId__c FeedbackRequestRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Feedback/Request');
                        String e=FeedbackRequestRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                 
                
                 //Set email contents - you can use variables!
            mail.setSubject(''+ TopicRecord.Name +' Topic Added To Case Number ' + caseRecord.CaseNumber + '');
            
            String body = 'Hi ,' + '<br/><br/>';
            
            body += 'Case topic has been added to the following case.'+'<br/><br/>';
            body += 'Topic Name : ' + TopicRecord.Name + '<br/>';
            body += 'Case Number : ' + caseRecord.CaseNumber + '<br/>';
            
            if(contactRec != null) 
            {
                body += '&nbsp;&nbsp;&nbsp;&nbsp;Case Contact Name : ' + (contactRec.Name==null?'':contactRec.Name) + '<br/>';  
                body += '&nbsp;&nbsp;&nbsp;&nbsp;Contact Email Id : ' + (contactRec.Email==null?'':contactRec.Email) + '<br/>'; 
            }
            
            
          //  body += '&nbsp;&nbsp;&nbsp;&nbsp;Case CaseNumber: ' + (caseRecord.CaseNumber==null?'':caseRecord.CaseNumber) + '<br/>'; 
            body += 'Case Subject : ' + (caseRecord.Subject==null?'':caseRecord.Subject) + '<br/>';
            body += 'Case Description : ' + (caseRecord.Description==null?'':caseRecord.Description) + '</br>';
            body += '<a href="' + caselink  + '">Case Link</a>'  + '<br/><br/>';
            
            
            body += 'Thanks,' + '</br>'; 
            body += 'Lynda.com';
            
             mail.setHtmlBody(body);
             mail.setToAddresses(sendTo);
             mails.add(mail);
            }
          
            else if(TopicRecord.Name.Contains('Community Relations') || TopicRecord.Name.Contains('Legal') 
            || TopicRecord.Name.Contains('Remove From Newsletter') || TopicRecord.Name.Contains('Piracy') 
            || TopicRecord.Name.Contains('PR') || TopicRecord.Name.Contains('Employment Verification') 
            || TopicRecord.Name.Contains('Careers') )
            {    system.debug('###############Topic record'+TopicRecord);    
                 List<Testimonial_Topic_Assignment_EmailId__c> AllEmailIdRecordList= Testimonial_Topic_Assignment_EmailId__c.getall().values();
                 
                 System.debug('AllEmailIdRecordList'+AllEmailIdRecordList);
                    if(TopicRecord.Name.Contains('Community Relations'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c communityRelationRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Community Relations');
                        String e=communityRelationRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Legal'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c LegalRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Legal');
                        String e=LegalRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Remove From Newsletter'))
                    {
                        System.debug('TopicRecord.Name'+TopicRecord.Name);
                        Testimonial_Topic_Assignment_EmailId__c RemovefromnewslettersRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Remove From Newsletter');
                        System.debug('RemovefromnewslettersRecord'+RemovefromnewslettersRecord);
                        String e=RemovefromnewslettersRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Piracy'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c PiracyRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Piracy');
                        String e=PiracyRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('PR'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c PRRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('PR');
                        String e=PRRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Employment Verification'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c EmploymentverificationRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Employment Verification');
                        String e=EmploymentverificationRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Careers'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c CareersRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Careers');
                        String e=CareersRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    
                
                 mail.setToAddresses(sendTo);
                
            system.debug(sendTo);  
            //Set email contents
            mail.setSubject('Case Alert from Salesforce');   
            String body = 'Hello ,' + '<br/><br/>'; 
            body += 'Customer Service received the email below, and we wanted to send it your way for review. The case is now closed, but please let us know if we may be of assistance regarding this issue.'+'<br/><br/>' ;
            body += 'Thank you, and have a great day!'+'<br/><br/>'; 
            body += 'Best Regards,'+'<br/>';
            body += 'Customer Service Team '+'<br/><br/>';

            body += 'Topic Name : ' + TopicRecord.Name + '<br/>';
            body += 'Case Number : ' + caseRecord.CaseNumber + '<br/>';
            body += 'Email Subject : ' + caseRecord.Subject + '<br/>';
            body += 'Email Description : ' + caseRecord.Description + '<br/>';
            body += '<a href="' + caselink  + '">Case Link</a>'  + '<br/>';
                              
            mail.setHtmlBody(body);
            //Add your email to the master list
            mails.add(mail);  
        }
        system.debug('total Emails' + mails);
        
        }
        Messaging.sendEmail(mails);
        }
        public List<String> SplitEmail(String Emails){
        List<String> EmailIdList =Emails.split(',');
        System.debug('EmailIdList '+EmailIdList );
        return EmailIdList;
    }
    
}

Apex Test Class:
 
/**}
 ** @author JadeGlobal Inc
 ** @created 4-Feb/2015
 **
 ** Test class for TopicAssignmentTriggerHandler.cls
**/


  

@isTest(SeeAllData=true)
public class TopicAssignmentTriggerHandlerTest{

    public static testmethod void AssignmentTriggerHandlertest(){
        Test.startTest();
          
          List <Topic> TopicList=[Select Id,Name from Topic where (Name='Testimonial Feedback' OR Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' OR Name='PR' OR Name='Employment Verification')];          
          List<TopicAssignment> TopicAssignmentList=new List<TopicAssignment>();
          List<Case> CaseList=new List<Case>();
          
         //create test Account
          Account testAcc = new Account(
          Name = 'testAccount',
          BillingCountryCode = 'US',
          BillingState = 'California',
          BillingStreet = '6410 via Real',
          BillingPostalCode = '93013',
          BillingCity = 'Carpinteria',
          Industry = 'Construction',
          LDC_ID__c =  1234);
          insert testAcc ;
          System.Assertequals(testAcc.Name,'testAccount');
          
          //Create contact data
          Contact c1 = new Contact(AccountId = testAcc.id, FirstName = 'Test', LastName = 'Contact', Email='adda@gmail.com');
          c1.MailingState = 'California';
          c1.MailingCountry = 'United States';
          insert c1;
          System.Assertequals(c1.FirstName,'Test');
          System.Assertequals(c1.LastName,'Contact');
          
                   
          //create case
          Case testcas1 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas1;
          TopicAssignment ta1 = new TopicAssignment(Topicid = TopicList.get(0).id, EntityId = testcas1.id);
          insert ta1;
          
          Case testcas2 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas2;
          TopicAssignment ta2 = new TopicAssignment(Topicid = TopicList.get(1).id, EntityId = testcas2.id);
          insert ta2;
          
          Case testcas3 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas3;
          TopicAssignment ta3 = new TopicAssignment(Topicid = TopicList.get(2).id, EntityId = testcas3.id);
          insert ta3;
          
          Case testcas4 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas4;
          TopicAssignment ta4 = new TopicAssignment(Topicid = TopicList.get(3).id, EntityId = testcas4.id);
          insert ta4;
          
          Case testcas5 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas5;
          TopicAssignment ta5 = new TopicAssignment(Topicid = TopicList.get(4).id, EntityId = testcas5.id);
          insert ta5;
         
          Case testcas6 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas6;
          TopicAssignment ta6 = new TopicAssignment(Topicid = TopicList.get(5).id, EntityId = testcas6.id);
          insert ta6;
          
          Case testcas7 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas7;
          TopicAssignment ta7 = new TopicAssignment(Topicid = TopicList.get(6).id, EntityId = testcas7.id);
          insert ta7;
          
          Case testcas8 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas8;
          TopicAssignment ta8 = new TopicAssignment(Topicid = TopicList.get(7).id, EntityId = testcas8.id);
          insert ta8;
          
          Case testcas9 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas9 ;
          TopicAssignment ta9 = new TopicAssignment(Topicid = TopicList.get(8).id, EntityId = testcas9.id);
          insert ta9;
          
          Case testcas10 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas10 ;
          TopicAssignment ta10 = new TopicAssignment(Topicid = TopicList.get(9).id, EntityId = testcas10.id);
          insert ta10;
        
               
        
          
          
         Test.stopTest();
    }
}

 
  • May 06, 2015
  • Like
  • 0
Hi, I have written a trigger with apex on Case Topic assignment but I only managed to get the code coverage upto 80%. Please help in getting the code coverage upto 90% although the code is working fine. 
/**}

 ** Test class for TopicAssignmentTriggerHandler.cls
**/


  

@isTest
public class TopicAssignmentTriggerHandlerTest{

    public static testmethod void AssignmentTriggerHandlertest(){
        Test.startTest();
          
          List <Topic> TopicList=[Select Id,Name from Topic where (Name='Testimonial Feedback' OR Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' OR Name='PR' OR Name='Employment Verification')];          
          List<TopicAssignment> TopicAssignmentList=new List<TopicAssignment>();
          List<Case> CaseList=new List<Case>();
          
         //create test Account
          Account testAcc = new Account(
          Name = 'testAccount',
          BillingCountryCode = 'US',
          BillingState = 'California',
          BillingStreet = '6410 via Real',
          BillingPostalCode = '93013',
          BillingCity = 'Carpinteria',
          Industry = 'Construction',
          LDC_ID__c =  1234);
          insert testAcc ;
          System.Assertequals(testAcc.Name,'testAccount');
          
          //Create contact data
          Contact c1 = new Contact(AccountId = testAcc.id, FirstName = 'Test', LastName = 'Contact', Email='c-aray@lynda.com');
          c1.MailingState = 'California';
          c1.MailingCountry = 'United States';
          insert c1;
          System.Assertequals(c1.FirstName,'Test');
          System.Assertequals(c1.LastName,'Contact');
          
          //create case
          Case testcas1 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas1;
        
          TopicAssignment ta1 = new TopicAssignment(Topicid = TopicList.get(0).id, EntityId = testcas1.id);
          insert ta1;
          Case testcas2 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas2;
          TopicAssignment ta2 = new TopicAssignment(Topicid = TopicList.get(1).id, EntityId = testcas2.id);
          insert ta2;
          Case testcas3 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas3;
          TopicAssignment ta3 = new TopicAssignment(Topicid = TopicList.get(2).id, EntityId = testcas3.id);
          insert ta3;
          
          Case testcas4 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas4;
          TopicAssignment ta4 = new TopicAssignment(Topicid = TopicList.get(3).id, EntityId = testcas4.id);
          insert ta4;
          Case testcas5 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas5;
          TopicAssignment ta5 = new TopicAssignment(Topicid = TopicList.get(4).id, EntityId = testcas5.id);
          insert ta5;
         
          Case testcas6 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas6;
          TopicAssignment ta6 = new TopicAssignment(Topicid = TopicList.get(5).id, EntityId = testcas6.id);
          insert ta6;
          Case testcas7 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas7;
          TopicAssignment ta7 = new TopicAssignment(Topicid = TopicList.get(6).id, EntityId = testcas7.id);
          insert ta7;
          
          Case testcas8 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas8;
          TopicAssignment ta8 = new TopicAssignment(Topicid = TopicList.get(7).id, EntityId = testcas8.id);
          insert ta8;
          Case testcas9 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas9 ;
          TopicAssignment ta9 = new TopicAssignment(Topicid = TopicList.get(8).id, EntityId = testcas9.id);
          insert ta9;
          
          Case testcas10 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas10 ;
          TopicAssignment ta10 = new TopicAssignment(Topicid = TopicList.get(9).id, EntityId = testcas10.id);
          insert ta10;
          
          
         Test.stopTest();
    }
}
  • April 29, 2015
  • Like
  • 0
Hi All,

I have created a button that passes value from Case to Contact when we try create a new contact from case. I am able to pass value like account info, phone number from case to phone number of contact as it is one to one maping. But I am not able to pass Case ID from Case to Contact. Case related list. Bascially, when I create a contact from case automatically my button should attached that case in the contact. Here is my URL formula.

https://cs30.salesforce.com/003/e?retURL=%2F003n0000006N1WG&con4={!Case.Account}&con10={!Account.Phone}&{!Case.ContactId}={!Case.Id}&RecordType=012n00000008Wq0&ent=Case

Please help.

Thanks 
Adda
 
  • April 15, 2015
  • Like
  • 0
Hi, 

I have a unqiue problem. We are using Case Feed. When the account and contact is update on case we need a functionality to automate. Whenever a account is selected on lookup and user goes to contact lookup and if they don't find the contact they create a new contact. But nothing is prepopulated from account to contact. So, I am creating a formula field and making it available on lookup dialog. 

But the problem is I want to prepopulate some of the field like account name, account id into the new contact record using the formula as a URL hack. But I am not able to get the formula right. This is what I came up with.

HYPERLINK("https://cs30.salesforce.com/003/e?retURL=%%F001%2Fo&accid=Case.AccountId", "Create a Contact")

Please help. I am sorry I don't make this requirement understandable.

Thanks
Adda
  • April 08, 2015
  • Like
  • 0
Hi,

I have written a fucntionality to convert lead to case. The only problem is I have a field on Lead as Web-to-Lead Notes which is Text Area long and that is being popualated on Case Subject on Conversion. It gives error if the length is big. So I need to truncate the length of Lead to Web Notes field to 50 to properly fit in the Case Subject. Please help. Here is my code VFP and Apex:

Visualforce Page:
<apex:page standardController="Lead" extensions="CaseConverter">
    <apex:form >
        <apex:pageBlock title="Convert information to a new Case.">
            <apex:pageBlockButtons >
                <apex:commandbutton action="{!createCase}" value="Finish"/>
                <apex:commandbutton action="{!returnToLead}" value="Previous"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Lead content Section">            
                <tr><th class="labelCol vfLabelColTextWrap " scope="row">Lead Record Type</th><td class="dataCol "><span id="j_id0:j_id1:j_id2:j_id6:nameId">{!recordTypeName}</span></td></tr>
                <apex:outputText value="{!lead.Web_To_Lead_Notes__c} " rendered="true"/>
                <apex:outputField value="{!lead.Name}" id="nameId"/>
                <apex:outputField value="{!lead.Company}" id="companyId"/>
                <apex:outputField value="{!lead.Phone}" id="phoneid"/>
                <apex:outputField value="{!lead.City}" id="cityId"/>
                <apex:outputField value="{!lead.Street}" id="streetId"/>
                <apex:outputField value="{!lead.Country}" id="countryid"/>
                <apex:outputField value="{!lead.Email}" id="emailid"/>
                <apex:outputField value="{!lead.PostalCode}" id="postalId"/>
                <apex:outputField value="{!lead.MobilePhone}" id="mobileId"/>
                <apex:outputField value="{!lead.Website}" id="websiteId"/>
                <apex:outputField value="{!lead.Description}" id="descriptionId"/>
                <apex:outputField value="{!lead.Industry}" id="industryId"/>                           
            </apex:pageBlockSection>
            
         
   Apex Class:
public with sharing class CaseConverter {
    public Lead curLead{set;get;}
    private final String name;
    private final String company;
    private final String phone;
    private final String city;
    private final String street;
    private final String country;
    private final String email;
    private final String postal;
    private final String mobile;
    private final String website;
    private final String description;
    private final String industry;
    private final String leadId;
    public String Web_To_Lead_Notes_Case;
    public string accountOwnerId;
    
    //getter,setter for the selected list values
    public String statusCaseSelected{get;set;}
    public String originSelected{get;set;}
    
    //getter,setter for the input fields
    public String subjectInput{get;set;}
    
    //getter,setter for the input fields
    public String recordTypeName{get;set;}
    
    //getter setter for the checkboxes
    public Boolean createAccount{get;set;}
    
    /*get the select options from the case system schema for the status field
    * Input: nothing
    * Output: List of the select options from case status
    */
    public List<Selectoption> getStatusCaseItems(){
        List<Selectoption> statusValues = new List<Selectoption>();
        Schema.Describefieldresult systemCaseStatus = Case.Status.getDescribe();
        for(Schema.Picklistentry plEntry : systemCaseStatus.getPicklistValues()){
            statusValues.add(
                new Selectoption(
                    plEntry.getValue(),
                    plEntry.getLabel()
                )
            );
        }
        return statusValues;
    }
    
    /*get the select options from the case system schema for the origin field
    * Input: nothing
    * Output: List of the select options from case origin
    */
    public List<Selectoption> getOriginItems(){
        List<Selectoption> originValues = new List<Selectoption>();
                 originValues.add(
                       new Selectoption(
                                'Customer Service','Customer Service'
                )
           );
                
        
        return originValues;
    }

    //constructor, this page will be called from the LeadToCase Page
    public CaseConverter(ApexPages.StandardController controller) {
        this.curLead= (Lead)controller.getRecord();
        List<QueueSobject> lstQueues = [SELECT Id,queue.Name, QueueId FROM QueueSobject WHERE SobjectType = 'Case'and queue.Name = 'Customer Service Cases'];
        curLead.OwnerId = lstQueues[0].QueueId;
        
        List<recordType> recordTypeList = [select Name from recordType where Id = :curlead.recordTypeId];
        if(recordTypeList.Size() > 0 ) {
            recordTypeName = recordTypeList[0].Name;
        } 
        this.name = curLead.FirstName + ' ' + curLead.LastName;
        this.company = curLead.Company;
        this.phone = curLead.Phone;
        this.city = curLead.City;
        this.street = curLead.Street;
        this.country = curLead.Country;
        this.email = curLead.Email;
        this.postal = curLead.PostalCode;
        this.mobile = curLead.MobilePhone;
        this.website = curLead.Website;
        this.description = curLead.Description;
        this.industry = curLead.Industry;
        this.leadId = curLead.Id; 
        this.Web_To_Lead_Notes_Case = curLead.Web_To_Lead_Notes__c;
        subjectInput = curLead.Web_To_Lead_Notes__c; 
       
    }
    
    
    
  • April 03, 2015
  • Like
  • 0

Hi,

 

In lookups for standard object, we have New button on lookup dialig.

Is it possible to have New button on lookup dialog for Cutom Object. If yes please let me know the steps or workaround

 

Thanks