• koti91
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 11
    Questions
  • 10
    Replies

trigger NMO_CaseOppurtunity_PrimaryLogic_BIBU on Opportunity__c(before insert,beforeupdate){

     

  list<Opportunity__c> lstCaseOpptyToUpdate = new list<Opportunity__c>();//lstCaseOpptyToUpdate is used to update CO that are not a part of trigger.

  list<lead> lstLeadsToUpdate = new list<lead>();//lstLeadsToUpdate is used to update the lead owner whenever a primary CO changes.

  set<Id> setLeadIds = new set<Id>();

  map<Id,lead> mapLeads = new  map<Id,lead>();

  map<id,list<Opportunity__c>> mapLeadWiseCaseOpty  = new map<id,list<Opportunity__c>>();

  map<id,id> mapLeadAssignOwner = new map<id,id> ();

  list<Opportunity__c> colst1=new list<Opportunity__c>() ;

 

  for(Opportunity__c co:trigger.new){

    setLeadIds.add(co.lead__c);// Gather all lead IDs related to the Case Oppurtunities.

  }

 

  for(Lead l : [select id,ownerId,(select id,OwnerId,Primary_Opportunity__c,Primary_Override__c from Case_Opportunities__r) from Lead where id in :setLeadIds]){

    mapLeadWiseCaseOpty.put(l.id,l.Case_Opportunities__r);// map used to have all the related Case Oppurtunities for the lead IDs

    mapLeads.put(l.id,l);

  }

 

   // When Record is inserted

if(trigger.isInsert){

    for(Opportunity__c co:trigger.new){

            colst1=mapLeadWiseCaseOpty.get(co.lead__C);// colst1 contains all the case oppurtunities related to the lead.

            if(colst1.size()!=0){//check if it has any siblings

                  if(co.Primary_Override__c=='Yes'){// check if current case oppurtunity wants to become the primary CO.

                        for(integer i=0;i<colst1.size();i++){ //make all other CO in the system to false

                              colst1[i].Primary_Opportunity__c=false;

                              colst1[i].Primary_Override__c='No';

                              lstCaseOpptyToUpdate.add(colst1[i]);     

                    }

                  co.Primary_Opportunity__c=true;

                  co.Primary_Override__c='No';

                  lead l = mapLeads.get(co.lead__c);

                  l.ownerId = co.ownerId;

                  lstLeadsToUpdate.add(l);

              }

                  else{

                        co.Primary_Opportunity__c=false;

                }

          }

          else{//No Siblings

                  co.Primary_Opportunity__c=true;

                  co.Primary_Override__c='No';

                  lead l = mapLeads.get(co.lead__c);

            l.ownerId = co.ownerId;

            lstLeadsToUpdate.add(l);

          }

      }    

}

 

  // when record is updated

if(trigger.isUpdate) {

      set<id> cOids=new set<id>();

      for(Opportunity__c coSet:trigger.new){

            cOids.add(coSet.id);

      }

      for(Opportunity__c co:trigger.new){

      colst1=mapLeadWiseCaseOpty.get(co.lead__C);// colst1 contains all the case oppurtunities related to the lead.

      if(colst1.size()!=0){//it has siblings

                  if(co.Primary_Override__c=='Yes'){// check if current case oppurtunity wants to become the primary CO.

                  for(integer i=0;i<colst1.size();i++){ //make all other CO in the system to false

                        if((colst1[i].id!=co.id) && (!cOids.contains(colst1[i].id))){

                                    colst1[i].Primary_Opportunity__c=false;

                                    colst1[i].Primary_Override__c='No';

                                    lstCaseOpptyToUpdate.add(colst1[i]);     

                              }

              }

                        co.Primary_Opportunity__c=true;

                        co.Primary_Override__c='No';

                        lead l = mapLeads.get(co.lead__c);

                  l.ownerId = co.ownerId;

                  lstLeadsToUpdate.add(l);

                  }

                  else{

                        if(co.Primary_Opportunity__c!=true){

                              co.Primary_Opportunity__c=false;

                        }

                        else{

                              lead l = mapLeads.get(co.lead__c);

                        l.ownerId = co.ownerId;

                        lstLeadsToUpdate.add(l);

                        }

                  }

      }

      else{//No Sibings

            co.Primary_Opportunity__c=true;

                  co.Primary_Override__c='No';

                  lead l = mapLeads.get(co.lead__c);

            l.ownerId = co.ownerId;

            lstLeadsToUpdate.add(l);

      }

    }

}

//Final DML

 

 if(lstCaseOpptyToUpdate.size()>0){

     try{

           update lstCaseOpptyToUpdate;

     }

      catch(System.DMLException e){

            set<id> exleadIds=new set<id>();

      for(Opportunity__c coError:lstCaseOpptyToUpdate){

          exleadIds.add(coError.lead__c);

      }

      for(Opportunity__c coError1:trigger.new){

            if(exleadIds.contains(coError1.lead__c)){

                  coError1.addError('The System encountered a problem when attempting to update related Case Oppurtunities:'+e.getMessage());

            }

      }

      }    

}

if(lstLeadsToUpdate.size()>0){

      try{

            update lstLeadsToUpdate;

      }

      catch(System.DMLException e){

            set<id> exleadIds=new set<id>();

      for(Lead luerror:lstLeadsToUpdate){

            exleadIds.add(luerror.id);

      }

      for(Opportunity__c coError:trigger.new){

            if(exleadIds.contains(coError.lead__c)){

                  coError.addError('The System encountered a problem when attempting to update lead owner:'+e.getMessage());

            }

      }

      }

}

}

 

 

This is my code and i had written a test class for this which is covering only 76% of code and my org needs above 85%. The problem is i cannot cover the try catch methods in the code. could any one help me on how to write test class to throw an exception and enter the code in to exception....its urgent

I had two objects : Obj A and Obj B

 

ObjB has a lookup relationship to objA.

 

objA-> fields->a1,a2,a3

objB->fields->b1,b2,b3

 

i need to create a trigger on objB (before insert and before update)

if trigger.isinsert  then copy three fields from objA to obj B(i.e copy filed values a1,a2,a3 to b1,b2,b3)

 

if trigger .is update then check if the three fields  are equal on obj A . if equal do nothing else update values from Obj A.

 

I need to write this in bulkify way. plzzz help.

 

 

 

 

Hi 

 

I am new to Apex Coding and i had a scenario like the below which i dont know how to start. can any one plz help.

 

I need to write a trigger on Object A(with 4 record types in it say 1,2,3,4)

After update trigger fires only  when

 

1.  A.checkbox is checked

2.  A.Picklist1='test'

3.  A.Picklist2=somevalue

4.  A.previous value of picklist1  != 'test' 

 

if all the conditions above are met then trigger needs to create records in object B with a Recordtype of R with the following conditions.

 

1. if ObjectA. record type =1 

then need to copy some values from A and put it in B

2. if if ObjectA. record type =2

then need to copy some values from A and put it in B

3. if ObjectA. record type =3

then need to copy some values from A and put it in B

4. if ObjectA. record type =4

then need to copy some values from A and put it in B. 

 

I need this logic to be in a BUlkify way.....can any one help......


 

 

 

 

hello

 

I am new to SFDC Development and i need some help with the trigger.

 

I created two objects A and B. there is no relation between these objects and i had event id and event id fields on both objects. 

On Object A i also had a status pick list field which has pass and Fail values.

 

on object A records are loaded by some ETL Process and after loading that records i am firing a trigger on A  which will process the reocrds for  the status value pass and then

1. checks to see if event exists by checking event ID  on A against event ID on object B.

 

how to compare this ......plz help

 

I am getting the following error "bad value for restricted picklist field: Out of Office"
when trying to add an Event. I have previously called describeSObject on SaveAs and it brings back these three stings "Busy","Free" and "Out of Office". "Busy" and "Free" work fine but "Out of Office" throws the error.

This just looks like a bug to me. Anyone able to get this to work?

-Bob