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
Justin DeckJustin Deck 

Modify the NewURLPolicyCondition Apex Class/Use Apex in Transaction Security Policies

I'm stuck on the Modify the NewURLPolicyCondition Apex Class challenge: Modify the NewURLPolicyCondition Apex class you created in an earlier challenge and change its condition from any time in the past two days to the past week.

I completed the previous challenges in this section with ease but don't see any reference to "in the past two days" so I am not sure how to modify the condition "to the past week". 

Please help!
 
Best Answer chosen by Justin Deck
Naveen DhanarajNaveen Dhanaraj
Hi Justin,
TRY THIS
 
ACCOUNT TRIGGER HANDLER:

public class AccountTriggerHandler 
{
    public static void CreateAccounts(List<Account> acclist)
    {
        for(Account a:acclist)
        {
            if(a.ShippingState!=a.BillingState)
            {
                a.ShippingState = a.BillingState; // you need to update ShippingState
            }
        }
    }
}
 
TEST CLASS:

@isTest
public class AccountTriggerTest {
    
    @isTest static void TestCreate200Records()
    {
        List<Account> accts = new List<Account>();
        for(Integer i=0; i < 200; i++) 
        {
            Account acct = new Account(Name='Test Account ' + i, BillingState = 'CA');
            accts.add(acct);
        }
        
        Test.startTest();
            insert accts ;
        Test.stopTest();
        // Please query all record once again to check assert
        List<Account> lstAccount = [select ShippingState from Account];
        for (Account a: lstAccount )
        {
            System.assertEquals('CA', a.ShippingState, 'ERROR');
        }
        
    }
}
 
TRIGGER:

trigger AccountTrigger on Account (before insert) 
{
    if (Trigger.isBefore && Trigger.isInsert) {
			AccountTriggerHandler.CreateAccounts(Trigger.new);
		}	
}

 

All Answers

Andreea Capata 16Andreea Capata 16
If you open Apex Class and select the New URL you created,click Alex Class  EDIT and change " LAST_N_DAYS " from 2 days to 7 days.
This is the code you should see in the Apex Class name NewURLPolicyCondition



 global class NewURLPolicyCondition implements TxnSecurity.PolicyCondition {

 public boolean evaluate(TxnSecurity.Event e) {
AggregateResult[] results = [SELECT ApiType FROM LoginHistory WHERE UserId = :e.userId AND LoginTime = LAST_N_DAYS:2 GROUP BY ApiType];

  if(!results.isEmpty() && results.size() > 1) {
   return true;
 }

 return false; 
 
Andreea Capata 16Andreea Capata 16
Justin,

i was stuck on the same exercise,just followed the steps and it works just fine.
Let me know if it worked for you as well.
Justin DeckJustin Deck
Hi, thanks for the swift response. I tried your code and got the following error: [image: Error]Error: Compile Error: expecting right curly bracket, found '' at line 11 column 0. I have attached a screen shot of what I am seeing. Best, Justin
Justin DeckJustin Deck
This is the code I've tried that returned the above error message. Please help!

 global class NewURLPolicyCondition implements TxnSecurity.PolicyCondition {

 public boolean evaluate(TxnSecurity.Event e) {
AggregateResult[] results = [SELECT ApiType FROM LoginHistory WHERE UserId = :e.userId AND LoginTime = LAST_N_DAYS:2 GROUP BY ApiType];

  if(!results.isEmpty() && results.size() > 1) {
   return true;
 }

 return false; 
Andreea Capata 16Andreea Capata 16
Once u open your Apex class and click Edit,you will see a code similar to mine.Change the number of days from 2 to 7 ,în the LAST -N-DAYS. The only change you will make is this mentioned above.As long as the Apex Class you created before is correct,it should work like magic. Sent via the Samsung Galaxy S® 6 edge, an AT&T 4G LTE smartphone-------- Original message --------
Andreea Capata 16Andreea Capata 16
That error is just telling you that the code (java I believe) is missing a curly bracket.
Justin DeckJustin Deck
Thanks, I figured out my mistake a few trailheads ago! Thanks so much for your swift help! I'm sure I'll be reaching out again with questions as I complete the Developer Trailheads. Thanks, Justin
Andreea Capata 16Andreea Capata 16
That reference is Last_N_Days:2.Do you see that in the code? You would change that number 2. Sent via the Samsung Galaxy S® 6 edge, an AT&T 4G LTE smartphone-------- Original message --------
Naveen DhanarajNaveen Dhanaraj
Hi Justin Deck,
Copy and paste this code, This will help you :)
 
global class NewURLPolicyCondition implements TxnSecurity.PolicyCondition {

 public boolean evaluate(TxnSecurity.Event e) {
AggregateResult[] results = [SELECT ApiType FROM LoginHistory WHERE UserId = :e.userId AND LoginTime = LAST_N_DAYS:7 GROUP BY ApiType];

  if(!results.isEmpty() && results.size() > 1) {
   return true;
   }
 return false; 
}
 }

 
Justin DeckJustin Deck
I do have another issue, perhaps you could help me with...Understanding execution content - Write an Apex trigger that modifies Account fields before inserting records.. I am receiving the following error: The 'AccountTrigger' Trigger does not appear to be working correctly. Inserted records did not have matching BillingState and ShippingState values My account trigger code is: trigger AccountTrigger on Account (before insert) { if (Trigger.isBefore && Trigger.isInsert) { AccountTriggerHandler.CreateAccounts(Trigger.new); } } My Account Trigger Test code is: @isTest public class AccountTriggerTest { @isTest static void TestCreate200Records(){ // Test Setup data // Create 200 new Accounts List accts = new List(); for(Integer i=0; i < 200; i++) { Account acct = new Account(Name='Test Account ' + i, BillingState = 'CA'); accts.add(acct); Test.startTest(); insert acct; Test.stopTest(); for (Account a:accts){ System.assertEquals('CA', a.ShippingState, 'ERROR'); } } } } My Account Trigger Handler code is: public class AccountTriggerHandler { public static void CreateAccounts(List acclist) { for(Account account : acclist) { if(account.ShippingState!=account.BillingState) { account.ShippingState = account.BillingState = 'CA'; } } } } Please help. I'm really stuck on this one! My Account Trigger seems to be the same as the example above - not sure what I'm missing here. I definitely executed the Run Alll Tests before trying to complete the challenge. Thanks, Justin
Justin DeckJustin Deck
Thanks, Naveen! I was actually able to figure that one out on my own in the mean time. However, I'm having a new issue I was hoping you could help me with. I do have another issue, perhaps you could help me with...Understanding execution content - Write an Apex trigger that modifies Account fields before inserting records.. I am receiving the following error: The 'AccountTrigger' Trigger does not appear to be working correctly. Inserted records did not have matching BillingState and ShippingState values My account trigger code is: trigger AccountTrigger on Account (before insert) { if (Trigger.isBefore && Trigger.isInsert) { AccountTriggerHandler.CreateAccounts(Trigger.new); } } My Account Trigger Test code is: @isTest public class AccountTriggerTest { @isTest static void TestCreate200Records(){ // Test Setup data // Create 200 new Accounts List accts = new List(); for(Integer i=0; i < 200; i++) { Account acct = new Account(Name='Test Account ' + i, BillingState = 'CA'); accts.add(acct); Test.startTest(); insert acct; Test.stopTest(); for (Account a:accts){ System.assertEquals('CA', a.ShippingState, 'ERROR'); } } } } My Account Trigger Handler code is: public class AccountTriggerHandler { public static void CreateAccounts(List acclist) { for(Account account : acclist) { if(account.ShippingState!=account.BillingState) { account.ShippingState = account.BillingState = 'CA'; } } } } Please help. I'm really stuck on this one! My Account Trigger seems to be the same as the example above - not sure what I'm missing here. I definitely executed the Run Alll Tests before trying to complete the challenge. Thanks, Justin
Naveen DhanarajNaveen Dhanaraj
Hi Justin,
TRY THIS
 
ACCOUNT TRIGGER HANDLER:

public class AccountTriggerHandler 
{
    public static void CreateAccounts(List<Account> acclist)
    {
        for(Account a:acclist)
        {
            if(a.ShippingState!=a.BillingState)
            {
                a.ShippingState = a.BillingState; // you need to update ShippingState
            }
        }
    }
}
 
TEST CLASS:

@isTest
public class AccountTriggerTest {
    
    @isTest static void TestCreate200Records()
    {
        List<Account> accts = new List<Account>();
        for(Integer i=0; i < 200; i++) 
        {
            Account acct = new Account(Name='Test Account ' + i, BillingState = 'CA');
            accts.add(acct);
        }
        
        Test.startTest();
            insert accts ;
        Test.stopTest();
        // Please query all record once again to check assert
        List<Account> lstAccount = [select ShippingState from Account];
        for (Account a: lstAccount )
        {
            System.assertEquals('CA', a.ShippingState, 'ERROR');
        }
        
    }
}
 
TRIGGER:

trigger AccountTrigger on Account (before insert) 
{
    if (Trigger.isBefore && Trigger.isInsert) {
			AccountTriggerHandler.CreateAccounts(Trigger.new);
		}	
}

 
This was selected as the best answer
Justin DeckJustin Deck
Thank you so so very much!
Naveen DhanarajNaveen Dhanaraj
Hi Justin,
If this helps you choose this as Best Answer
Justin DeckJustin Deck
All set!
MarkOCTMarkOCT
A straigtforward solution would be to retrieve the timestamp from the Event and check it against a range of time, this should do the trick.
public boolean evaluate(TxnSecurity.Event e) {

          DateTime currD = System.now();
          DateTime endD = System.now() - 7;
          DateTime event = e.timeStamp;
        
          if( (event >= endD) && (event <= currD))
              return true;

          return false;
	}

 
David Catindoy 101David Catindoy 101
I'm not sure why above answer was marked as best answer where it does not answer the question at all. Here's an answer that works for me:
 
global class NewURLPolicyCondition implements TxnSecurity.PolicyCondition {

    public boolean evaluate(TxnSecurity.Event e) {
        AggregateResult[] results = [SELECT ApiType FROM LoginHistory WHERE UserId = :e.userId AND LoginTime = LAST_N_DAYS:7 GROUP BY ApiType];

        if(!results.isEmpty() && results.size() > 1) {
           return true;
        }

    return false; 
    }
}

 
Tomasz JurekTomasz Jurek
Hi I followed this module and I don't have this part of code
public boolean evaluate(TxnSecurity.Event e) {
AggregateResult[] results = [SELECT ApiType FROM LoginHistory WHERE UserId = :e.userId AND LoginTime = LAST_N_DAYS:2 GROUP BY ApiType];
Where this was taken from? I did all previous steps and do not recall LAST N DAYS config through UI?
 
Tomasz JurekTomasz Jurek
Okay what I've done is delete class and policy and recreate it.  When you do pls focus on this section
User-added image

This will give you easy to edit option later on in apex class
Andrew EversleyAndrew Eversley
Thanks to Andreea Capata 16 for his great help and assistance. Appreciate it.
Beto Carvalho 11Beto Carvalho 11
1) Answer chosen as "best answer" on this page doesn't even address the issue.

2) Several people keep repeated "just edit LAST_N_DAYS from 2 to 7". If that was present in the code for everyone, it'd be obvious. Here's what the code in my org looked like, even after completing all the previous challenges successfully. 
global class NewURLPolicyCondition implements TxnSecurity.PolicyCondition {
 public boolean evaluate(TxnSecurity.Event e) {
 return false; 
}
 }
User-added image
 
3) Naveen  Dhanaraj (https://dfc-org-production.force.com/forums/ForumsProfile?communityId=09aF00000004HMG&userId=005F0000007EbyP&showHeader=false)'s response above works on the first try.
 
Adam OsieckiAdam Osiecki
Hi, currently (march 2018) this task looks like this:
User-added image
and here is code which actually help you pass this challenge:
global class NewURLPolicyCondition implements TxnSecurity.PolicyCondition {

 public boolean evaluate(TxnSecurity.Event e) {
     LoginHistory eObj = [SELECT Platform FROM LoginHistory WHERE Id = :e.data.get('LoginHistoryId')];
     if(eObj.Platform ==  'besupercarefulwiththis'){
         return true;
     }
     return false;
     }
 }

 
Mahnaaz Mohammadi 2Mahnaaz Mohammadi 2
Hi Adam,
Your answers are the best to solve this exercise.
Thanks,
mah 
Roland Knippenberg 5Roland Knippenberg 5
The Transaction Security module should be changed. It's not available in Classic anymore.