• RAJEEV REDDY 3
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 4
    Replies
public class LeadHandlerClass {
 public static void createContactRoleType (Map<id,lead> newMapLead, Map<id,Lead> oldMapLead){
        
        Set<Id> setOppId = new Set<Id>();
        Lead oldLead; 
        Lead newLead;
        Map<Id,Lead> mapContLead = new Map<Id,Lead>();
        
        for(Id CRId : newMapLead.keySet()){
            oldLead = OldMapLead.get(CRId);
            newLead = newMapLead.get(CRId);
            if(newLead.IsConverted && !oldLead.IsConverted){
                setOppId.add(newLead.ConvertedOpportunityId);
                mapContLead.put(newLead.ConvertedContactId,newLead);
            }
        }
        
        List<OpportunityContactRole> lsOppCtRole = [select id,ContactId, role from OpportunityContactRole where OpportunityId in: setOppId limit 50000];
        Lead tmpLead ;
        
        for(OpportunityContactRole oppCtRole : lsOppCtRole){
            tmpLead = mapContLead.get(OppCtRole.ContactId);
            OppctRole.Role = tmpLead.NIL_Contact_Type__c;
        }


        System.debug('Ls OppCtRole is ' + lsOppCtRole);
        if(!lsOppCtRole.isEmpty())
        {  
            Database.SaveResult[] results = Database.update(lsOppCtRole, false);
            for (Database.SaveResult res : results) {
                if (res.isSuccess()) {
                    System.debug('Successfully updated: ' + res.getId());
                } 
                else {
                    // This condition will be executed for failed records
                    for(Database.Error objErr : res.getErrors()) {
                        System.debug('The following error has occurred.');
                        // Printing error message in Debug log
                        System.debug(objErr.getStatusCode() + ': ' + objErr.getMessage());
                        System.debug('Update failed by the error:' + objErr.getFields());
                    }
                }
            }
        }
        
    }
 
Create a  Opportunity Contact role between the Converted Contact and Opportunity. The role value should be fetched from "Contact Role" field.

Can anyone help me how to write trigger on this.
Is there any way where  I want to display  only closed date and month format in the reports for Opportunity Object using Formula  or any way.
one of my teammate created list view for an object in 'A' Org. Then he told me to create the same in 'B' Org.

How to view his list view and the conditions he used for creating list view.
Edit Cases from Queue only if it is accepted/assigned to an user and  we can avoid other users working on cases which are not assigned to them
public class LeadHandlerClass {
 public static void createContactRoleType (Map<id,lead> newMapLead, Map<id,Lead> oldMapLead){
        
        Set<Id> setOppId = new Set<Id>();
        Lead oldLead; 
        Lead newLead;
        Map<Id,Lead> mapContLead = new Map<Id,Lead>();
        
        for(Id CRId : newMapLead.keySet()){
            oldLead = OldMapLead.get(CRId);
            newLead = newMapLead.get(CRId);
            if(newLead.IsConverted && !oldLead.IsConverted){
                setOppId.add(newLead.ConvertedOpportunityId);
                mapContLead.put(newLead.ConvertedContactId,newLead);
            }
        }
        
        List<OpportunityContactRole> lsOppCtRole = [select id,ContactId, role from OpportunityContactRole where OpportunityId in: setOppId limit 50000];
        Lead tmpLead ;
        
        for(OpportunityContactRole oppCtRole : lsOppCtRole){
            tmpLead = mapContLead.get(OppCtRole.ContactId);
            OppctRole.Role = tmpLead.NIL_Contact_Type__c;
        }


        System.debug('Ls OppCtRole is ' + lsOppCtRole);
        if(!lsOppCtRole.isEmpty())
        {  
            Database.SaveResult[] results = Database.update(lsOppCtRole, false);
            for (Database.SaveResult res : results) {
                if (res.isSuccess()) {
                    System.debug('Successfully updated: ' + res.getId());
                } 
                else {
                    // This condition will be executed for failed records
                    for(Database.Error objErr : res.getErrors()) {
                        System.debug('The following error has occurred.');
                        // Printing error message in Debug log
                        System.debug(objErr.getStatusCode() + ': ' + objErr.getMessage());
                        System.debug('Update failed by the error:' + objErr.getFields());
                    }
                }
            }
        }
        
    }
 
one of my teammate created list view for an object in 'A' Org. Then he told me to create the same in 'B' Org.

How to view his list view and the conditions he used for creating list view.
Hey all,

I have created the following Apex trigger that will create, update and delete Opportunity Contact Roles based on a Contact Lookup field placed on the Opportunity. Everything works great currently. I am just trying to figure out one final scenario. If John Doe already has a Contact Role but is not the Primary Contact Role and I want him to be. I want to be able to change my look up field to him and then have that check him as the primary. Currently it just creates a new one and checks them both as primary. So what I need to do is check the current Contact Roles and if there is already one with the same name as in the Lookup field then just check that one as primary, but if there isn't then create a new one. Below is the code. Please let me know if you need further description.

Thanks,
 
trigger CreateContactRole on Opportunity (after insert, after update) {
    
    List<OpportunityContactRole> newContactRoleList = new List<OpportunityContactRole>();
    List<OpportunityContactRole> oldContactRoleList = new List<OpportunityContactRole>();
    Set<Id> OppId = new Set<Id>();
    sET<Id> ContactId = new Set<Id>();
    
    if(Trigger.isInsert) {
        for(Opportunity opp : Trigger.new) {
          if(opp.Primary_Contact__c != null) {
              //Creating new Contact Role
              newContactRoleList.add(new OpportunityContactRole(ContactId=opp.Primary_Contact__c,OpportunityId=opp.Id,Role='Decision Maker',IsPrimary=true));
          }
      }
    }
    
    if(Trigger.isUpdate) {      
      for(Opportunity opp : Trigger.new) {
          if(opp.Primary_Contact__c != null && Trigger.oldMap.get(opp.Id).Primary_Contact__c == null) {
                //Creating new Contact Role
              newContactRoleList.add(new OpportunityContactRole(ContactId=opp.Primary_Contact__c,OpportunityId=opp.Id,Role='Decision Maker',IsPrimary=true));
          }
            else if(opp.Primary_Contact__c != null && Trigger.oldMap.get(opp.Id).Primary_Contact__c != null) {
                //Create New Contact Role make new CR Primary over the old CR
                Opportunity OldOpp = Trigger.oldMap.get(opp.Id);
                OppId.add(OldOpp.id);
                ContactId.add(OldOpp.Primary_Contact__c);
                newContactRoleList.add(new OpportunityContactRole(ContactId=opp.Primary_Contact__c,OpportunityId=opp.Id,Role='Decision Maker',IsPrimary=true));
            }
          else if(opp.Primary_Contact__c == null && Trigger.oldMap.get(opp.Id).Primary_Contact__c != null) {
                Opportunity OldOpp = Trigger.oldMap.get(opp.Id);
                OppId.add(OldOpp.id);
                ContactId.add(OldOpp.Primary_Contact__c);
                try {
                //Deleting old Contact Roles
                if(oldContactRoleList.size()>0) delete oldContactRoleList;
                }
                catch(Exception e) {
                    System.debug(e);
                    trigger.new[0].addError('An error has occurred. Please contact your system administrator.');
                }
          }
      }
    }
    
    try {
        //inserting new contact roles
        if(newContactRoleList.size()>0)insert newContactRoleList;
        
        //Selecting old Contact Roles
        if(OppId.size()>0) oldContactRoleList = [Select Id from OpportunityContactRole where ContactId in : ContactId and OpportunityId in : OppId];        
        
    }
    catch(Exception e) {
        System.debug(e);
        trigger.new[0].addError('An error has occurred. Please contact your system administrator.');
    }
}