• cml9
  • NEWBIE
  • 45 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 15
    Questions
  • 12
    Replies
Hi Masters,

I have successfully created this trigger and it work as what I suppose to but on my test I only have 5 percent coverage. As per my understanding I need to insert a new Opportunity first then update it.Lastly, create another Opportunity. Please correct me if this is wrong. Below is my trigger and my suppose test that is not working.

Thanks!

Trigger
 
trigger Contracted_Opportunities on Opportunity (after update) {

  for(Opportunity o: trigger.new){
	  if (o.isContracted__c == True && o.StageName=='Sold') {
          
                if (o.Payment_Frequency__c == 'Monthly - Locked In (6)'){
              		integer x = 5;
                    double z = double.valueOf(o.Total_Amount_w_o_GST__c);
                    date closedate = date.valueOf(o.CloseDate);
                    integer payment = 2;
                    integer num = 1;
                  
                    
                   for (integer y =x; y>=1;y--) {
     
                        Opportunity newOpp = new Opportunity();
                        
                        newOpp.Name = 'Monthly Contracted '+ payment;
                        newOpp.Amount=o.Total_Amount_w_o_GST__c;
                        newOpp.StageName='Sold';
                        newOpp.AccountId=o.AccountId;
                        newOpp.Contact__c=o.Contact__c;
                        newOpp.CloseDate =closedate.addMonths(1);
                        
                        insert NewOpp;
                        closedate = closeDate.addMonths(1);
                        payment++;
						num++;
                    }
      
    
 	}
          
          		   if (o.Payment_Frequency__c == 'Monthly - Locked In (12)'){
              		integer x = 11;
                    double z = double.valueOf(o.Total_Amount_w_o_GST__c);
                    date closedate = date.valueOf(o.CloseDate);
                    integer payment = 2;
                    integer num = 1;
                  
                    
                   for (integer y =x; y>=1;y--) {
     
                        Opportunity newOpp = new Opportunity();
                        
                        newOpp.Name = 'Monthly Contracted '+ payment;
                        newOpp.Amount=o.Total_Amount_w_o_GST__c;
                        newOpp.StageName='Sold';
                        newOpp.AccountId=o.AccountId;
                        newOpp.Contact__c=o.Contact__c;
                        newOpp.CloseDate =closedate.addMonths(1);
                        
                        insert NewOpp;
                        closedate = closeDate.addMonths(1);
                        payment++;
						num++;
                    }
      
    
 	}
    }
    }
}

Test
 
@isTest
public class ContractedOppsTest {

    static testMethod void insertNewOpps() {
        
         Account a = new Account();
         a.Name = 'Account';
        
        
        insert a;
        
        Contact c = new Contact();
        c.LastName = 'Kash';
        c.AccountId = a.Id;
        
        insert c;
        
        
   List<Opportunity> o = new List<Opportunity>();
        
       o.add(new Opportunity(
       
           Name = 'My Opps',
           StageName = 'New',
           CloseDate = Date.today().addDays(7),
           Contact__c = c.Id,
           AccountId = a.Id,
           Amount = 100));
           
           insert o;
        
        List <Opportunity> oppids = [ select id from Opportunity where Id = :o[0].Id];
        
         for (Opportunity UpOpp: oppids) {
                     
       		 UpOpp.isContracted__c = False;
      	 	 UpOpp.Payment_Frequency__c = 'Monthly - Locked In (6)';
   		 	 UpOpp.StageName = 'Sold';

           update UpOpp;
             
             Opportunity NewOpp = new Opportunity();
        
        	NewOpp.Name='New Opp1';
        	NewOpp.StageName='Sold';
                Newopp.Amount=upOpp.Total_Amount_w_o_GST__c;
        	NewOpp.Contact__c=upopp.Contact__c;
        	NewOpp.AccountId=upopp.AccountId;
        
        insert NewOpp;
            }
      
      
    }
   
  
}

 
  • November 18, 2016
  • Like
  • 0
trigger AccountSync2 on Account (after insert, after update) {

   List<RecordType> rtypes = [Select Name, Id From RecordType 
                 					 where sObjectType='Account' and isActive=true];
    Map<String,String> accountRecordTypes = new Map<String,String>{};
     
        for(RecordType rt: rtypes)
       		 accountRecordTypes.put(rt.Name,rt.Id);
    
    if (trigger.isInsert) {

         for (Account a: Trigger.new ) {
           String LastName, FirstName;
             
           if (a.Contact_Name__c.indexof(' ') == -1) {
                 LastName =  '-';
               	 FirstName = a.Contact_Name__c;
           } else {
                 LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                 FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
           }   
        
            
           if (a.RecordTypeId==accountRecordTypes.get('Sales Process')){ 
               		if (a.CreatedById == '005p0000000r5oI') { //Use to determine if Account was created through API || Needs to be changed to '00528000004nP9f' before deploying to Prod
                       Decimal myD = a.Business_ID__c; 
                       Integer newInt = myD.intValue();
                       
                       Contact c = New Contact();
                     
                             c.FirstName = FirstName;
                   			 c.LastName = LastName;
                             c.AccountId = a.id;
                             c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
                             c.Phone = a.Contact_Phone__c;
                             c.Business_Id__c = newInt.format().remove(',');
                             c.User_ID__c = a.User_Id__c;
                                
       
              			insert C;
                 	}
         }
    	}
    }
    
    if (trigger.isUpdate) {
      System.debug(Trigger.newMap.keySet());
      List<Account> accountsWithContacts = [select id, User_Id__c, Contact_Name__c, Business_ID__c,Contact_Email__c,Phone,Contact_Phone__c,  (select id, 
                                                                firstname, lastname, email, User_ID__c,Phone,Business_Id__c,MobilePhone from Contacts) 
                                                                from Account where id IN :Trigger.newMap.keySet()];
	  
  List<Contact> contactsToUpdate = new List<Contact>{};
  
 	 for(Account a: accountsWithContacts){
           String LastName, FirstName;
             
           if (a.Contact_Name__c.indexof(' ') == -1) {
                 LastName =  '-';
               	 FirstName = a.Contact_Name__c;
           } else {
                 LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                 FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
           }   
         
    
     	if (a.Contacts.isEmpty()) {
        	             Decimal myD = a.Business_ID__c; 
             Integer newInt = myD.intValue();
                       
                       Contact c = New Contact();
                     
                             c.FirstName = FirstName;
                   			 c.LastName = LastName;
                             c.AccountId = a.id;
                             c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
                             c.MobilePhone = a.Contact_Phone__c;
                             c.Business_Id__c = newInt.format().remove(',');
                             c.User_ID__c = a.User_Id__c;
            
              contactsToUpdate.add(c);  
                 insert contactsToUpdate;
        }
        for(Contact c: a.Contacts) {
         
              if (a.User_Id__c == c.User_Id__c || true) {
               				 c.Description= c.firstName + ' ' + c.lastname; 
               				 c.FirstName = FirstName;
                   			 c.LastName = LastName;
                  			 c.MobilePhone = a.Contact_Phone__c;
                       		 c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
               
   	  			contactsToUpdate.add(c);  
                   update contactsToUpdate;
              }
   	  		
        } 	  
      }
    }   
}

Thanks!
  • September 30, 2016
  • Like
  • 0
Hi Dev Masters,

I am writing a trigger wherein if there is a new Account created in SF it will create a Contact. The trigger was working fine during creation of new Contacts but during UPDATES its not doing anything. I am not sure why this is not working can anyone help? Thanks in advance!
 
trigger AccountSync on Account (after insert, after update) {

    if (trigger.isafter) {
        if (trigger.isInsert) {
   			 for (Account a: Trigger.new ) {
        
      			  if (a.RecordTypeId == '01228000000kgCd') { // Use to check if Record Type is equal to Sales Process 
           			 if (a.CreatedById == '005p0000000r5oI') { //Use to determine if Account was created through API || Needs to be changed to '00528000004nP9f' before deploying to Prod
        
                            Decimal myD = a.Business_ID__c; // Conversion of Double to Integer so that when converted to it will remove .00
                            Integer newInt = myD.intValue();
                                             
                             Contact c = New Contact();
                             c.LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                             c.FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
                             c.AccountId = a.id;
                             c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
                             c.Phone = a.Contact_Phone__c;
                             c.Business_Id__c = newInt.format().remove(',');
                             c.User_ID__c = a.User_Id__c;
                                
       
            insert C;
            }
            }
         } 
       }
        
        if (trigger.isUpdate) {
   			 for (Account a: Trigger.new ) {
        
      			  if (a.RecordTypeId == '01228000000kgCd') { // Use to check if Record Type is equal to Sales Process
           			 if (a.CreatedById == '005p0000000r5oI') { //Use to determine if Account was created through API || Needs to be changed to '00528000004nP9f' before deploying to Prod
        
                            Decimal myD = a.Business_ID__c; // Conversion of Double to Integer so that when converted to it will remove .00
                            Integer newInt = myD.intValue();
                                             
                             Contact c = New Contact();
                             c.LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                             c.FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
                             c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
                             c.Phone = a.Contact_Phone__c;
                             
                            
                                
       
            update C; 
                              }
            }
        } 
       }
    }
       
 
}

 
  • September 28, 2016
  • Like
  • 0
Please help me why its giving my test class is giving me 0% coverage.

Here is my Trigger Code.
 
trigger DirectOpp on Direct_Opportunities__c (before insert) {
   
    Set<Id> contactId = new Set<Id>();
	id accountId;
    date myDate = date.valueOf(system.today());
    
    for (Direct_Opportunities__c d : Trigger.new){
        
	      System.debug(d.Contact__c);
          contactId.add(d.Contact__c);
      
       // Getting the Account Id from Direct Opportunity Contact__c
       // and assigned it to variable aId
       
        accountId = [select AccountId from Contact where id in :contactId].get(0).accountId;
		
        
	// Inserting of New Opportunity
        Opportunity o = new Opportunity();
        o.Name =  ' Direct Opportunity ' + d.Type__c;
        o.CloseDate = myDate + 7;
        o.StageName = 'New';
        o.OwnerId = d.Assigned_BC__c;
        o.AccountId = accountId;
        o.Contact__c = d.Contact__c;
        o.CampaignId = d.Campaign__c;

        insert o;
        
        // Inserting of Tasks
        
        Task t = new Task();
        
        t.Subject = d.Type__c;
        t.ActivityDate = myDate + 1;
	    t.Status = 'Not Started';
        t.Priority = 'High';
        t.OwnerId = d.Assigned_BC__c;
        t.WhatId = o.Id;
        t.WhoId = o.Contact__c;
        
        insert t;
        
        
    }
}

And here is my Test Class.
 
@istest

public class Unit_Test_DirectOpp {

    static testMethod void TestonDirectOpp() {
   		
     test.startTest();

        Account acct = new Account(Name = 'Test Account ');

        insert acct;
     
        Contact cont = new Contact(LastName = 'Last', AccountId = acct.Id );

        insert cont;
    
    	Direct_Opportunities__c d = new Direct_Opportunities__c(Assigned_BC__c = cont.OwnerId, Contact__c = cont.Id, Type__c = 'Type', Campaign__c = 'MyCampaign');


        Opportunity Opp = new Opportunity(AccountId = acct.Id,  Contact__c = cont.Id, Name = 'Test Opportunity', StageName ='New' , CloseDate = date.today());

        insert Opp;

      
        Task t = new Task(Subject='Subject', ActivityDate = date.today(), Status = 'Not Started', Priority = 'High', OwnerId = Opp.OwnerId, WhatId = Opp.Id, WhoId = cont.id);
        insert t;

    
        
        test.stopTest();
       
 }
}

 
  • September 01, 2016
  • Like
  • 0
Hi Experts,

Please help me in mapping out the Account Id from Contact so that I can assign the contact ID to the Opportunity that I created on the Trigger.

Please see code below: 
 
trigger DirectOpp on Direct_Opportunities__c (before insert) {
   
    for (Direct_Opportunities__c d : Trigger.new) {
        date myDate = date.valueOf(system.today());
     
        Opportunity o = new Opportunity();
        o.Name =  ' Direct Opportunity ';
        o.CloseDate = myDate + 7;
        o.StageName = 'New';
        o.OwnerId = d.Assigned_BC__c;
    //  o.AccountId =   I need the account Id here but I was not able to do it
        o.Contact__c = d.Contact__c;
        o.CampaignId = d.Campaign__c;
   
     
        insert o;
        
        Task t = new Task();
        
        t.Subject = 'Subject Type';
        t.ActivityDate = myDate + 1;
	t.Status = 'Not Started';
        t.Priority = 'High';
        t.OwnerId = d.Assigned_BC__c;
        t.WhatId = o.Id;
        
        insert t;
        
        
    }   
}

 
  • August 31, 2016
  • Like
  • 0
Hi Experts,

I am not sure why this is not working, I found this code in the net and I thought this is working just fine. There is an error about list. Please help.
 
trigger triggerOnAttachment on Attachment (before insert) {
    List OppList = new List();
    Set OppIds = new Set();
    for(Attachment att : trigger.New){
         //Check if added attachment is related to Account or not
         if(att.ParentId.getSobjectType() == Oppportunity.SobjectType){
              OppIds.add(att.ParentId);
         }
    }
    accountList = [select id, hasAttachment__c from Opportunity where id in : OppIds];
    if(OppList!=null && OppList.size()>0){
        for(Opportunity Opp : OppList){
            Opp.hasAttachment__c = true;
        }
        update OppList;
    }
}

It is saying Unexpected Error on List. 
  • August 29, 2016
  • Like
  • 0
For some reason I am stucked at 53% and I don't know what I am missing.

here is my trigger:
 
trigger activityTrigger on Task (after insert, after update) {
    set<id> oppIds = new set<id>();
    //map<id,Task> mapTask = new map<id,Task>();
    
    if(trigger.isafter){
        if(trigger.isInsert){
            for(Task loopTask:trigger.new){
                if(loopTask.WhatId != null && string.valueof(loopTask.whatid).startsWith('006')){
                    
                    oppIds.add(loopTask.WhatId);
                    //mapTask.put(loopTask.Id, loopTask);
                }
            }
            
            list<Opportunity> opportunityList = new list<Opportunity>([ SELECT id, test_first_activity__c, 
                                                                        (SELECT Id, Status,Subject,Type, CreatedDate, whatId  FROM Tasks order by CreatedDate asc limit 1) 
                                                                        FROM Opportunity Where id IN: oppIds ]); 
            list<Task> taskList = new list<Task>();
            
            if(oppIds.size() > 0){ 
                for(Opportunity loopOpp : opportunityList  ){
                    
                    if(loopOpp.getSobjects('Tasks') != null && loopOpp.getSobjects('Tasks').size() > 0) 
                    taskList = loopOpp.getSobjects('Tasks'); 
                    
                    if(taskList[0].whatId == loopOpp.id){
                        if(taskList[0].Status != null && taskList[0].Status.equalsignorecase('Completed') ){
                            loopOpp.test_first_activity__c = taskList[0].CreatedDate;
                        }
                    }
                }
            }
            update opportunityList;
        }
        
        if(trigger.isUpdate){
            for(Task loopTask:trigger.new){
                if(loopTask.WhatId != null && string.valueof(loopTask.whatid).startsWith('006')){
                    
                    oppIds.add(loopTask.WhatId);
                    //mapTask.put(loopTask.Id, loopTask);
                }
            }
            
            list<Opportunity> opportunityList = new list<Opportunity>([ SELECT id, test_first_activity__c, 
                                                                        (SELECT Id, Status,Subject,Type, CreatedDate, whatId  FROM Tasks order by CreatedDate asc limit 1) 
                                                                        FROM Opportunity Where id IN: oppIds ]); 
            list<Task> taskList = new list<Task>();
            
            if(oppIds.size() > 0){ 
                for(Opportunity loopOpp : opportunityList  ){
                    
                    if(loopOpp.getSobjects('Tasks') != null && loopOpp.getSobjects('Tasks').size() > 0) 
                    taskList = loopOpp.getSobjects('Tasks'); 
                    
                    if(taskList[0].whatId == loopOpp.id){
                        if(taskList[0].Status != null && taskList[0].Status.equalsignorecase('Completed') ){
                            loopOpp.test_first_activity__c = taskList[0].CreatedDate;
                        }
                    }
                }
            }
            update opportunityList;
        }
    }
    
}

And here is my unit test
 
@isTest
public class PounceTime_Test {
    
    static testMethod void PounceTime() {
        
         Account a = new Account();
   		 a.Name = 'Account';
        
        
        insert a;
        
        Contact c = new Contact();
        c.LastName = 'Kash';
        c.AccountId = a.Id;
        
        insert c;
        
        
        Opportunity o = new Opportunity();
        o.Name = 'MyOpps';
        o.StageName = 'New';
        o.CloseDate = Date.today().addDays(7);
        o.Discount__c = 1;
        o.test_first_activity__c = Datetime.now();
        o.Contact__c = c.Id;
       
      insert o;
   
      
        
        List<Task> tasks = new List<Task>();
		 tasks.add(new Task(
   		 ActivityDate = Date.today().addDays(7),
   		 Subject='Sample Task',
    	 WhatId = o.Id,
         WhoId = c.Id,
         Priority = 'Normal',
   		 OwnerId = UserInfo.getUserId(),
    	 Status='In Progress'));

		insert tasks;
    }

}

Thank you in advance.!
  • August 24, 2016
  • Like
  • 0
What we need is to show the difference between the first Completed Activity and the Opportunity Created Date.

The problem is that the LastActivityDate is a Date field.
Should be like this 
DATETIMEVALUE(LastActivityDate + The Current Hour(of NOW() ))

I have been trying  and reading a lot of post regarding this but it all does not make sense.

Can anyone help me with this?
 
  • August 16, 2016
  • Like
  • 0
User-added image

I have finished the visual force page and the wrapper and now what I wanted to happen is that when a record/s has been Selected then the action page will redirect wherein users will be able to edit the PRICE and add QUANTITY then it will be SAVED to another OBJECT called Sales Product (Sales_Product__c) which is related to Product_Entry__c.

Should I create new visual force page or something? Can someone code this for me?

Here is the code that I used now.
 
<apex:page controller="wrapperExample3">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons location="both">
        <apex:commandButton value="Select and Create Sales Product" /> 
     <!-- I have no Action for the Save yet :( -->
        <apex:commandButton value="Cancel" />
        </apex:pageBlockButtons>
        <apex:pageBlockSection >
        <apex:pageBlockTable value="{!lstWrapperString}" var="wrap">
        <apex:column headerValue="Action">
            <apex:inputCheckbox />
        </apex:column>
        
        <apex:column headerValue="Name">
            {!wrap.Name}
        </apex:column>
        
        <apex:column headerValue="Price">
             {!wrap.Price}
        </apex:column>
        </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
  
</apex:page>

and here is the wrapper.
 
public class wrapperExample3 {

  List <Product_Entry__c> ProdList=new List<Product_Entry__c>();
  List <wrapper> lstw =new List<wrapper>();
  
        public List<wrapper> getlstWrapperString() 
        { 
                ProdList=[select name,Price__c from Product_Entry__c];
                for (Product_Entry__c pe : ProdList)   
                {
                        lstw.add(new wrapper( pe.name, pe.Price__c ) );
                }              
                return lstw;
        }
                      
        public Class wrapper 
        {  
                public string name {get;set;}
                public decimal price {get;set;}
                public wrapper(string Name,decimal Price) 
                {
                        this.Name = Name;
                        this.Price = Price; 
                }
    
    }
}

Thank you all for the Help!
  • August 04, 2016
  • Like
  • 0
public class wrapperExample2 {

  List <Product_Entry__c> ProdList=new List<Product_Entry__c>();
  List <wrapper> lstw =new List<wrapper>();
  
  public List<wrapper> getlstWrapperString() 
  
      { 
              ProdList=[select name,Price__c from Product_Entry__c];
               for (integer i=0; ProdList.size();i++) 
                      {
                  lstw.add(new wrapper(ProdList[i].name,ProdList[i].Price__c));
                      }
              
                      return lstw;
        }
                      
 public Class wrapper {  
     public string name {get;set;}
     public decimal price {get;set;}
     public wrapper(string Name,decimal Price) 
             {
             this.Name = Name;
             this.Price = Price; 
             }
    
     }
  }

 
  • August 03, 2016
  • Like
  • 0
I have three Objects as follows
Sales
Product
Products Entry

What I want to happen is that when I am adding a new product on Sales the picture below is what I am seeing. (the picture below is the list of Product Entry.
User-added image

Once I checked 1 or more and then clicked the button Select, it will bring me to another visual for page where I can edit the price, quatity and description and when I hit Save, it will be saved in Products object.

Hope someone can help me. This is a bit urgent and my job depends on this. Thank you very much.
  • August 03, 2016
  • Like
  • 0
trigger CasesNewCallLog on Task (after insert) {

    list <case> c = new list <case>();
    
    for (Task t:trigger.new){
        if(t.WhatId.getSobjectType() == case.SObjectType){
            if(t.subject == 'Call') {
                c.add(new case(id=t.whatid,Last_Comment_Date__c=System.now()
                              ));
                
            }
        }
    }
    if(c!=null){
        update c;
    }
}

==================

I have created 1 but its not working

@isTest
class UpdateLastCaseComment {

   public static testMethod void caseUpdateLastActivity() {
        
       User u = new User();
        
        u.FirstName = 'You';
        u.LastName = 'Mei';
        u.Alias = 'youmei';
        u.Username = 'youmei@test.com';
        u.Id = '0051a000000Ho3g';
        u.LocaleSidKey = 'en_AU';
        u.EmailEncodingKey = 'ISO-8859-1';
        u.TimeZoneSidKey = 'Asia/Singapore';
        u.Email='youmei@test.com';
       
        
       insert u;
        
        Case c = new Case();
        
           
            c.Status = 'New';
            c.Origin = 'Phone';
            c.GSOT_Role__c = 'Staff';
        
        insert c;
        
        Task t = new Task();
        
            t.OwnerId = u.id;
            t.Subject = 'Anything';
            t.Priority = 'Normal';
            t.WhatId = c.id;
        
        
        
        insert t;
    }
}

BUT ITS NOT WORKING and I DONT KNOW ANYMORE!
  • January 21, 2016
  • Like
  • 0
IF( CloseDate >= Date(2015, 07, 01) && IsClosed == TRUE , Amount , NULL)

I would like to update this so that there is no need for me to update this formula yearly... can anyone help?
  • January 12, 2016
  • Like
  • 0
rigger BulkyEmpSched on Schedule_Creator__c (after insert) {

for (Schedule_Creator__c BSched: Trigger.new ) {
String EmpID = Bsched.Employee_ID__c;
Datetime EmpSched = BSched.Start_Date_and_Time__c;
if (BSched.Start_Date_and_Time__c != NULL) {

    for (integer counter=0; counter <=4; counter ++){
  Employee_Schedule__c nAtt = new Employee_Schedule__c();
  nAtt.Start_Time__c = EmpSched.addDays(counter);
    nAtt.Employee_ID__c= EmpID;
       
insert nAtt;
        
    }
}}}

=======================
Here is my Test Class but its not passing :(

@isTest
Public Class Bulk_Schedule_Creator {
    
    
   static testMethod void insertnewBulkySched () {
        
       Schedule_Creator__c scCreator = new Schedule_Creator__c();
        scCreator.Employee_ID__c    = 'GA-0001';
        scCreator.Start_Date_and_Time__c = Datetime.now();
       
       
        insert scCreator;
    
   Employee_Schedule__c EmpScheds = new Employee_Schedule__c ();
        EmpScheds.Start_TIme__c = Datetime.now();
        EmpScheds.Employee_ID__c = 'GA-0001';
        
        insert EmpScheds;
    }
}



Thank you
  • January 12, 2016
  • Like
  • 0
Hi All,

I am a bit new in SF Developer and I need to create something that can create a WorkSchedule for the whole week/cut off for an employee.
I have created 2 objects already Employee and Work Schedule but I am stuck on how can I create this schedule for the whole week/cut off/month, I cannot create this record 1 @ time.

Currently, I am trying to create our own TimeSheet wherein our HR Manager can create a create a schedule for each employee. This App is currently very small but I am sure once we have created just the simple timesheet it will become big.

I will really appreciate if anyone can help me with this.

Thanks!
  • January 04, 2016
  • Like
  • 0
trigger AccountSync2 on Account (after insert, after update) {

   List<RecordType> rtypes = [Select Name, Id From RecordType 
                 					 where sObjectType='Account' and isActive=true];
    Map<String,String> accountRecordTypes = new Map<String,String>{};
     
        for(RecordType rt: rtypes)
       		 accountRecordTypes.put(rt.Name,rt.Id);
    
    if (trigger.isInsert) {

         for (Account a: Trigger.new ) {
           String LastName, FirstName;
             
           if (a.Contact_Name__c.indexof(' ') == -1) {
                 LastName =  '-';
               	 FirstName = a.Contact_Name__c;
           } else {
                 LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                 FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
           }   
        
            
           if (a.RecordTypeId==accountRecordTypes.get('Sales Process')){ 
               		if (a.CreatedById == '005p0000000r5oI') { //Use to determine if Account was created through API || Needs to be changed to '00528000004nP9f' before deploying to Prod
                       Decimal myD = a.Business_ID__c; 
                       Integer newInt = myD.intValue();
                       
                       Contact c = New Contact();
                     
                             c.FirstName = FirstName;
                   			 c.LastName = LastName;
                             c.AccountId = a.id;
                             c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
                             c.Phone = a.Contact_Phone__c;
                             c.Business_Id__c = newInt.format().remove(',');
                             c.User_ID__c = a.User_Id__c;
                                
       
              			insert C;
                 	}
         }
    	}
    }
    
    if (trigger.isUpdate) {
      System.debug(Trigger.newMap.keySet());
      List<Account> accountsWithContacts = [select id, User_Id__c, Contact_Name__c, Business_ID__c,Contact_Email__c,Phone,Contact_Phone__c,  (select id, 
                                                                firstname, lastname, email, User_ID__c,Phone,Business_Id__c,MobilePhone from Contacts) 
                                                                from Account where id IN :Trigger.newMap.keySet()];
	  
  List<Contact> contactsToUpdate = new List<Contact>{};
  
 	 for(Account a: accountsWithContacts){
           String LastName, FirstName;
             
           if (a.Contact_Name__c.indexof(' ') == -1) {
                 LastName =  '-';
               	 FirstName = a.Contact_Name__c;
           } else {
                 LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                 FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
           }   
         
    
     	if (a.Contacts.isEmpty()) {
        	             Decimal myD = a.Business_ID__c; 
             Integer newInt = myD.intValue();
                       
                       Contact c = New Contact();
                     
                             c.FirstName = FirstName;
                   			 c.LastName = LastName;
                             c.AccountId = a.id;
                             c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
                             c.MobilePhone = a.Contact_Phone__c;
                             c.Business_Id__c = newInt.format().remove(',');
                             c.User_ID__c = a.User_Id__c;
            
              contactsToUpdate.add(c);  
                 insert contactsToUpdate;
        }
        for(Contact c: a.Contacts) {
         
              if (a.User_Id__c == c.User_Id__c || true) {
               				 c.Description= c.firstName + ' ' + c.lastname; 
               				 c.FirstName = FirstName;
                   			 c.LastName = LastName;
                  			 c.MobilePhone = a.Contact_Phone__c;
                       		 c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
               
   	  			contactsToUpdate.add(c);  
                   update contactsToUpdate;
              }
   	  		
        } 	  
      }
    }   
}

Thanks!
  • September 30, 2016
  • Like
  • 0
Hi Dev Masters,

I am writing a trigger wherein if there is a new Account created in SF it will create a Contact. The trigger was working fine during creation of new Contacts but during UPDATES its not doing anything. I am not sure why this is not working can anyone help? Thanks in advance!
 
trigger AccountSync on Account (after insert, after update) {

    if (trigger.isafter) {
        if (trigger.isInsert) {
   			 for (Account a: Trigger.new ) {
        
      			  if (a.RecordTypeId == '01228000000kgCd') { // Use to check if Record Type is equal to Sales Process 
           			 if (a.CreatedById == '005p0000000r5oI') { //Use to determine if Account was created through API || Needs to be changed to '00528000004nP9f' before deploying to Prod
        
                            Decimal myD = a.Business_ID__c; // Conversion of Double to Integer so that when converted to it will remove .00
                            Integer newInt = myD.intValue();
                                             
                             Contact c = New Contact();
                             c.LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                             c.FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
                             c.AccountId = a.id;
                             c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
                             c.Phone = a.Contact_Phone__c;
                             c.Business_Id__c = newInt.format().remove(',');
                             c.User_ID__c = a.User_Id__c;
                                
       
            insert C;
            }
            }
         } 
       }
        
        if (trigger.isUpdate) {
   			 for (Account a: Trigger.new ) {
        
      			  if (a.RecordTypeId == '01228000000kgCd') { // Use to check if Record Type is equal to Sales Process
           			 if (a.CreatedById == '005p0000000r5oI') { //Use to determine if Account was created through API || Needs to be changed to '00528000004nP9f' before deploying to Prod
        
                            Decimal myD = a.Business_ID__c; // Conversion of Double to Integer so that when converted to it will remove .00
                            Integer newInt = myD.intValue();
                                             
                             Contact c = New Contact();
                             c.LastName = a.Contact_Name__c.Substring(a.Contact_Name__c.indexof(' '),a.Contact_Name__c.length());
                             c.FirstName = a.Contact_Name__c.substring(0,a.Contact_Name__c.indexof(' '));
                             c.Phone = a.Phone;
                             c.Email= a.Contact_Email__c;
                             c.Phone = a.Contact_Phone__c;
                             
                            
                                
       
            update C; 
                              }
            }
        } 
       }
    }
       
 
}

 
  • September 28, 2016
  • Like
  • 0
Please help me why its giving my test class is giving me 0% coverage.

Here is my Trigger Code.
 
trigger DirectOpp on Direct_Opportunities__c (before insert) {
   
    Set<Id> contactId = new Set<Id>();
	id accountId;
    date myDate = date.valueOf(system.today());
    
    for (Direct_Opportunities__c d : Trigger.new){
        
	      System.debug(d.Contact__c);
          contactId.add(d.Contact__c);
      
       // Getting the Account Id from Direct Opportunity Contact__c
       // and assigned it to variable aId
       
        accountId = [select AccountId from Contact where id in :contactId].get(0).accountId;
		
        
	// Inserting of New Opportunity
        Opportunity o = new Opportunity();
        o.Name =  ' Direct Opportunity ' + d.Type__c;
        o.CloseDate = myDate + 7;
        o.StageName = 'New';
        o.OwnerId = d.Assigned_BC__c;
        o.AccountId = accountId;
        o.Contact__c = d.Contact__c;
        o.CampaignId = d.Campaign__c;

        insert o;
        
        // Inserting of Tasks
        
        Task t = new Task();
        
        t.Subject = d.Type__c;
        t.ActivityDate = myDate + 1;
	    t.Status = 'Not Started';
        t.Priority = 'High';
        t.OwnerId = d.Assigned_BC__c;
        t.WhatId = o.Id;
        t.WhoId = o.Contact__c;
        
        insert t;
        
        
    }
}

And here is my Test Class.
 
@istest

public class Unit_Test_DirectOpp {

    static testMethod void TestonDirectOpp() {
   		
     test.startTest();

        Account acct = new Account(Name = 'Test Account ');

        insert acct;
     
        Contact cont = new Contact(LastName = 'Last', AccountId = acct.Id );

        insert cont;
    
    	Direct_Opportunities__c d = new Direct_Opportunities__c(Assigned_BC__c = cont.OwnerId, Contact__c = cont.Id, Type__c = 'Type', Campaign__c = 'MyCampaign');


        Opportunity Opp = new Opportunity(AccountId = acct.Id,  Contact__c = cont.Id, Name = 'Test Opportunity', StageName ='New' , CloseDate = date.today());

        insert Opp;

      
        Task t = new Task(Subject='Subject', ActivityDate = date.today(), Status = 'Not Started', Priority = 'High', OwnerId = Opp.OwnerId, WhatId = Opp.Id, WhoId = cont.id);
        insert t;

    
        
        test.stopTest();
       
 }
}

 
  • September 01, 2016
  • Like
  • 0
Hi Experts,

Please help me in mapping out the Account Id from Contact so that I can assign the contact ID to the Opportunity that I created on the Trigger.

Please see code below: 
 
trigger DirectOpp on Direct_Opportunities__c (before insert) {
   
    for (Direct_Opportunities__c d : Trigger.new) {
        date myDate = date.valueOf(system.today());
     
        Opportunity o = new Opportunity();
        o.Name =  ' Direct Opportunity ';
        o.CloseDate = myDate + 7;
        o.StageName = 'New';
        o.OwnerId = d.Assigned_BC__c;
    //  o.AccountId =   I need the account Id here but I was not able to do it
        o.Contact__c = d.Contact__c;
        o.CampaignId = d.Campaign__c;
   
     
        insert o;
        
        Task t = new Task();
        
        t.Subject = 'Subject Type';
        t.ActivityDate = myDate + 1;
	t.Status = 'Not Started';
        t.Priority = 'High';
        t.OwnerId = d.Assigned_BC__c;
        t.WhatId = o.Id;
        
        insert t;
        
        
    }   
}

 
  • August 31, 2016
  • Like
  • 0
Hi Experts,

I am not sure why this is not working, I found this code in the net and I thought this is working just fine. There is an error about list. Please help.
 
trigger triggerOnAttachment on Attachment (before insert) {
    List OppList = new List();
    Set OppIds = new Set();
    for(Attachment att : trigger.New){
         //Check if added attachment is related to Account or not
         if(att.ParentId.getSobjectType() == Oppportunity.SobjectType){
              OppIds.add(att.ParentId);
         }
    }
    accountList = [select id, hasAttachment__c from Opportunity where id in : OppIds];
    if(OppList!=null && OppList.size()>0){
        for(Opportunity Opp : OppList){
            Opp.hasAttachment__c = true;
        }
        update OppList;
    }
}

It is saying Unexpected Error on List. 
  • August 29, 2016
  • Like
  • 0
For some reason I am stucked at 53% and I don't know what I am missing.

here is my trigger:
 
trigger activityTrigger on Task (after insert, after update) {
    set<id> oppIds = new set<id>();
    //map<id,Task> mapTask = new map<id,Task>();
    
    if(trigger.isafter){
        if(trigger.isInsert){
            for(Task loopTask:trigger.new){
                if(loopTask.WhatId != null && string.valueof(loopTask.whatid).startsWith('006')){
                    
                    oppIds.add(loopTask.WhatId);
                    //mapTask.put(loopTask.Id, loopTask);
                }
            }
            
            list<Opportunity> opportunityList = new list<Opportunity>([ SELECT id, test_first_activity__c, 
                                                                        (SELECT Id, Status,Subject,Type, CreatedDate, whatId  FROM Tasks order by CreatedDate asc limit 1) 
                                                                        FROM Opportunity Where id IN: oppIds ]); 
            list<Task> taskList = new list<Task>();
            
            if(oppIds.size() > 0){ 
                for(Opportunity loopOpp : opportunityList  ){
                    
                    if(loopOpp.getSobjects('Tasks') != null && loopOpp.getSobjects('Tasks').size() > 0) 
                    taskList = loopOpp.getSobjects('Tasks'); 
                    
                    if(taskList[0].whatId == loopOpp.id){
                        if(taskList[0].Status != null && taskList[0].Status.equalsignorecase('Completed') ){
                            loopOpp.test_first_activity__c = taskList[0].CreatedDate;
                        }
                    }
                }
            }
            update opportunityList;
        }
        
        if(trigger.isUpdate){
            for(Task loopTask:trigger.new){
                if(loopTask.WhatId != null && string.valueof(loopTask.whatid).startsWith('006')){
                    
                    oppIds.add(loopTask.WhatId);
                    //mapTask.put(loopTask.Id, loopTask);
                }
            }
            
            list<Opportunity> opportunityList = new list<Opportunity>([ SELECT id, test_first_activity__c, 
                                                                        (SELECT Id, Status,Subject,Type, CreatedDate, whatId  FROM Tasks order by CreatedDate asc limit 1) 
                                                                        FROM Opportunity Where id IN: oppIds ]); 
            list<Task> taskList = new list<Task>();
            
            if(oppIds.size() > 0){ 
                for(Opportunity loopOpp : opportunityList  ){
                    
                    if(loopOpp.getSobjects('Tasks') != null && loopOpp.getSobjects('Tasks').size() > 0) 
                    taskList = loopOpp.getSobjects('Tasks'); 
                    
                    if(taskList[0].whatId == loopOpp.id){
                        if(taskList[0].Status != null && taskList[0].Status.equalsignorecase('Completed') ){
                            loopOpp.test_first_activity__c = taskList[0].CreatedDate;
                        }
                    }
                }
            }
            update opportunityList;
        }
    }
    
}

And here is my unit test
 
@isTest
public class PounceTime_Test {
    
    static testMethod void PounceTime() {
        
         Account a = new Account();
   		 a.Name = 'Account';
        
        
        insert a;
        
        Contact c = new Contact();
        c.LastName = 'Kash';
        c.AccountId = a.Id;
        
        insert c;
        
        
        Opportunity o = new Opportunity();
        o.Name = 'MyOpps';
        o.StageName = 'New';
        o.CloseDate = Date.today().addDays(7);
        o.Discount__c = 1;
        o.test_first_activity__c = Datetime.now();
        o.Contact__c = c.Id;
       
      insert o;
   
      
        
        List<Task> tasks = new List<Task>();
		 tasks.add(new Task(
   		 ActivityDate = Date.today().addDays(7),
   		 Subject='Sample Task',
    	 WhatId = o.Id,
         WhoId = c.Id,
         Priority = 'Normal',
   		 OwnerId = UserInfo.getUserId(),
    	 Status='In Progress'));

		insert tasks;
    }

}

Thank you in advance.!
  • August 24, 2016
  • Like
  • 0
What we need is to show the difference between the first Completed Activity and the Opportunity Created Date.

The problem is that the LastActivityDate is a Date field.
Should be like this 
DATETIMEVALUE(LastActivityDate + The Current Hour(of NOW() ))

I have been trying  and reading a lot of post regarding this but it all does not make sense.

Can anyone help me with this?
 
  • August 16, 2016
  • Like
  • 0
public class wrapperExample2 {

  List <Product_Entry__c> ProdList=new List<Product_Entry__c>();
  List <wrapper> lstw =new List<wrapper>();
  
  public List<wrapper> getlstWrapperString() 
  
      { 
              ProdList=[select name,Price__c from Product_Entry__c];
               for (integer i=0; ProdList.size();i++) 
                      {
                  lstw.add(new wrapper(ProdList[i].name,ProdList[i].Price__c));
                      }
              
                      return lstw;
        }
                      
 public Class wrapper {  
     public string name {get;set;}
     public decimal price {get;set;}
     public wrapper(string Name,decimal Price) 
             {
             this.Name = Name;
             this.Price = Price; 
             }
    
     }
  }

 
  • August 03, 2016
  • Like
  • 0
I have three Objects as follows
Sales
Product
Products Entry

What I want to happen is that when I am adding a new product on Sales the picture below is what I am seeing. (the picture below is the list of Product Entry.
User-added image

Once I checked 1 or more and then clicked the button Select, it will bring me to another visual for page where I can edit the price, quatity and description and when I hit Save, it will be saved in Products object.

Hope someone can help me. This is a bit urgent and my job depends on this. Thank you very much.
  • August 03, 2016
  • Like
  • 0
Hi All,

I am a bit new in SF Developer and I need to create something that can create a WorkSchedule for the whole week/cut off for an employee.
I have created 2 objects already Employee and Work Schedule but I am stuck on how can I create this schedule for the whole week/cut off/month, I cannot create this record 1 @ time.

Currently, I am trying to create our own TimeSheet wherein our HR Manager can create a create a schedule for each employee. This App is currently very small but I am sure once we have created just the simple timesheet it will become big.

I will really appreciate if anyone can help me with this.

Thanks!
  • January 04, 2016
  • Like
  • 0