• vivek singh 26
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 1
    Likes Given
  • 12
    Questions
  • 12
    Replies
As discussed I would like to get two emails that are automatically sent each day plus a weekly digest on a Monday morning.  I have attached a report that we manually do each week and an example of an email from the Register.
public with sharing class clsNoticeToQuit {

  public boolean ShowHQ {get; set;}
    public boolean HideHQ {get; set;}
    public Date dDate {get; set;}
    public UserRole objUserRole {get; set;}
   
   
    public clsNoticeToQuit(ApexPages.StandardController controller)
    {
      if(ApexPages.currentPage().getParameters().get('dd')!=null){
      dDate = Date.valueOf(ApexPages.currentPage().getParameters().get('dd'));
      }
      string RoleId = UserInfo.getUserRoleId();
    objUserRole = [Select u.Id, u.Name from UserRole u where id =:RoleId];
    if(objUserRole.Name == 'L4L HQ Partner Manager' || objUserRole.Name=='CEO')
            {
               ShowHQ = true;
               HideHQ = false;
            }
        else
        {
             ShowHQ = false;
               HideHQ = true;
        }
   
    }
global class CallIntermediateTRpageEmail
{
    
   WebService static void  CallIntermedateClass(string TenantId)
   {
        system.debug('TenantId'+TenantId);
        IntermediateTRpageEmail CallIntermediate = new IntermediateTRpageEmail(TenantId);
        CallIntermediate.Returnpage();
   }  
   
}
public class ViewDispatchOrder
{

  

    public list<Contact> lstContact{get; Set;}
    public transient Cookie ContactID {get; set;}
    public string contact {get; set;}
    public list<omsPackList__c>  lstPacklist {get; set;}
    public omsDispatchOrder__c objDispatchOrder{get; set;}
    public list<omsDispatchOrderLineItem__c> lstDOLI {get; set;}
    public  List<omsInventory__c> lstNewInv {get; set;}

    public ViewDispatchOrder()
    {
            ContactID = ApexPages.currentPage().getCookies().get('ContactID');
            if (ContactID!= null)
            {
            contact = ContactID.getValue();
            lstContact= [select id, name, Contact.Account.Name from Contact where id=:contact limit 1];
            }
            objDispatchOrder = [select id,Name,omsWarehouse__c,omsWarehouse__r.Name,omsExpectedDispatchDate__c,
           
            (Select Id, omsProduct__c, omsQuantity__c,
                                           omsDispatchOrderLine__c,omsDispatchOrder__c,
                                           omsSalesOrder__c
                                           From omsInventoryIssueds__r),
            omsStatus__c,omsSalesOrder__r.omsAccount__c from omsDispatchOrder__c where id=:ApexPages.currentPage().getParameters().get('DispatchOrderid')];
           
          
            lstDOLI = [select id,name,omsProduct__c,omsStatus__c,omsProduct__r.name,omsQuantity__c from omsDispatchOrderLineItem__c where  omsDispatchOrder__c =:objDispatchOrder.id];
           
            lstPacklist = [Select Id, Name, omsDispatchOrder__c, omsFreightCharges__c, fkids_Number_of_Packets__c,omsPackDate__c,
                            omsPackingCharge__c, omsPicklist__c,omsPicklist__r.Name, omsSizeDimension__c, omsStatus__c,
                            omsTotal_Packlist_Items__c, omsTotal_PLI_Cancelled_Records__c, omsTotal_PLI_Dispatched_Records__c,
                            omsTotal_PLI_Pending_Records__c, omsTypeOfPacket__c, omsWeightOfPacket__c,
                            (select ID,Name,omsProduct__c, omsQuantity__c,omsStatus__c,omsPackList__r.omsFreightCharges__c,
                            omsPackList__r.omsPackingCharge__c from PackList_Items__r)
                            from omsPackList__c where omsDispatchOrder__c =: objDispatchOrder.id  and omsStatus__c != 'Dispatched' order by omsStatus__c desc];
                           
                 
    }
     public PageReference ReceiveGoods()
     {
           
            lstNewInv = new List<omsInventory__c>();
            Map<String, omsInventoryIssued__c > mapUniqe = new Map<String, omsInventoryIssued__c >();
           
            list<omsPackList__c > lstUpdatePacklist = new list<omsPackList__c>();
           
            list<omsDispatchOrderLineItem__c> lstUpdateDOLI = new list<omsDispatchOrderLineItem__c>();
           
             for(omsPackList__c P:lstPacklist)
             {
                 P.omsStatus__c ='Dispatched';
                 lstUpdatePacklist.add(P);
             }
            
             if(lstUpdatePacklist.size() > 0)
             {
                 update lstUpdatePacklist;
             }
             for(omsDispatchOrderLineItem__c objDOLI:lstDOLI)
             {
                 objDOLI.omsStatus__c = 'Dispatched';
                 lstUpdateDOLI .add(objDOLI);
                
             }
              if(lstUpdateDOLI.size() > 0)
             {
                 update lstUpdateDOLI;
             }
           
             if(objDispatchOrder != null && objDispatchOrder.omsInventoryIssueds__r.size() > 0)
            {
                    for(omsInventoryIssued__c objInvIss: objDispatchOrder.omsInventoryIssueds__r)
                    {
                        string UniqueVal = string.valueof(objDispatchOrder.omsSalesOrder__r.omsAccount__c).substring(0,15) + String.ValueOf(objInvIss.omsProduct__c).SubString(0,15);
                        mapUniqe.put(UniqueVal,objInvIss);
                    }
            }
        Map<String, omsInventory__c> mapUniqInv = new Map<String, omsInventory__c>();
        if(objDispatchOrder.omsInventoryIssueds__r != null)
        {
              for(omsInventory__c INV : [select Id, omsUniqueFieldAccount__c,omsProduct__c from omsInventory__c where omsUniqueFieldAccount__c IN: mapUniqe.keySet()]){
                mapUniqInv.put(INV.omsUniqueFieldAccount__c, INV);
              }
              RecordType objRT = [select id,Name,sObjectType from recordtype where name = 'Virtual Inventory' and sObjectType = 'omsInventory__c'];
              for(String UNIQ : mapUniqe.keySet()){
                if(mapUniqInv.get(UNIQ) == null){
                    omsInventory__c inv = new omsInventory__c();
                    inv.omsProduct__c = mapUniqe.get(UNIQ).omsProduct__c;
                    inv.omsAccount__c = objDispatchOrder.omsSalesOrder__r.omsAccount__c;
                    inv.recordtypeid = objRT.id;
                    lstNewInv.add(inv);
                }
            }
            if(lstNewInv.size() > 0){
                insert lstNewInv;
            }
            system.debug('Ankita-----'+lstNewInv);
            for(omsInventory__c INV : [select Id, omsUniqueField__c,omsUniqueFieldAccount__c, omsAccount__c,omsProduct__c,RecordType.Name from omsInventory__c where Id IN: lstNewInv]){
                mapUniqInv.put(INV.omsUniqueFieldAccount__c, INV);
            }
            system.debug('Ankita111-----'+mapUniqe.keySet());
            //Create New Inventory Receive:
            list<omsInventoryReceived__c> lstINVREC = new list<omsInventoryReceived__c>();
            for(String UNIQ : mapUniqe.keySet()){
                if(mapUniqe.get(UNIQ) != null){
                    omsInventoryReceived__c objInvReceived = new omsInventoryReceived__c();
                    objInvReceived.omsInventory__c = mapUniqInv.get(UNIQ).Id;
                    objInvReceived.omsProduct__c = mapUniqe.get(UNIQ).omsProduct__c;
                    objInvReceived.fkids_Order__c = mapUniqe.get(UNIQ).omsSalesOrder__c;
                    objInvReceived.fkids_Dispatch_Order_Line_Item__c = mapUniqe.get(UNIQ).omsDispatchOrderLine__c;
                    objInvReceived.omsQuantity__c = mapUniqe.get(UNIQ).omsQuantity__c;
                    lstINVREC.add(objInvReceived);
                }
            }
            system.debug('Ankita@@@@'+lstINVREC);
            if(lstINVREC.size() > 0){
                insert lstINVREC;
            }
        }
         PageReference pagel = new PageReference('/apex/Dashboard');
          pagel.setRedirect(true);
          return pagel;
          
       
     }
    public PageReference signout() {
        ContactID = new Cookie ('ContactID','', null,0, false);
         ApexPages.currentPage().setCookies(new Cookie[]{ContactID});
         PageReference pagel = new PageReference('/apex/fkitLogin');
          pagel.setRedirect(true);
          return pagel;
    }
    public pageReference isLogin(){
    
        ContactID = ApexPages.currentPage().getCookies().get('ContactID');
       
        if(ContactID == null){
            PageReference pagel = new PageReference('/apex/fkitLogin');
            pagel.setRedirect(true);
            return pagel;
           }
        else{
            return null;
        }
     }
}
You need to create a Custom field on HOA Setting called Default Account Id

you need to create a workflow rule on Contact so that whenever an account is created with blank contact , this Id will be stored in the Account id of the contact.'
Hi

Is there any chance you can take the information from the 'Overview' Tab within Salesforce and get this sent as an automatic email on the first Monday of every month ?

Sort of like a HTML email ? I'd need this sending to Primary and Secondary Email contacts.

Let me know.

How we make it through trigger or workflow please suggest
Create a visualforce page that will show all accounts
paged by Alphabets
There wil be a button called Mass edit
on click of it the account list will be in editable mode.
There will be a button called Save
 on click of this button all the changes made will be saved to salesforce
TRigger
---------------------------------
trigger OpportunityTrigger on Opportunity (After insert,After update)
{
    if(trigger.isAfter )
   
    {
        if(trigger.isInsert){
            List<OpportunityContactRole> lstContactRoletoUpdate = new List<OpportunityContactRole>();
            Set<String> setOppId = new Set<String>();
                    system.debug('!!!!After insert');
                    List<OpportunityContactRole> lstOppContactRolePrimary = new List<opportunityContactRole>();
                    for(Opportunity o:trigger.new)
                    {
                        setOppId.add(String.valueOf(o.id));
                        system.debug('@@@@@setOppId '+(String.valueOf(o.id)) );
                    }
           
                    lstOppContactRolePrimary = [select id,contactId,opportunityId from OpportunityContactRole where opportunityId in :setOppId];
                    //no primaryContact role found at all have make from scratch
                    system.debug('lstOppContactRolePrimary '+lstOppContactRolePrimary );
                    if(lstOppContactRolePrimary.size() > 0)
                    {
                        Map<String,OpportunityContactRole> mapOppIdConrole = new Map<String,OpportunityContactRole>();
                        for(OpportunityContactRole conRole:lstOppContactRolePrimary)
                        {
                            mapOppIdConrole.put(String.valueOf(conRole.opportunityId),conRole);
                        }
                        for(String id:setOppId)
                        {
                            if(mapOppIdConrole.containsKey(id))
                            {
                                Opportunity oppTeamp = trigger.newMap.get(id);
                                if(oppTeamp.buyer__c == mapOppIdConrole.get(id).contactId)
                                {
                                    //both are equal , we will just have to update the role
                                    OpportunityContactRole tempRole = mapOppIdConrole.get(id);
                                    tempRole.Role = 'Buyer';
                                    lstContactRoletoUpdate.add(tempRole);
                                }
                                else
                                {
                                    //we have the contact role but the contacts are different
                                    OpportunityContactRole tempRole = mapOppIdConrole.get(id);
                                    tempRole.Role = 'Buyer';
                                    tempRole.contactId = oppTeamp.buyer__c;
                                    lstContactRoletoUpdate.add(tempRole);
                                }
                            }else
                            {
                                // we have to make contact role
                                System.debug('####Creating absolutely new one 1');
                                OpportunityContactRole tempRole = new OpportunityContactRole();
                                tempRole.Role = 'Buyer';
                                tempRole.contactId = trigger.newMap.get(id).buyer__c;
                                tempRole.isPrimary = true;
                                lstContactRoletoUpdate.add(tempRole);
                            }
                        }
                    }else
                    {
                        System.debug('####Creating absolutely new one 2');
                        opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
                        objopportunityTriggerHandler.createNewContact(trigger.new);
                    }
                if(lstContactRoletoUpdate.size() > 0)
                {
                    upsert lstContactRoletoUpdate;
                }  
           
        }
    }
    /*
    if(trigger.isBefore)
    {
        if(trigger.isInsert)
        {
                    system.debug('!!!!Before Insert');
                    for(Opportunity o:trigger.new)
                {
                    List<OpportunityContactRole> lstOppContactRolePrimary = [select id,contactId from OpportunityContactRole where opportunityId =: o.id];
                    System.debug('!!!!Before insert contact id'+o);
                    System.debug('!!!!Before insert buyer id'+o.buyer__c);
                    System.debug('!!!!Before lstOppContactRolePrimary = >'+lstOppContactRolePrimary);
                }
           
        }
    }
    */ 
        //opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
        //objopportunityTriggerHandler.createNewContact(trigger.new);
   
   
    //code added by deepak dhingra on 13/6/2014 to update the line items
     if(trigger.isAfter && trigger.isUpdate)
     {
        Set<string> qualifiedOppId = new Set<string>();
        for(Opportunity o:Trigger.new)
        {
            if(o.Agency_Discount_Percentage__c != Trigger.Oldmap.get(o.id).Agency_Discount_Percentage__c)
            {
                qualifiedOppId.add(String.valueOf(o.id).left(15));
            }
           
        }
       
        if(qualifiedOppId.size() > 0)
        {
            List<OpportunityLineItem> lstLineItems = new List<OpportunityLineItem>();
            lstLineItems  = [select id from OpportunityLineItem where opportunityid in :qualifiedOppId];
            if(lstLineItems.size() > 0)
            {
                update lstLineItems;
            }
        }


_______________________________

TRACKER CLASSSSSSS



@isTest
public class OpportunityTriggerHandlerTest
{
static testMethod void insertOpportunity()
{
Account objAccount = new Account();
objAccount.Name = 'Vivek';
objAccount.Type = 'Supplier';
objAccount.Phone = '1234';
objAccount.CurrencyIsoCode = 'AUD';
insert objAccount;

Contact objcontact = new contact();
objcontact.LastName = 'Singh';
objcontact.CurrencyIsoCode = 'AUD';
objcontact.Accountid = objAccount.id ;
insert objContact;

opportunity objopportunity= new opportunity ();
objopportunity.Name = 'Hindi2014';
objopportunity.CloseDate = System.Today();
objopportunity.Accountid = objAccount.id;
objopportunity.Buyer__c = objcontact.id;
objopportunity.Vendor__c = objaccount.id;
objopportunity.StageName = 'Prposal Sent';
objopportunity.Billing_Terms__c = 'On Completion';              
objopportunity.Type = 'New Business';
insert Objopportunity;

OpportunityContactRole objOpportunityContactRole=new OpportunityContactRole();
   objOpportunityContactRole.ContactId = Objopportunity.Buyer__c;
   objOpportunityContactRole.OpportunityId = Objopportunity.Id;
   objOpportunityContactRole.Role = 'Buyer';
  insert objOpportunityContactRole;

  List<OpportunityContactRole> lstOpportunityContactRole = new List<OpportunityContactRole>();
           //OpportunityContactRole = [select id from OpportunityContactRole where OpportunityContactRole ='Buyer'];

}
static testMethod void updateOpp()
{
  opportunity objopportunity2= new opportunity ();
  objopportunity2.Name = 'Hindi2015';
  //upadte Objopportunity2;
}
}
/*Date.............12/06/2014,VivekSingh
Discription.......
This trigger is work on the opportunity object this is bulkify trigger update contact role in opportuniy
*/


trigger OpportunityTrigger on Opportunity (After insert,After update)
{
    if(trigger.isAfter )
   
    {
        if(trigger.isInsert){
            List<OpportunityContactRole> lstContactRoletoUpdate = new List<OpportunityContactRole>();
            Set<String> setOppId = new Set<String>();
                    system.debug('!!!!After insert');
                    List<OpportunityContactRole> lstOppContactRolePrimary = new List<opportunityContactRole>();
                    for(Opportunity o:trigger.new)
                    {
                        setOppId.add(String.valueOf(o.id));
                    }
                    lstOppContactRolePrimary = [select id,contactId,opportunityId from OpportunityContactRole where opportunityId in :setOppId];
                    no primaryContact role found at all have make from scratch
                    if(lstOppContactRolePrimary.size() > 0)
                    {
                        Map<String,OpportunityContactRole> mapOppIdConrole = new Map<String,OpportunityContactRole>();
                        for(OpportunityContactRole conRole:lstOppContactRolePrimary)
                        {
                            mapOppIdConrole.put(String.valueOf(conRole.opportunityId),conRole);
                        }
                        for(String id:setOppId)
                        {
                            if(mapOppIdConrole.containsKey(id))
                            {
                                Opportunity oppTeamp = trigger.newMap.get(id);
                                if(oppTeamp.buyer__c == mapOppIdConrole.get(id).contactId)
                                {
                                    both are equal , we will just have to update the role
                                    OpportunityContactRole tempRole = mapOppIdConrole.get(id);
                                    tempRole.Role = 'Buyer';
                                    lstContactRoletoUpdate.add(tempRole);
                                }
                                else
                                {
                                    we have the contact role but the contacts are different
                                    OpportunityContactRole tempRole = mapOppIdConrole.get(id);
                                    tempRole.Role = 'Buyer';
                                    tempRole.contactId = oppTeamp.buyer__c;
                                    lstContactRoletoUpdate.add(tempRole);
                                }
                            }else
                            {
                                 we have to make contact role
                                System.debug('####Creating absolutely new one 1');
                                OpportunityContactRole tempRole = new OpportunityContactRole();
                                tempRole.Role = 'Buyer';
                                tempRole.contactId = trigger.newMap.get(id).buyer__c;
                                tempRole.isPrimary = true;
                                lstContactRoletoUpdate.add(tempRole);
                            }
                        }
                    }else
                    {
                        System.debug('####Creating absolutely new one 2');
                        opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
                        objopportunityTriggerHandler.createNewContact(trigger.new);
                    }
                if(lstContactRoletoUpdate.size() > 0)
                {
                    upsert lstContactRoletoUpdate;
                }  
           
        }
    }
   
    if(trigger.isBefore)
    {
        if(trigger.isInsert)
        {
                    system.debug('!!!!Before Insert');
                    for(Opportunity o:trigger.new)
                {
                    List<OpportunityContactRole> lstOppContactRolePrimary = [select id,contactId from OpportunityContactRole where opportunityId =: o.id];
                    System.debug('!!!!Before insert contact id'+o);
                    System.debug('!!!!Before insert buyer id'+o.buyer__c);
                    System.debug('!!!!Before lstOppContactRolePrimary = >'+lstOppContactRolePrimary);
                }
           
        }
    }
    
        opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
        objopportunityTriggerHandler.createNewContact(trigger.new);
   
   
    code added by deepak dhingra on 13/6/2014 to update the line items
     if(trigger.isAfter && trigger.isUpdate)
     {
        Set<string> qualifiedOppId = new Set<string>();
        for(Opportunity o:Trigger.new)
        {
            if(o.Agency_Discount_Percentage__c != Trigger.Oldmap.get(o.id).Agency_Discount_Percentage__c)
            {
                qualifiedOppId.add(String.valueOf(o.id).left(15));
            }
           
        }
       
        if(qualifiedOppId.size() > 0)
        {
            List<OpportunityLineItem> lstLineItems = new List<OpportunityLineItem>();
            lstLineItems  = [select id from OpportunityLineItem where opportunityid in :qualifiedOppId];
            if(lstLineItems.size() > 0)
            {
                update lstLineItems;
            }
        }
       
       
     }
}
Example ----When we fullfill contact then opporetunity then two contact role

Action            Contact  Name        Company Name           Email                                                        Phone                           Role                       Primary
Edit |  Del     Mark Sze                  informgroup.com            mark.sze@informgroup.com                                                                                    Checked
Edit | Del       Mark Sze                 informgroup.com            mark.sze@informgroup.com                                                      Buyer                         Checked

_______________________________________________________________________________________
When we fill oportunity then only one contact role ___
Action            Contact  Name        Company Name              Email                                                        Phone                           Role                        Primary
Edit |  Del     Mark Sze                  informgroup.com            mark.sze@informgroup.com                                                                                         Checked

How i solve it and i want to only one contact role in under opportunity







Trigger-------------
________________________
trigger OpportunityTrigger on Opportunity (After insert)
{
    if(trigger.isAfter && trigger.isInsert)
    { system.debug('After insert');
        opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
        objopportunityTriggerHandler.createNewContact(trigger.new);
    }
}

________________________________
Class is _______________________________
Public class opportunityTriggerHandler
{
   
    public void createNewContact(List<Opportunity> triggerNew)
    {
        List<OpportunityContactRole> lstOppContactRole = new list<OpportunityContactRole>();
        if(triggerNew != null && triggerNew.size() > 0)
        {
            for(Opportunity objOpp : triggerNew)
            {
                if(objOpp.Buyer__c != null)
                {
                    OpportunityContactRole objOppContactRole = new OpportunityContactRole();
                   
                    objOppContactRole.ContactId = objOpp.Buyer__c;
                    objOppContactRole.OpportunityId = objOpp.ID;
                    objOppContactRole.Role = 'Buyer';
                   
                    lstOppContactRole.add(objOppContactRole );
                }
            }
        }
       
        if(lstOppContactRole.size() > 0)
      
       insert lstOppContactRole;
       
    }
}




You need to create a Custom field on HOA Setting called Default Account Id

you need to create a workflow rule on Contact so that whenever an account is created with blank contact , this Id will be stored in the Account id of the contact.'
global class CallIntermediateTRpageEmail
{
    
   WebService static void  CallIntermedateClass(string TenantId)
   {
        system.debug('TenantId'+TenantId);
        IntermediateTRpageEmail CallIntermediate = new IntermediateTRpageEmail(TenantId);
        CallIntermediate.Returnpage();
   }  
   
}
You need to create a Custom field on HOA Setting called Default Account Id

you need to create a workflow rule on Contact so that whenever an account is created with blank contact , this Id will be stored in the Account id of the contact.'
Create a visualforce page that will show all accounts
paged by Alphabets
There wil be a button called Mass edit
on click of it the account list will be in editable mode.
There will be a button called Save
 on click of this button all the changes made will be saved to salesforce
TRigger
---------------------------------
trigger OpportunityTrigger on Opportunity (After insert,After update)
{
    if(trigger.isAfter )
   
    {
        if(trigger.isInsert){
            List<OpportunityContactRole> lstContactRoletoUpdate = new List<OpportunityContactRole>();
            Set<String> setOppId = new Set<String>();
                    system.debug('!!!!After insert');
                    List<OpportunityContactRole> lstOppContactRolePrimary = new List<opportunityContactRole>();
                    for(Opportunity o:trigger.new)
                    {
                        setOppId.add(String.valueOf(o.id));
                        system.debug('@@@@@setOppId '+(String.valueOf(o.id)) );
                    }
           
                    lstOppContactRolePrimary = [select id,contactId,opportunityId from OpportunityContactRole where opportunityId in :setOppId];
                    //no primaryContact role found at all have make from scratch
                    system.debug('lstOppContactRolePrimary '+lstOppContactRolePrimary );
                    if(lstOppContactRolePrimary.size() > 0)
                    {
                        Map<String,OpportunityContactRole> mapOppIdConrole = new Map<String,OpportunityContactRole>();
                        for(OpportunityContactRole conRole:lstOppContactRolePrimary)
                        {
                            mapOppIdConrole.put(String.valueOf(conRole.opportunityId),conRole);
                        }
                        for(String id:setOppId)
                        {
                            if(mapOppIdConrole.containsKey(id))
                            {
                                Opportunity oppTeamp = trigger.newMap.get(id);
                                if(oppTeamp.buyer__c == mapOppIdConrole.get(id).contactId)
                                {
                                    //both are equal , we will just have to update the role
                                    OpportunityContactRole tempRole = mapOppIdConrole.get(id);
                                    tempRole.Role = 'Buyer';
                                    lstContactRoletoUpdate.add(tempRole);
                                }
                                else
                                {
                                    //we have the contact role but the contacts are different
                                    OpportunityContactRole tempRole = mapOppIdConrole.get(id);
                                    tempRole.Role = 'Buyer';
                                    tempRole.contactId = oppTeamp.buyer__c;
                                    lstContactRoletoUpdate.add(tempRole);
                                }
                            }else
                            {
                                // we have to make contact role
                                System.debug('####Creating absolutely new one 1');
                                OpportunityContactRole tempRole = new OpportunityContactRole();
                                tempRole.Role = 'Buyer';
                                tempRole.contactId = trigger.newMap.get(id).buyer__c;
                                tempRole.isPrimary = true;
                                lstContactRoletoUpdate.add(tempRole);
                            }
                        }
                    }else
                    {
                        System.debug('####Creating absolutely new one 2');
                        opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
                        objopportunityTriggerHandler.createNewContact(trigger.new);
                    }
                if(lstContactRoletoUpdate.size() > 0)
                {
                    upsert lstContactRoletoUpdate;
                }  
           
        }
    }
    /*
    if(trigger.isBefore)
    {
        if(trigger.isInsert)
        {
                    system.debug('!!!!Before Insert');
                    for(Opportunity o:trigger.new)
                {
                    List<OpportunityContactRole> lstOppContactRolePrimary = [select id,contactId from OpportunityContactRole where opportunityId =: o.id];
                    System.debug('!!!!Before insert contact id'+o);
                    System.debug('!!!!Before insert buyer id'+o.buyer__c);
                    System.debug('!!!!Before lstOppContactRolePrimary = >'+lstOppContactRolePrimary);
                }
           
        }
    }
    */ 
        //opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
        //objopportunityTriggerHandler.createNewContact(trigger.new);
   
   
    //code added by deepak dhingra on 13/6/2014 to update the line items
     if(trigger.isAfter && trigger.isUpdate)
     {
        Set<string> qualifiedOppId = new Set<string>();
        for(Opportunity o:Trigger.new)
        {
            if(o.Agency_Discount_Percentage__c != Trigger.Oldmap.get(o.id).Agency_Discount_Percentage__c)
            {
                qualifiedOppId.add(String.valueOf(o.id).left(15));
            }
           
        }
       
        if(qualifiedOppId.size() > 0)
        {
            List<OpportunityLineItem> lstLineItems = new List<OpportunityLineItem>();
            lstLineItems  = [select id from OpportunityLineItem where opportunityid in :qualifiedOppId];
            if(lstLineItems.size() > 0)
            {
                update lstLineItems;
            }
        }


_______________________________

TRACKER CLASSSSSSS



@isTest
public class OpportunityTriggerHandlerTest
{
static testMethod void insertOpportunity()
{
Account objAccount = new Account();
objAccount.Name = 'Vivek';
objAccount.Type = 'Supplier';
objAccount.Phone = '1234';
objAccount.CurrencyIsoCode = 'AUD';
insert objAccount;

Contact objcontact = new contact();
objcontact.LastName = 'Singh';
objcontact.CurrencyIsoCode = 'AUD';
objcontact.Accountid = objAccount.id ;
insert objContact;

opportunity objopportunity= new opportunity ();
objopportunity.Name = 'Hindi2014';
objopportunity.CloseDate = System.Today();
objopportunity.Accountid = objAccount.id;
objopportunity.Buyer__c = objcontact.id;
objopportunity.Vendor__c = objaccount.id;
objopportunity.StageName = 'Prposal Sent';
objopportunity.Billing_Terms__c = 'On Completion';              
objopportunity.Type = 'New Business';
insert Objopportunity;

OpportunityContactRole objOpportunityContactRole=new OpportunityContactRole();
   objOpportunityContactRole.ContactId = Objopportunity.Buyer__c;
   objOpportunityContactRole.OpportunityId = Objopportunity.Id;
   objOpportunityContactRole.Role = 'Buyer';
  insert objOpportunityContactRole;

  List<OpportunityContactRole> lstOpportunityContactRole = new List<OpportunityContactRole>();
           //OpportunityContactRole = [select id from OpportunityContactRole where OpportunityContactRole ='Buyer'];

}
static testMethod void updateOpp()
{
  opportunity objopportunity2= new opportunity ();
  objopportunity2.Name = 'Hindi2015';
  //upadte Objopportunity2;
}
}
/*Date.............12/06/2014,VivekSingh
Discription.......
This trigger is work on the opportunity object this is bulkify trigger update contact role in opportuniy
*/


trigger OpportunityTrigger on Opportunity (After insert,After update)
{
    if(trigger.isAfter )
   
    {
        if(trigger.isInsert){
            List<OpportunityContactRole> lstContactRoletoUpdate = new List<OpportunityContactRole>();
            Set<String> setOppId = new Set<String>();
                    system.debug('!!!!After insert');
                    List<OpportunityContactRole> lstOppContactRolePrimary = new List<opportunityContactRole>();
                    for(Opportunity o:trigger.new)
                    {
                        setOppId.add(String.valueOf(o.id));
                    }
                    lstOppContactRolePrimary = [select id,contactId,opportunityId from OpportunityContactRole where opportunityId in :setOppId];
                    no primaryContact role found at all have make from scratch
                    if(lstOppContactRolePrimary.size() > 0)
                    {
                        Map<String,OpportunityContactRole> mapOppIdConrole = new Map<String,OpportunityContactRole>();
                        for(OpportunityContactRole conRole:lstOppContactRolePrimary)
                        {
                            mapOppIdConrole.put(String.valueOf(conRole.opportunityId),conRole);
                        }
                        for(String id:setOppId)
                        {
                            if(mapOppIdConrole.containsKey(id))
                            {
                                Opportunity oppTeamp = trigger.newMap.get(id);
                                if(oppTeamp.buyer__c == mapOppIdConrole.get(id).contactId)
                                {
                                    both are equal , we will just have to update the role
                                    OpportunityContactRole tempRole = mapOppIdConrole.get(id);
                                    tempRole.Role = 'Buyer';
                                    lstContactRoletoUpdate.add(tempRole);
                                }
                                else
                                {
                                    we have the contact role but the contacts are different
                                    OpportunityContactRole tempRole = mapOppIdConrole.get(id);
                                    tempRole.Role = 'Buyer';
                                    tempRole.contactId = oppTeamp.buyer__c;
                                    lstContactRoletoUpdate.add(tempRole);
                                }
                            }else
                            {
                                 we have to make contact role
                                System.debug('####Creating absolutely new one 1');
                                OpportunityContactRole tempRole = new OpportunityContactRole();
                                tempRole.Role = 'Buyer';
                                tempRole.contactId = trigger.newMap.get(id).buyer__c;
                                tempRole.isPrimary = true;
                                lstContactRoletoUpdate.add(tempRole);
                            }
                        }
                    }else
                    {
                        System.debug('####Creating absolutely new one 2');
                        opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
                        objopportunityTriggerHandler.createNewContact(trigger.new);
                    }
                if(lstContactRoletoUpdate.size() > 0)
                {
                    upsert lstContactRoletoUpdate;
                }  
           
        }
    }
   
    if(trigger.isBefore)
    {
        if(trigger.isInsert)
        {
                    system.debug('!!!!Before Insert');
                    for(Opportunity o:trigger.new)
                {
                    List<OpportunityContactRole> lstOppContactRolePrimary = [select id,contactId from OpportunityContactRole where opportunityId =: o.id];
                    System.debug('!!!!Before insert contact id'+o);
                    System.debug('!!!!Before insert buyer id'+o.buyer__c);
                    System.debug('!!!!Before lstOppContactRolePrimary = >'+lstOppContactRolePrimary);
                }
           
        }
    }
    
        opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
        objopportunityTriggerHandler.createNewContact(trigger.new);
   
   
    code added by deepak dhingra on 13/6/2014 to update the line items
     if(trigger.isAfter && trigger.isUpdate)
     {
        Set<string> qualifiedOppId = new Set<string>();
        for(Opportunity o:Trigger.new)
        {
            if(o.Agency_Discount_Percentage__c != Trigger.Oldmap.get(o.id).Agency_Discount_Percentage__c)
            {
                qualifiedOppId.add(String.valueOf(o.id).left(15));
            }
           
        }
       
        if(qualifiedOppId.size() > 0)
        {
            List<OpportunityLineItem> lstLineItems = new List<OpportunityLineItem>();
            lstLineItems  = [select id from OpportunityLineItem where opportunityid in :qualifiedOppId];
            if(lstLineItems.size() > 0)
            {
                update lstLineItems;
            }
        }
       
       
     }
}
Example ----When we fullfill contact then opporetunity then two contact role

Action            Contact  Name        Company Name           Email                                                        Phone                           Role                       Primary
Edit |  Del     Mark Sze                  informgroup.com            mark.sze@informgroup.com                                                                                    Checked
Edit | Del       Mark Sze                 informgroup.com            mark.sze@informgroup.com                                                      Buyer                         Checked

_______________________________________________________________________________________
When we fill oportunity then only one contact role ___
Action            Contact  Name        Company Name              Email                                                        Phone                           Role                        Primary
Edit |  Del     Mark Sze                  informgroup.com            mark.sze@informgroup.com                                                                                         Checked

How i solve it and i want to only one contact role in under opportunity







Trigger-------------
________________________
trigger OpportunityTrigger on Opportunity (After insert)
{
    if(trigger.isAfter && trigger.isInsert)
    { system.debug('After insert');
        opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
        objopportunityTriggerHandler.createNewContact(trigger.new);
    }
}

________________________________
Class is _______________________________
Public class opportunityTriggerHandler
{
   
    public void createNewContact(List<Opportunity> triggerNew)
    {
        List<OpportunityContactRole> lstOppContactRole = new list<OpportunityContactRole>();
        if(triggerNew != null && triggerNew.size() > 0)
        {
            for(Opportunity objOpp : triggerNew)
            {
                if(objOpp.Buyer__c != null)
                {
                    OpportunityContactRole objOppContactRole = new OpportunityContactRole();
                   
                    objOppContactRole.ContactId = objOpp.Buyer__c;
                    objOppContactRole.OpportunityId = objOpp.ID;
                    objOppContactRole.Role = 'Buyer';
                   
                    lstOppContactRole.add(objOppContactRole );
                }
            }
        }
       
        if(lstOppContactRole.size() > 0)
      
       insert lstOppContactRole;
       
    }
}




I have created Detailed abutton and placed on the Task Record. The button should function like when we click the button a New opportunity page will opened and carry the data from Task to the Opportunity and need to display on Opportunity fields.  Below javascript created. But  it giving error like that    'Task.WhoId' is not exist like that. Please anyone correct bthis.




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

    var OpportunityCreate= new sforce.SObject("Opportunity");

OpportunityCreate.Contact_Name__c = '{!Task.WhoId}';


    var result = sforce.connection.create([OpportunityToCreate]);

    if (result[0].success == 'true')
    
   var newURL = '/' + result[0].id + '/e';
   window.top.location = newURL;
}
  • June 26, 2014
  • Like
  • 1