• Aravind Sriram
  • NEWBIE
  • 25 Points
  • Member since 2012

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 16
    Replies

Hi,

 

I have the trigger for Before Insert and After Insert with the Code written below. It perfectly works good. But I have to write the Same logic of Before Insert and After Insert Trigger on Before Update.

I am new to Apex. Thanks for the Help.

 

Code 

 

public with sharing class SalesTargetHandler {

// these class variables are used by the OnBeforeInsert and OnAfterInsert methods
Map<String, Sales_Target__c> offCodeToSalesTarget = new Map<String, Sales_Target__c>(); //map of officer codes to a new copy of a Sales Target object
Date salesTargetMaxEndDate;
Date salesTargetMinBegDate;

// these class variables are used by the OnBeforeInsert methods
set<Id> newRecordIds = new set<Id>();

// -------------------------------------------------------------------------------------------------------------------------------
// Constructor
public SalesTargetHandler(){
}

// -------------------------------------------------------------------------------------------------------------------------------
// Methods
public void OnBeforeInsert(Sales_Target__c[] newRecords){
// note the way the trigger works is that every invocation of every "trigger mode" instantiatiates a new handler.
// so each method, is in effect, on a new instance of this class.
gatherQueryInfo(newRecords, false);

for(Sales_Target__c existST :[Select Id ,Officer_Code__c, Officer_Code__r.Officer_Code__c, Target_Period_Start__c ,Target_Period_End__c
from Sales_Target__c
where Officer_Code__r.Officer_Code__c IN :offCodeToSalesTarget.keySet()
and Target_Period_End__c >= :salesTargetMinBegDate
and Target_Period_Start__c <= :salesTargetMaxEndDate])
{
Sales_Target__c newST = offCodeToSalesTarget.get(existST.Officer_Code__r.Officer_Code__c);

if(newST != null && existST.Officer_Code__c == newST.Officer_Code__c &&
((existST.Target_Period_Start__c <= newST.Target_Period_Start__c &&
existST.Target_Period_End__c >= newST.Target_Period_Start__c)
|| (existST.Target_Period_Start__c<= newST.Target_Period_End__c &&
existST.Target_Period_End__c>= newST.Target_Period_End__c)
)){

String errorMsg = System.Label.Sales_Target_Error_Message;
newST.addError(errorMsg);
}
}
}

public void OnAfterInsert(Sales_Target__c[] newRecords){
// note the way the trigger works is that every invocation of every "trigger mode" instantiatiates a new handler.
// so each method, is in effect, on a new instance of this class.
gatherQueryInfo(newRecords, true);

Sales_Opportunity__c[] salesOpp = new Sales_Opportunity__c[]{};
for(Sales_Opportunity__c opp : [SELECT id ,Officer_Code__c, Officer_Code__r.Officer_Code__c, Sales_Target__c,Count_Against_Targets__c,Close_Date__c
FROM Sales_Opportunity__c
WHERE Count_Against_Targets__c = true
AND Sales_Target__c = null
AND Officer_Code__r.Officer_Code__c IN :offCodeToSalesTarget.keySet()
AND Close_Date__c <= :salesTargetMaxEndDate
AND Close_Date__c >= :salesTargetMinBegDate])
{
Sales_Target__c newST = offCodeToSalesTarget.get(opp.Officer_Code__r.Officer_Code__c);

if(newST != null && opp.Close_Date__c <= newST.Target_Period_End__c && opp.Close_Date__c >= newST.Target_Period_Start__c){

opp.Sales_Target__c = newST.id;
salesOpp.add(opp);
newST.Actual_Number_of_Sales__c++;
}
}
if (salesOpp.isEmpty()==false)
update salesOpp;

Task[] tasksToUpdate = new Task[]{};
for(Task t : [SELECT Id, ActivityDate, Officer_Code__c
FROM Task
WHERE Count_Against_Targets__c = true
AND Officer_Code__c IN :offCodeToSalesTarget.keySet()
AND ActivityDate <= :salesTargetMaxEndDate
AND ActivityDate >= :salesTargetMinBegDate]){

Sales_Target__c newST = offCodeToSalesTarget.get(t.Officer_Code__c);

if(newST != null && t.ActivityDate <= newST.Target_Period_End__c && t.ActivityDate >= newST.Target_Period_Start__c){

t.Sales_Target_Id__c = newST.Id;
tasksToUpdate.add(t);
newST.Actual_Number_of_Calls__c++;
}
}

if(tasksToUpdate.isEmpty()==false)
update tasksToUpdate;


if(offCodeToSalesTarget.values().isEmpty()==false)
update offCodeToSalesTarget.values();
}

private void gatherQueryInfo(Sales_Target__c[] stRecords, Boolean createClones){
salesTargetMaxEndDate = system.today();
salesTargetMinBegDate = system.today();

if (stRecords.size()>0){
salesTargetMaxEndDate = stRecords.get(0).Target_Period_End__c;
salesTargetMinBegDate = stRecords.get(0).Target_Period_Start__c;
}

set<ID> officerCodeSfidSet = new set<ID>();
// first pass. get the data I need
for(Sales_Target__c st : stRecords){
officerCodeSfidSet.add(st.Officer_Code__c);
if(st.Target_Period_End__c > salesTargetMaxEndDate) salesTargetMaxEndDate = st.Target_Period_End__c;
if(st.Target_Period_Start__c < salesTargetMinBegDate) salesTargetMinBegDate = st.Target_Period_Start__c;
}

// retrieve the actual officer code values from the Officer_Code__c table
map<ID, Officer_Code__c> offCodeMap = new map<ID, Officer_Code__c>([select id, Officer_Code__c from Officer_Code__c where ID in :officerCodeSfidSet]);

for(Sales_Target__c st : stRecords){
Sales_Target__c mapST = st;
if (createClones){
mapST = st.Clone(true, true, true, true);
// we are going to depend on the object being configured to default these two fields to 0.
// The trigger code cannot handle null values for these fields
//if (mapST.Actual_Number_of_Calls__c==null) mapST.Actual_Number_of_Calls__c = 0;
//if (mapST.Actual_Number_of_Sales__c==null) mapST.Actual_Number_of_Sales__c = 0;
}
Officer_Code__c offCodeRec = offCodeMap.get(mapST.Officer_Code__c);
if (offCodeRec!=null) offCodeToSalesTarget.put(offCodeRec.Officer_Code__c, mapST);
}
}

}

  • January 12, 2012
  • Like
  • 0
Hi All,

While exploring our Customer community, I had the following observations and I am not sure whether every org faces these or we are the only ones or if we are doing something wrongly here.

Background -  We have a Customer community and we use the Overage High Volume Customer Portal license for providing access to Customers.

The observations - 

1. External OWD seems to affect the High-Volume users while its a general belief that none of the sharing settings apply to HV users as they neither have a Role nor can be part of Public Groups.

2. Our Community has several VF pages exposed on it and the entire look and feel is customized (no standard stylesheets). We have just exposed the Home tab on Community and no other tabs. All standard tabs like Account, Contact, Case etc. are hidden on the profile associated with these HV users. Still if an authenticated community user types in the standard Salesforce URL patterns, they are able to view these tabs along with the data (as per the external OWD, if their profile provides them at least Read permission on the object). For e.g. typing "/500/o" would take him to the Cases tab, "/001/o" would take him to the Accounts tab and so on. They can also type in the Id of any record and reach the detail page (if they don't have access, insufficient privileges page). All these standard URLs display the pages with standard community styling.

The issue is why are the users able to access tabs by typing in the URLs when their profile has those tabs as hidden ?

It seems to be a very serious issue and I expect that I am not the only one who's facing it (please do not assume you don't have it unless you have explicitly checked/tried to reproduce). What is the use of having a custom styling when such behavior destroys the uniform experience that a Customer is supposed to have and there are chances of exposing data that they are not supposed to see (they should see what has been exposed via VF pages on Community).

If you faced the same issue, how did you work-around this ? or if you are facing the same issue, how do you plan to mitigate it ?

Hi,

       Very few users are getting this Authorization Required error on Sites Page.Any Idea?

 

Thanks

Hi,

 

I have the trigger for Before Insert and After Insert with the Code written below. It perfectly works good. But I have to write the Same logic of Before Insert and After Insert Trigger on Before Update.

I am new to Apex. Thanks for the Help.

 

Code 

 

public with sharing class SalesTargetHandler {

// these class variables are used by the OnBeforeInsert and OnAfterInsert methods
Map<String, Sales_Target__c> offCodeToSalesTarget = new Map<String, Sales_Target__c>(); //map of officer codes to a new copy of a Sales Target object
Date salesTargetMaxEndDate;
Date salesTargetMinBegDate;

// these class variables are used by the OnBeforeInsert methods
set<Id> newRecordIds = new set<Id>();

// -------------------------------------------------------------------------------------------------------------------------------
// Constructor
public SalesTargetHandler(){
}

// -------------------------------------------------------------------------------------------------------------------------------
// Methods
public void OnBeforeInsert(Sales_Target__c[] newRecords){
// note the way the trigger works is that every invocation of every "trigger mode" instantiatiates a new handler.
// so each method, is in effect, on a new instance of this class.
gatherQueryInfo(newRecords, false);

for(Sales_Target__c existST :[Select Id ,Officer_Code__c, Officer_Code__r.Officer_Code__c, Target_Period_Start__c ,Target_Period_End__c
from Sales_Target__c
where Officer_Code__r.Officer_Code__c IN :offCodeToSalesTarget.keySet()
and Target_Period_End__c >= :salesTargetMinBegDate
and Target_Period_Start__c <= :salesTargetMaxEndDate])
{
Sales_Target__c newST = offCodeToSalesTarget.get(existST.Officer_Code__r.Officer_Code__c);

if(newST != null && existST.Officer_Code__c == newST.Officer_Code__c &&
((existST.Target_Period_Start__c <= newST.Target_Period_Start__c &&
existST.Target_Period_End__c >= newST.Target_Period_Start__c)
|| (existST.Target_Period_Start__c<= newST.Target_Period_End__c &&
existST.Target_Period_End__c>= newST.Target_Period_End__c)
)){

String errorMsg = System.Label.Sales_Target_Error_Message;
newST.addError(errorMsg);
}
}
}

public void OnAfterInsert(Sales_Target__c[] newRecords){
// note the way the trigger works is that every invocation of every "trigger mode" instantiatiates a new handler.
// so each method, is in effect, on a new instance of this class.
gatherQueryInfo(newRecords, true);

Sales_Opportunity__c[] salesOpp = new Sales_Opportunity__c[]{};
for(Sales_Opportunity__c opp : [SELECT id ,Officer_Code__c, Officer_Code__r.Officer_Code__c, Sales_Target__c,Count_Against_Targets__c,Close_Date__c
FROM Sales_Opportunity__c
WHERE Count_Against_Targets__c = true
AND Sales_Target__c = null
AND Officer_Code__r.Officer_Code__c IN :offCodeToSalesTarget.keySet()
AND Close_Date__c <= :salesTargetMaxEndDate
AND Close_Date__c >= :salesTargetMinBegDate])
{
Sales_Target__c newST = offCodeToSalesTarget.get(opp.Officer_Code__r.Officer_Code__c);

if(newST != null && opp.Close_Date__c <= newST.Target_Period_End__c && opp.Close_Date__c >= newST.Target_Period_Start__c){

opp.Sales_Target__c = newST.id;
salesOpp.add(opp);
newST.Actual_Number_of_Sales__c++;
}
}
if (salesOpp.isEmpty()==false)
update salesOpp;

Task[] tasksToUpdate = new Task[]{};
for(Task t : [SELECT Id, ActivityDate, Officer_Code__c
FROM Task
WHERE Count_Against_Targets__c = true
AND Officer_Code__c IN :offCodeToSalesTarget.keySet()
AND ActivityDate <= :salesTargetMaxEndDate
AND ActivityDate >= :salesTargetMinBegDate]){

Sales_Target__c newST = offCodeToSalesTarget.get(t.Officer_Code__c);

if(newST != null && t.ActivityDate <= newST.Target_Period_End__c && t.ActivityDate >= newST.Target_Period_Start__c){

t.Sales_Target_Id__c = newST.Id;
tasksToUpdate.add(t);
newST.Actual_Number_of_Calls__c++;
}
}

if(tasksToUpdate.isEmpty()==false)
update tasksToUpdate;


if(offCodeToSalesTarget.values().isEmpty()==false)
update offCodeToSalesTarget.values();
}

private void gatherQueryInfo(Sales_Target__c[] stRecords, Boolean createClones){
salesTargetMaxEndDate = system.today();
salesTargetMinBegDate = system.today();

if (stRecords.size()>0){
salesTargetMaxEndDate = stRecords.get(0).Target_Period_End__c;
salesTargetMinBegDate = stRecords.get(0).Target_Period_Start__c;
}

set<ID> officerCodeSfidSet = new set<ID>();
// first pass. get the data I need
for(Sales_Target__c st : stRecords){
officerCodeSfidSet.add(st.Officer_Code__c);
if(st.Target_Period_End__c > salesTargetMaxEndDate) salesTargetMaxEndDate = st.Target_Period_End__c;
if(st.Target_Period_Start__c < salesTargetMinBegDate) salesTargetMinBegDate = st.Target_Period_Start__c;
}

// retrieve the actual officer code values from the Officer_Code__c table
map<ID, Officer_Code__c> offCodeMap = new map<ID, Officer_Code__c>([select id, Officer_Code__c from Officer_Code__c where ID in :officerCodeSfidSet]);

for(Sales_Target__c st : stRecords){
Sales_Target__c mapST = st;
if (createClones){
mapST = st.Clone(true, true, true, true);
// we are going to depend on the object being configured to default these two fields to 0.
// The trigger code cannot handle null values for these fields
//if (mapST.Actual_Number_of_Calls__c==null) mapST.Actual_Number_of_Calls__c = 0;
//if (mapST.Actual_Number_of_Sales__c==null) mapST.Actual_Number_of_Sales__c = 0;
}
Officer_Code__c offCodeRec = offCodeMap.get(mapST.Officer_Code__c);
if (offCodeRec!=null) offCodeToSalesTarget.put(offCodeRec.Officer_Code__c, mapST);
}
}

}

  • January 12, 2012
  • Like
  • 0
Hi, I have a WSDL generated Apex class which i am using to perform apex callouts. I did not realize it requires a test class When i am trying to deploy my code to production my code percentage was screwed due to this WSDL Apex class which is huge. It has code which is out of my understanding as it is automatically generated by salesfroce. How would i able to write the test class, is there a way to by pass this. Any suggestions would be greatly appreciative. Thank you,

public class UpdateField {
    public static void upd(List<Customer__c> c)
    {
        for(Customer__c p:c)
        {
            if(p.MRRMonthly__c!=CustomerInfo__c.MRR__c)
            {
                p.MRRMonthly__c=CustomerInfo__c.MRR__c;
            }
        }
    }
}

 

 

I am getting an error:   Error: Compile Error: Illegal assignment from Schema.SObjectField to Decimal at line 8 column 17

trigger HandleProductionPriceChange on<a href="http://www.buywatchesandjewelry.com/"> rolex </a> Marchandise__c (after update) {
List<Line_Item__c> openLineItems=[SELECT j.Unit_Price__c,j.Merchandise__r.Price__c FROM Line_Item__c j
 WHERE j.Invoice_Statement__r.Status__c='Negotiating' AND j.Merchandise__r.id IN :Trigger.new FOR UPDATE];

for(Line_Item__c Ii:openLineItems){
if(Ii.Merchandise__r.Price__c < Ii.Unit_Price__c){
    Ii.Unit_Price__c=Ii.Merchandise__r.Price__c;
    }
 }
 update openLineItems;
}

 

 

 

Error: Compile Error: Didn't understand relationship 'Merchandise__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 2 column 34

 

 

 

Unable to figure out the problem.

I had written this trigger on force IDE and while saving it gave an error :File only saved locally,not to server

Hello,

 

Can anybody tell me that 

 

How i can use list variable value from controller in javascript without using apex:repeat in visualforce page.

 

 

I have one list<String> in controller with 1000 records.

i want that recoerds in javascript so i can't use apex:repeat because of (page view size limit)

 

Reply fast...

Hi ,

      I got a error , Account type is not visible . I checked sharing settings it is fine  , I am working as sys admin , My apex code is correct , I dont know where i gone wrong , Can any one help me ?..

 

What security settings i have to check . My apex code is fine , I have a problem in security settings can any one help me about security settings for account , 

 

Error : 'account type is not visible '

 

 

Thanks ,

 

Jana

Just want to make sure I have this correct........

 

If I have a custom object

 

OWD set to Public Read

Profile Object Access Set to ALL UNCHECKED

 

 

1. The user WILL NOT be able to access the object to view records, VF pages will NOT display the records, API access will NOT return results for the user to view correct?

 

2. Classes WILL be able to look at the records to get value from it and use elsewhere or store in a value to display?

 

Just trying to see what effect it would have to set the OWD to Public Read and profile to basically NO access