0

I have an update trigger on account object. There are two fields in account: Revenue_Split_Owner_1__c AND Revenue_Split_Percentage_1__c. When these two fields get updated and get different value from previous ones, the trigger works.

The trigger will generate some records of Sunk_User_Revenue__c objects. The records'values are retrieved from existing User_Revenue__c records. However, nothing happens with my trigger.

Here is the code:

trigger trgr_createSunkUserRevenueRecord on Account (after update) {

String accName;
List<id>accRevId = new List<id>();
List<Sunk_User_Revenue__c>sunkUserRevenues = new List<Sunk_User_Revenue__c>();

for (account a:trigger.new)
{
    if ((a.Revenue_Split_Owner_1__c != trigger.oldmap.get(a.ID).Revenue_Split_Owner_1__c) || 
        (a.Revenue_Split_Percentage_1__c != trigger.oldmap.get(a.ID).Revenue_Split_Percentage_1__c)) 
    {
        accName = a.name;
        List<Revenue__c>accRev = [select id from Revenue__c where Account__c =: a.name];
        for (integer i = 0; 0 < accrev.size(); i++)
        {
            accRevId.add(accRev[i].id);
        }
        for (user_revenue__c ur:[select revenue__c, Date_Paid__c, Revenue_Amount__c, Revenue_Earned_By_User__c, Revenue_Split_Percentage__c, user__c from user_revenue__c where Revenue__c in: accRevId]) 
        {
            sunkUserRevenues.add(createRecord(ur, ur.user__c));
        }

        insert (sunkUserRevenues);
    }
}


Sunk_User_Revenue__c createRecord (user_revenue__c ur, ID user)
{
    Sunk_User_Revenue__c s = new Sunk_User_Revenue__c (Revenue__c = ur.Revenue__c, Date_Paid__c = ur.Date_Paid__c, Revenue_Ex_GST_Amount__c = ur.Revenue_Amount__c, Revenue_Ex_GST_Earned_By_User__c = ur.Revenue_Earned_By_User__c, Revenue_Ex_GST_Split_Percentage__c = ur.Revenue_Split_Percentage__c, User__c = user);
    if(ur.Account__c!=null)
        s.Account__c = ur.Account__c;
    else if(ur.Contract__c!=null)
        s.Contract__c = ur.Contract__c;

    return s;
}

}
2
  • 1
    I'm confused. You're saying it doesn't create records, but then you say 'The trigger will generate some records of Sunk_User_Revenue__c objects'. Can try explaining what you expect to happen, and what is happening? BTW, you have two SOQL queries in a for loop, which is non-bulkified code.
    – Nick Cook
    Sep 17 '15 at 6:41
  • Sorry, I didn't explain it clearly enough. I want the trigger to generate some records of Sunk_User_Revenue__c, this is what I expect to happen. But what is happening is that this trigger doesn't work, which means it doesn't generate any record of Sunk_User_Revenue__c. BTW, I will move the second loop out of the first loop later, but now I just want the trigger works first.
    – Josh
    Sep 22 '15 at 23:38
0

Assuming Account__c in Revenue__c is a lookup to Account object, then your SOQL select should be using the account id, rather than its name. i.e.:

accRev = [select id from Revenue__c where Account__c =: a.id];

Also, you would not want to have insert within the loop.

2
  • Insert was outside of the loop originally, but when I was formatting this post, I put the bracket in the wrong place accidentally. a.id resolved the problem, thank you. That's all I want.
    – Josh
    Sep 23 '15 at 4:37
  • okay. glad to help.
    – pnoytechie
    Sep 23 '15 at 4:48
0

If you mean to say that the trigger only works when a new Revenue_Split_Owner_1__c or Revenue_Split_Percentage_1__c value is different from the old value, that's because of these lines:

if ((a.Revenue_Split_Owner_1__c != trigger.oldmap.get(a.ID).Revenue_Split_Owner_1__c) || 
        (a.Revenue_Split_Percentage_1__c != trigger.oldmap.get(a.ID).Revenue_Split_Percentage_1__c))

where the current value is compared with the old value (using Oldmap). If you remove that if, the trigger will always fire, if that is what you want.

1
  • Even if I remove the IF condition, still nothing happens, no record of Sunk_User_Revenue__c is generated. Besides, I want to keep this IF condition so the trigger fires only in this case.
    – Josh
    Sep 22 '15 at 23:39
0

Now I changed my code like this: I moved the inner loop out and removed the IF condition in order to simplify the code and make it work (I will put back the IF condition later). The following code still doesn't work, no Sunk_User_Revenue record is generated. Also, I checked the debug log, 0 row is selected. Don't know what's wrong.

    trigger trgr_createSunkUserRevenueRecord on Account (after update) {

    List<id>accRevId = new List<id>();

    List<Sunk_User_Revenue__c>sunkUserRevenues = new List<Sunk_User_Revenue__c>();
    List<Revenue__c>accRev = new List<Revenue__c>();

    for (account a:trigger.new)
    {

            accRev = [select id from Revenue__c where Account__c =: a.name];
            createSURList();
            insert (sunkUserRevenues);

    }

    List<Sunk_User_Revenue__c> createSURList()
    {
        for (integer i = 0; i < accRev.size(); i++)
        {   
            accRevId.add(accRev[i].id);
        }

        for (user_revenue__c ur:[select revenue__c, Date_Paid__c, Revenue_Amount__c, Revenue_Earned_By_User__c, Revenue_Split_Percentage__c, user__c from user_revenue__c where Revenue__c in: accRevId]) 
        {
            sunkUserRevenues.add(createRecord(ur, ur.user__c));
        }
        return sunkUserRevenues;
    }

    Sunk_User_Revenue__c createRecord (user_revenue__c ur, ID user)
    {
        Sunk_User_Revenue__c s = new Sunk_User_Revenue__c (Revenue__c = ur.Revenue__c, Date_Paid__c = ur.Date_Paid__c, 
        Revenue_Ex_GST_Amount__c = ur.Revenue_Amount__c, Revenue_Ex_GST_Earned_By_User__c = ur.Revenue_Earned_By_User__c, Revenue_Ex_GST_Split_Percentage__c = ur.Revenue_Split_Percentage__c, User__c = user);
        if(ur.Account__c!=null)
            s.Account__c = ur.Account__c;
        else if(ur.Contract__c!=null)
            s.Contract__c = ur.Contract__c;

        return s;
    }

}
0

Looking at your code and your use case statement.

You need to make it AND instead of OR in the initial trigger.

{
if ((a.Revenue_Split_Owner_1__c != trigger.oldmap.get(a.ID).Revenue_Split_Owner_1__c) &&
    (a.Revenue_Split_Percentage_1__c != trigger.oldmap.get(a.ID).Revenue_Split_Percentage_1__c)) 
{

This means, both the fields values should change and should not match with old values. Additionally provide the debug log Incase of any more issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.