• Hong_Yan
  • NEWBIE
  • 35 Points
  • Member since 2012

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

Hi,

 

Here im created a trigger and test class.Currently im having 44% code coverage.How to achieve 100% for this trigger.

trigger MailTask on Task (after insert,after update)
{
    set<String> setownerIds = new set<String>();
    Set<ID> TaskId = new Set<Id>();
     if((Trigger.isUpdate)){
    for(Task TaskObj : Trigger.New)
    {
        TaskId.add(TaskObj.Id);       
    }
    Task[] TaskList = [Select Id,Owner.Name,CreatedByID,OwnerId,Owner.Email,Subject,ActivityDate,Priority,Status,Description,What.Type,What.Name,Who.type,Who.Name from Task where Id in : TaskId];
    system.debug('Tasklist'+TaskList);
    Map<Id,Account> AccountMap ;
    Map<Id,Lead> LeadMap ;
    Map<Id,Opportunity> OpportunityMap;
    Map<Id,Contact> ContactMap ;
  
    Set<Id> AccountId = new Set<Id>();
    Set<Id> LeadId = new Set<Id>();
    Set<Id> OpportunityId = new Set<Id>();
    Set<Id> ContactId = new Set<Id>();
   
    //if(Trigger.isUpdate && Trigger.isBefore)
   
    for(Task TaskObj : TaskList)
    {
        
        if(TaskObj.Who.Type == 'Lead')
        {
            LeadId.add(TaskObj.whoId);
        }
        if(TaskObj.who.Type == 'Contact')
        {
            ContactId.add(TaskObj.WhoId);
        }
        if(TaskObj.what.Type == 'Account')
        {
            AccountId.add(TaskObj.WhatId);
        }
        if(TaskObj.what.Type == 'Opportunity')
        {
            OpportunityId.add(TaskObj.WhatId);
        }
       
        
    }
     
    AccountMap =  new Map<Id,Account>([Select Id,Name,Owner.Email from Account where Id in : AccountId ]);
    LeadMap = new Map<Id,Lead>([Select Id,Name,Owner.Email from Lead where Id in : LeadId ]);
    OpportunityMap  = new Map<Id,Opportunity>( [Select Id,Name,Owner.Email from Opportunity where Id in : OpportunityId ]);
    system.debug('opportunitymap!!!!!!!!!!!!!'+OpportunityMap);
    ContactMap = new Map<Id,Contact>([Select Id,Owner.Email,Name from Contact where Id in : ContactId ]);

    
    for(Task TskObj : TaskList)
    {
     
    if(TskObj.OwnerId != TskObj.CreatedByID){
       String[] toAddresses ;
        String forName = '';
        if(TskObj.who.Type == 'Contact')
        {
           toAddresses = new String[] {(ContactMap.get(TskObj.WhoId).Owner.Email)};
            forName = TskObj.who.Name;
        }
        if(TskObj.Who.Type == 'Lead')
        {
           toAddresses = new String[] {(LeadMap.get(TskObj.WhoId).Owner.Email)};
            forName = TskObj.who.Name;
        }
        if(TskObj.what.Type == 'Account')
        {
             toAddresses = new String[] {(AccountMap.get(TskObj.WhatId).Owner.Email)};
             forName = TskObj.what.Name;
        }
        if(TskObj.what.Type == 'Opportunity')
        {
         toAddresses = new String[] {(OpportunityMap.get(TskObj.WhatId).Owner.Email)};
        system.debug('Address'+ toAddresses);
        
         forName = TskObj.what.Name;
          system.debug('name'+forName);
        }
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        system.debug('ToAddress'+toAddresses);
        mail.setToAddresses(toAddresses);
        system.debug('Mail '+mail);
        mail.setSubject('A task assigned by you has Completed');  
        
         system.debug('Mail subject'+mail);  // Set the subject
        String template = 'Hello, \nYour assigned task has Completed. Here are the details - \n\n';
        template+= 'Subject - {0}\n';
        template+= 'Related to - {1}\n';
        template+= 'Due Date - {2}\n';
        template+= 'Priority - {3}\n';
        template+= 'Comments - {4}\n';
        
        String duedate = '';
        if (TskObj.ActivityDate==null)
            duedate = '';
        else
            duedate = TskObj.ActivityDate.format();
            system.debug('Duedate'+duedate);
        List<String> args = new List<String>();
         
        args.add(TskObj.Subject);
        
        args.add(forName);
      
        args.add(duedate);
        args.add(TskObj.Priority);
        args.add(TskObj.Description);
        String formattedHtml = String.format(template, args);
       
        mail.setPlainTextBody(formattedHtml);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
   }  
      
  }

}
}

 Test class

@isTest
public class testemailalert
{
 static testmethod void testemailalertmethod()
 {
       
       Messaging.Singleemailmessage testEmail;
        List<Messaging.Sendemailresult> testEmailResults;
        Opportunity testOpp = new Opportunity( Name ='TestOpp', StageName ='Prospecting', CloseDate = System.today());
        insert testOpp;
        Account acc=new Account(Name='Testaccount');
        insert acc;
        Lead ld=new Lead(LastName='testlead',Company=acc.Id,Status='Open-Not Contacted');
        insert ld;
        Contact cd=new Contact(LastName='testcontact',Email='amritaaravind@gmail.com');
        insert cd;
       // List<Task> taskList = new List<Task> ();
      //  taskList.add(new Task(Subject='test',Priority='Medium',Status='Completed',WhatId=acc.id,WhoId=cd.id,ActivityDate=System.today()));     
       // insert taskList;
          test.startTest(); 
       List<Task> taskList = new List<Task> ();
       Task t = new Task();
      
        t.OwnerId = UserInfo.getUserId();
        system.debug('User----------------'+t.OwnerId );
        t.Subject='Send Out Notice To Vacate';
        t.Status='Not Started';
        t.Priority='Normal';
        t.WhatId=testOpp.Id;
        t.WhoId=cd.id;
        insert t;
     
        t.Priority='High';
        t.Status='Completed';
        update t;     
        taskList.add(t);
        Task testTask2 = new Task (OwnerId = UserInfo.getUserId(),WhatId = acc.Id,WhoId = cd.Id,Subject = 'Other', Status ='In Progress',ActivityDate=System.today());
        insert testTask2;
        Task testTask3 = new Task (OwnerId = UserInfo.getUserId(),WhoId = ld.Id,Subject = 'Other', Status ='In Progress',ActivityDate=System.today());
        insert testTask3;
         Task testTask4 = new Task (OwnerId = UserInfo.getUserId(),WhoId = cd.Id,Subject = 'Other', Status ='In Progress',ActivityDate=System.today());
        insert testTask4;
        testTask4.Status = 'Completed';
        update testTask4;
       
        Task edTask2 = [Select t.WhatId, t.Subject, t.Status From Task t where t.Id=:testTask2.Id];
        edTask2.Status = 'Completed';
        update edTask2;
        taskList.add(edTask2);
        Task edTask3 = [Select t.WhoId, t.Subject, t.Status From Task t where t.Id=:testTask3.Id];
        edTask3.Status = 'Completed';
        update edTask3;
        taskList.add(edTask3);
        String[] toAddresses ;
        String forName = '';
        if(testTask4.who.Type == 'Contact')
           {
            toAddresses = new String[] {'amritaaravind@gmail.com'};
            }
            testEmail.setToAddresses(toAddresses);
         test.stopTest();
      
 
 }
 
 
 }

 This is related to task.Im not able to get coverage for  the address in Messaging.sendemailmessage..Please suggest me any methods to get more coverage fo this

Thanks

  • December 06, 2012
  • Like
  • 0

I've been trying to do this by setting the DML options, but it doesn't seem to be working.

 

Stuck as to what I'm doing wrong.

This is the code I'm using:

 

    public PageReference doSave()
    {
        List<sObject> sList=setCon.getSelected();
        List<CaseComment> ccList=new List<CaseComment>();
        for(sObject s:sList)
        {
            s.Put('Status',CaseStatus);
            s.Put('Reason',CaseReason);
            if(!String.isBlank(Comment))
            {
                ccList.Add(new CaseComment(CommentBody=Comment, ParentId=(Id)s.Get('id'), isPublished=PublicComment));
            }
        }
        if(!ccList.isEmpty())
        {
            Database.DMLOptions dmlOpts = new Database.DMLOptions();
            dmlOpts.EmailHeader.triggerAutoResponseEmail = true;
            dmlOpts.EmailHeader.triggerOtherEmail = true;
            database.insert(ccList,dmlOpts);
        }
        return setCon.save();
    }

Is there an option that can be sent to send the welcome email to a user when creating the user through apex?

I'm able to create the user successfully, just can't find any option to send the welcome email.

 

Thanks!

Hi,

 

Here im created a trigger and test class.Currently im having 44% code coverage.How to achieve 100% for this trigger.

trigger MailTask on Task (after insert,after update)
{
    set<String> setownerIds = new set<String>();
    Set<ID> TaskId = new Set<Id>();
     if((Trigger.isUpdate)){
    for(Task TaskObj : Trigger.New)
    {
        TaskId.add(TaskObj.Id);       
    }
    Task[] TaskList = [Select Id,Owner.Name,CreatedByID,OwnerId,Owner.Email,Subject,ActivityDate,Priority,Status,Description,What.Type,What.Name,Who.type,Who.Name from Task where Id in : TaskId];
    system.debug('Tasklist'+TaskList);
    Map<Id,Account> AccountMap ;
    Map<Id,Lead> LeadMap ;
    Map<Id,Opportunity> OpportunityMap;
    Map<Id,Contact> ContactMap ;
  
    Set<Id> AccountId = new Set<Id>();
    Set<Id> LeadId = new Set<Id>();
    Set<Id> OpportunityId = new Set<Id>();
    Set<Id> ContactId = new Set<Id>();
   
    //if(Trigger.isUpdate && Trigger.isBefore)
   
    for(Task TaskObj : TaskList)
    {
        
        if(TaskObj.Who.Type == 'Lead')
        {
            LeadId.add(TaskObj.whoId);
        }
        if(TaskObj.who.Type == 'Contact')
        {
            ContactId.add(TaskObj.WhoId);
        }
        if(TaskObj.what.Type == 'Account')
        {
            AccountId.add(TaskObj.WhatId);
        }
        if(TaskObj.what.Type == 'Opportunity')
        {
            OpportunityId.add(TaskObj.WhatId);
        }
       
        
    }
     
    AccountMap =  new Map<Id,Account>([Select Id,Name,Owner.Email from Account where Id in : AccountId ]);
    LeadMap = new Map<Id,Lead>([Select Id,Name,Owner.Email from Lead where Id in : LeadId ]);
    OpportunityMap  = new Map<Id,Opportunity>( [Select Id,Name,Owner.Email from Opportunity where Id in : OpportunityId ]);
    system.debug('opportunitymap!!!!!!!!!!!!!'+OpportunityMap);
    ContactMap = new Map<Id,Contact>([Select Id,Owner.Email,Name from Contact where Id in : ContactId ]);

    
    for(Task TskObj : TaskList)
    {
     
    if(TskObj.OwnerId != TskObj.CreatedByID){
       String[] toAddresses ;
        String forName = '';
        if(TskObj.who.Type == 'Contact')
        {
           toAddresses = new String[] {(ContactMap.get(TskObj.WhoId).Owner.Email)};
            forName = TskObj.who.Name;
        }
        if(TskObj.Who.Type == 'Lead')
        {
           toAddresses = new String[] {(LeadMap.get(TskObj.WhoId).Owner.Email)};
            forName = TskObj.who.Name;
        }
        if(TskObj.what.Type == 'Account')
        {
             toAddresses = new String[] {(AccountMap.get(TskObj.WhatId).Owner.Email)};
             forName = TskObj.what.Name;
        }
        if(TskObj.what.Type == 'Opportunity')
        {
         toAddresses = new String[] {(OpportunityMap.get(TskObj.WhatId).Owner.Email)};
        system.debug('Address'+ toAddresses);
        
         forName = TskObj.what.Name;
          system.debug('name'+forName);
        }
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        system.debug('ToAddress'+toAddresses);
        mail.setToAddresses(toAddresses);
        system.debug('Mail '+mail);
        mail.setSubject('A task assigned by you has Completed');  
        
         system.debug('Mail subject'+mail);  // Set the subject
        String template = 'Hello, \nYour assigned task has Completed. Here are the details - \n\n';
        template+= 'Subject - {0}\n';
        template+= 'Related to - {1}\n';
        template+= 'Due Date - {2}\n';
        template+= 'Priority - {3}\n';
        template+= 'Comments - {4}\n';
        
        String duedate = '';
        if (TskObj.ActivityDate==null)
            duedate = '';
        else
            duedate = TskObj.ActivityDate.format();
            system.debug('Duedate'+duedate);
        List<String> args = new List<String>();
         
        args.add(TskObj.Subject);
        
        args.add(forName);
      
        args.add(duedate);
        args.add(TskObj.Priority);
        args.add(TskObj.Description);
        String formattedHtml = String.format(template, args);
       
        mail.setPlainTextBody(formattedHtml);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
   }  
      
  }

}
}

 Test class

@isTest
public class testemailalert
{
 static testmethod void testemailalertmethod()
 {
       
       Messaging.Singleemailmessage testEmail;
        List<Messaging.Sendemailresult> testEmailResults;
        Opportunity testOpp = new Opportunity( Name ='TestOpp', StageName ='Prospecting', CloseDate = System.today());
        insert testOpp;
        Account acc=new Account(Name='Testaccount');
        insert acc;
        Lead ld=new Lead(LastName='testlead',Company=acc.Id,Status='Open-Not Contacted');
        insert ld;
        Contact cd=new Contact(LastName='testcontact',Email='amritaaravind@gmail.com');
        insert cd;
       // List<Task> taskList = new List<Task> ();
      //  taskList.add(new Task(Subject='test',Priority='Medium',Status='Completed',WhatId=acc.id,WhoId=cd.id,ActivityDate=System.today()));     
       // insert taskList;
          test.startTest(); 
       List<Task> taskList = new List<Task> ();
       Task t = new Task();
      
        t.OwnerId = UserInfo.getUserId();
        system.debug('User----------------'+t.OwnerId );
        t.Subject='Send Out Notice To Vacate';
        t.Status='Not Started';
        t.Priority='Normal';
        t.WhatId=testOpp.Id;
        t.WhoId=cd.id;
        insert t;
     
        t.Priority='High';
        t.Status='Completed';
        update t;     
        taskList.add(t);
        Task testTask2 = new Task (OwnerId = UserInfo.getUserId(),WhatId = acc.Id,WhoId = cd.Id,Subject = 'Other', Status ='In Progress',ActivityDate=System.today());
        insert testTask2;
        Task testTask3 = new Task (OwnerId = UserInfo.getUserId(),WhoId = ld.Id,Subject = 'Other', Status ='In Progress',ActivityDate=System.today());
        insert testTask3;
         Task testTask4 = new Task (OwnerId = UserInfo.getUserId(),WhoId = cd.Id,Subject = 'Other', Status ='In Progress',ActivityDate=System.today());
        insert testTask4;
        testTask4.Status = 'Completed';
        update testTask4;
       
        Task edTask2 = [Select t.WhatId, t.Subject, t.Status From Task t where t.Id=:testTask2.Id];
        edTask2.Status = 'Completed';
        update edTask2;
        taskList.add(edTask2);
        Task edTask3 = [Select t.WhoId, t.Subject, t.Status From Task t where t.Id=:testTask3.Id];
        edTask3.Status = 'Completed';
        update edTask3;
        taskList.add(edTask3);
        String[] toAddresses ;
        String forName = '';
        if(testTask4.who.Type == 'Contact')
           {
            toAddresses = new String[] {'amritaaravind@gmail.com'};
            }
            testEmail.setToAddresses(toAddresses);
         test.stopTest();
      
 
 }
 
 
 }

 This is related to task.Im not able to get coverage for  the address in Messaging.sendemailmessage..Please suggest me any methods to get more coverage fo this

Thanks

  • December 06, 2012
  • Like
  • 0

Is there an option that can be sent to send the welcome email to a user when creating the user through apex?

I'm able to create the user successfully, just can't find any option to send the welcome email.

 

Thanks!

Hi,

 

I have one custom object "Award", and there is lookup on award to Opportunity, In opportunity there is Product related list. What my require ment is, when user choose Opportunity from award, all products related to that Opportunity should attach to Award. 

 

i wrote code for this, however it is attaching only one product to award(updated one), please help me in this,

 

trigger trg_HAS_AssociateProductToAwardStagePO_AI_AU on Award__c (after insert,after update) {
//list<Award__c> awd= new list<Award__c>();
set<string> setId = new set<string>();
for(Award__c aw:trigger.new){
if(aw.Stage__c == 'Email Commitment' ) {
setId.add(aw.Opportunity__c);
system.debug('@@@@@'+setID);
}
}


//List<Product__c> pdt = [Select Id,Name,Opportunity__c, Award__c from Product__c where Opportunity__c IN: setId];
map<string,Product__c > pdtmap= new Map<string,Product__c>();
List<Product__c> pdt = new List<Product__c>();
for (Product__c obj :[Select Id,Name,Opportunity__c, Award__c from Product__c where Opportunity__c IN: setId]) {
pdtmap.put(obj.Opportunity__c ,obj);
}
Product__c prd = new Product__c();
for(Award__c aw1:trigger.new){

if(pdtmap.containskey(aw1.Opportunity__c)) {
//prd = new Product__c(id=pdtmap.get(aw1.Opportunity__c).id);
//prd = new Product__c(Opportunity__c=pdtmap.get(aw1.Opportunity__c).id);
prd = new Product__c();
prd.Opportunity__c = aw1.Opportunity__c;
prd.Award__c = aw1.Id;
pdt.add(prd);
}
}
if(pdt.size() > 0 ) {
insert pdt;

}
System.debug('pdt '+pdt);

}

 

 

 

 

Thanks

  • December 01, 2012
  • Like
  • 0

hi this is deva i am to sfdc.i have one quesion.is there any chance execute apex code throught commandline  prompt plz send me how to execute.?i want to run run the apex program throught commandline prompt by giving inputs throught keyboard? plz send the example?

public with sharing class ServiceClass 
{
     public static Map<String, RecordType> CaseRecordTypesNameMap
    {
        get
        {
            if(CaseRecordTypesNameMap == null){
                CaseRecordTypesNameMap = new Map<String,RecordType>();
                for(RecordType rt: [Select Id, Name from RecordType where sObjectType = 'Case' ])
                CaseRecordTypesNameMap.put(rt.Name, rt);
            }
            return CaseRecordTypesNameMap;

        }
        
        private set;
    }

     public static Map<String, RecordType> TaskRecordTypesNameMap
    {
        get
        {
            if(TaskRecordTypesNameMap == null){
                TaskRecordTypesNameMap = new Map<String,RecordType>();
                for(RecordType rt: [Select Id, Name from RecordType where sObjectType = 'Task' ])
                TaskRecordTypesNameMap.put(rt.Name, rt);
            }
            return TaskRecordTypesNameMap;

        }
        
        private set;
    }
   
  }

What I need is to be able to pass in sObjectName and the RecordType Name to retrieve a single recordtypeid.

 

ServiceClass.TaskRecordTypesNameMap.get('PM Task').Id

Above is what I'm currently using but instead of having to build a look up function for every object I'd rather get it all done in one single call passing two params sObjectName and RecordType Name.  Any ideas?

 

Thank you,

Steve Laycock

I was previously able to delete PermissionSetAssignment through the REST API. Now I am getting:

 

You can't assign or unassign this permission set because it's associated with a profile.

 

I would think that I was doing exactly that - deleting the assignment to dissociate the profile from the PermissionSet. Any pointers? 

 

thanks

yan at crowdcast dot com