function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
YaduYadu 

Need Help in Trigger, Have a Deaaline...

  • Update the Sales_Target__c field (Lookup to Sales Target) for Sales Opportunities that match on Officer Code and Close Date/Period Start-End (with Count Against Targets checked)
  • SELECT SO.Id, … FROM Sales_Opportunity__c SO,
  •        Sales_Target__c ST

    WHERE      SO.Count_Against_Targets__c = true

    AND  SO.Officer_Code__c = ST.Officer_Code__c

    AND  SO.Close_Date__c  <= ST.Target_Period_End__c

    AND  SO.Close_Date__c      >= ST.Target_Period_Start__c

     

    If there are any SalesTarget that match the above criteria i need to update the SalesOppurtunity. sales target field on sales oppurtunity Object. This should handle bulk records

     

    Have a dead line. Please help me.

     

    Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Try...

 

trigger myTrg On Sales_Opportunity(Before Insert, Before Update){

//Change to the field type for Office_Code__c
Set<FIELDTYPE> sOC = New Set<FieldType>();

for(Sales_Opportunity__c so :trigger.new){
   if(so.Count_Against_Targets__c == True)
      sOC.add(so.Officer_Code__c);
}

List<Sales_Target__c) lST = [Select FIELD From Sales_Target__c Where Officer_Code__c IN :sOC];

for (Sales_Opportunity__c so : trigger.new){
 
   for(Sales_Target__c st : lST){
      if(st.Target_Period_End__c >= so.Close_date__c && st.Target_Period_Start <= so.Close_Date__c)
        
       so.Sales_Target__c = st.id;

   }

}


}

 You may need to do this in a batch process as you could potentially hit script limits depending on how big the sales target list is. I do not see another way to do this unless the sales target is already related to the opportunity. Also, what is there are more than one sales target that meets criteria? What if the sales target is already assigned to another opportunity? some things to think about.

All Answers

Starz26Starz26

Are you doing this on record creation, edit, etc or are you trying to look through all records and update them at specific intervals?

YaduYadu

 

 

 

 

Its on Inserting new records and updating the records. its not on specific intervals.

YaduYadu

Hi,

 

The trigger is After Insert. The logic is whenever a new Sales Oppurtunity record gets created, it should look for the sales Target records  for certain critiria, if there exists any sales Target record then update the Sales Oppurtunity record with the matched sales Target  ID on the Sales_Target__c field on Sales_Oppurtunity__C Object.

 

Cirtiria is

 

Sales Oppurtunity__C.Count_Against_Targets__c = true and

Sales Oppurtunity__C. Officer_code__c = Sales_Target__C. Officer_code__c  and

Sales Oppurtunity__C.Close_Date__c  <=  Sales_Target__C.Target_Period_End__c

AND  Sales Oppurtunity__C.Close_Date__c    >=  Sales_Target__C.Target_Period_Start__c

 

Thanks!

 

 



Starz26Starz26

Try...

 

trigger myTrg On Sales_Opportunity(Before Insert, Before Update){

//Change to the field type for Office_Code__c
Set<FIELDTYPE> sOC = New Set<FieldType>();

for(Sales_Opportunity__c so :trigger.new){
   if(so.Count_Against_Targets__c == True)
      sOC.add(so.Officer_Code__c);
}

List<Sales_Target__c) lST = [Select FIELD From Sales_Target__c Where Officer_Code__c IN :sOC];

for (Sales_Opportunity__c so : trigger.new){
 
   for(Sales_Target__c st : lST){
      if(st.Target_Period_End__c >= so.Close_date__c && st.Target_Period_Start <= so.Close_Date__c)
        
       so.Sales_Target__c = st.id;

   }

}


}

 You may need to do this in a batch process as you could potentially hit script limits depending on how big the sales target list is. I do not see another way to do this unless the sales target is already related to the opportunity. Also, what is there are more than one sales target that meets criteria? What if the sales target is already assigned to another opportunity? some things to think about.

This was selected as the best answer
YaduYadu

Thank you so much :)