• Kritika Raj
  • NEWBIE
  • 85 Points
  • Member since 2020

  • Chatter
    Feed
  • 3
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 17
    Replies
Hello,

i need to test a class which needs account.isPersonAccount set.
My test fails, because i am not able to initialize the property inside the test method.

My test method:

@IsTest
    public static void testPersonAccountFields() {
        Schema.RecordTypeInfo recordTypeInfo = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByDeveloperName().get('PersonAccount');
        System.assert(recordTypeInfo != null, 'There must be a Person Account record type');
        String recordTypeId = recordTypeInfo.getRecordTypeId();
        Account account = new Account(RecordTypeId = recordTypeId);
        account.FirstName = 'First';
        account.LastName = 'Last';
        System.assert(account.isPersonAccount, 'must be a person account');
        // here call code which needs is PersonAccount set
    }

any help appreciated.

Markus
I have written a Apex class which is getting called in a trigger. Now I have written a test class for the Apex class. But my code coverage is 0%.

APEX CLASS

public class ChangeOwnerClass{

    public static void changeOwner(Quote quote){
        
        Opportunity opp = [Select ID, CreatedById, OwnerId from Opportunity where Id =: quote.OpportunityId];
        System.debug(opp.OwnerId);
        if(opp.OwnerId != null) {}
            User user = [Select Id,ProfileId from User where Id=: opp.OwnerId];
            System.debug(user.Id);
            if(user!= null && user.ProfileId != null) {
                Profile profile = [Select name from Profile where Id =: user.ProfileId ];
                System.debug(profile.name);
                if(profile !=null && profile.name != null && profile.name == 'TriLink Sales & Service') {
                    quote.ownerId = opp.CreatedById;
                }            
            }
        }
    }

TRIGGER

trigger ChangeOwner on Quote (before insert) {
    
        for(Quote q: Trigger.new)
        {
            ChangeOwnerClass.changeOwner(q);
        }        
    }

TEST CLASS

@isTest
public class ChangeOwnerTest {

    @isTest static void changeOwnerTest(){
        Opportunity opp = new Opportunity();
        opp.Name = 'Test';
        opp.LeadSource = 'Test';
        opp.StageName = 'Identification';
        opp.CloseDate = Date.newInstance(2020, 12, 14);
        opp.OwnerId = '0056A00000264YcQAI';
        insert opp;
        if(opp.OwnerId != null) {}
        User user = [Select Id,ProfileId from User where Id=: opp.OwnerId];
        if(user!= null && user.ProfileId != null){
            Profile profile = [Select name from Profile where Id =: user.ProfileId ];
            if(profile !=null && profile.name != null && profile.name == 'TriLink Sales & Service'){
                Quote q = new Quote();
                q.Id = opp.Id;
                q.ownerId = opp.CreatedById;
                insert q;
            }
        }
    }
}

Please help me to modify it to get the code coverage.
Hi evryone,

how to write test class for below code if any one know please help me  and if possible please explain me.

public String getFieldByName(ProspectUser__c prospectUser, String fieldName ){
        if(fieldName.equals('FirstName')){
                return prospectUser.FirstName__c;
            }else if(fieldName.equals('LastName')){
                return prospectUser.LastName__c;
            }else if(fieldName.equals('Gender')){
                return prospectUser.Gender__c;
            }else if(fieldName.equals('Email')){
                return prospectUser.Email__c;
            }else if(fieldName.equals('DateOfBirth')){
                return String.valueOf(prospectUser.DOB__c.format());
            }else if(fieldName.trim().equalsIgnoreCase('MemberId')){
                return prospectUser.Member_ID__c;
            }else{
                return '';
            }
    }

thanks inadvance,
raju
In Pardot we have a Prospect Activity tab as follows: 
User-added image
I want to have a custom field in Salesforce as Form Handler Last Activity and Visit Last Activity which will be equal to the latest Date/Time for these Activities. Ex. Here for this Prospect , In Salesforce
Form Handler Last Activity = Sep 8, 2020 11:21 AM ; 
Visit Last Activity = Sep 9, 2020 8:08 AM
Can please someone suggest a way how this can be achieved as I am not able to find a solution for this?
I am publishing the events in such a way that first the controller executes the publish method for Parent and then the child . In System.debug , the statements are printed accordingly such as 
- Successfully Published Parent Event
- Successfully Published Child Event

But the Child replayId is less and Parent is greater indicating that child event was published first . Please , can someone explain the reason why this might happen?
In Pardot we have a Prospect Activity tab as follows: 
User-added image
I want to have a custom field in Salesforce as Form Handler Last Activity and Visit Last Activity which will be equal to the latest Date/Time for these Activities. Ex. Here for this Prospect , In Salesforce
Form Handler Last Activity = Sep 8, 2020 11:21 AM ; 
Visit Last Activity = Sep 9, 2020 8:08 AM
Can please someone suggest a way how this can be achieved as I am not able to find a solution for this?
Hi Team,

I am stuck in a trigger. I have 2 look-up fields on Product obejct. Through first look-up field i.e  'Parent Id'  we can select Account Name and through second look-up field we select Account Manager Name. Now my requirement is as i select the Account Name in first look-up field, it should automatically populate respective Account Manager Name in second look-up field  .
I have created the trigger.

trigger AddAccountManager on Product2 (before update) {
        Set<Id> Ids = new Set<Id>();
    for(Product2 p : Trigger.new){
        Ids.add(p.Parent_ID__c);
    }
    Map<Id, Account> accountownername = new Map<Id , Account>([SELECT id, Name, AccountManager__c FROM Account WHERE Id IN: Ids]);
    for(Product2 p : Trigger.new){
        if(p.Parent_ID__c != null && accountownername.containsKey(p.Parent_ID__c))   {
            p.Account_Manager__c = accountownername.get(p.Parent_ID__c).AccountManager__c;
        }
    }
     }  

I am getting this error:

Apex trigger AddAccountManager caused an unexpected exception, contact your administrator: AddAccountManager: execution of AfterUpdate caused by: System.FinalException: Record is read-only: ()

Please help me to solve this.

Thanks,
Mahesh
Hello,

I have created a trigger to update a custom field called "Chiffre_d_affaire__c". Anytime an Order with the status 'Activated' is added to an account. The amount of the order should be added to the field "Chiffre_d_affaire__c".
So let's say my : Chiffre_d_affaire__c = 100 000.00 ;
Order (TotalAmount) = 85 000.00 ;
the Chiffre_d_affaire__c should be updated to 185 000.00
But instead of adding to amount of the order to Chiffre_d_affaire__c;
The Trigger overwrite the 100 000.00 in Chiffre_d_affaire__c with the Order.
So Chiffre_d_affaire__c is updated with 85 000.00
How can I fix it?
Thanks

Initial Chiffre_d_affaire__c :
User-added imageOrder added to Account

User-added image

Chiffre_d_affaire__c  overwritten with Order TotalAmount

User-added image

my trigger:
 
trigger UpdateAccountCA on Order (after update, before delete) {

    Set<Id> accountIds = new Set<Id>();
   
    if(Trigger.isUpdate) {
        //Iterate through each Order
        for(Order o : Trigger.new) {
            accountIds.add(o.AccountId);
        }
    }  
    else if(Trigger.isDelete) {
        for(Order o : Trigger.old) {
            accountIds.add(o.AccountId);
        }
    }
       
    AggregateResult[] groupedResults = [SELECT AccountId, SUM(TotalAmount)amt FROM Order WHERE AccountId In : accountIds  AND Status = 'Activated' GROUP BY AccountId];
   
List<Account> toBeUpdateAccount = new List<Account>();
    for(AggregateResult Results: groupedResults) {
        Account acc = new Account();
acc.Id = (id) Results.get('AccountId');
        acc.Chiffre_d_affaire__c = (Decimal)Results.get('amt');
        toBeUpdateAccount.add(acc);
    }
    update toBeUpdateAccount;
}

 
Hello,

i need to test a class which needs account.isPersonAccount set.
My test fails, because i am not able to initialize the property inside the test method.

My test method:

@IsTest
    public static void testPersonAccountFields() {
        Schema.RecordTypeInfo recordTypeInfo = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByDeveloperName().get('PersonAccount');
        System.assert(recordTypeInfo != null, 'There must be a Person Account record type');
        String recordTypeId = recordTypeInfo.getRecordTypeId();
        Account account = new Account(RecordTypeId = recordTypeId);
        account.FirstName = 'First';
        account.LastName = 'Last';
        System.assert(account.isPersonAccount, 'must be a person account');
        // here call code which needs is PersonAccount set
    }

any help appreciated.

Markus
I have written a Apex class which is getting called in a trigger. Now I have written a test class for the Apex class. But my code coverage is 0%.

APEX CLASS

public class ChangeOwnerClass{

    public static void changeOwner(Quote quote){
        
        Opportunity opp = [Select ID, CreatedById, OwnerId from Opportunity where Id =: quote.OpportunityId];
        System.debug(opp.OwnerId);
        if(opp.OwnerId != null) {}
            User user = [Select Id,ProfileId from User where Id=: opp.OwnerId];
            System.debug(user.Id);
            if(user!= null && user.ProfileId != null) {
                Profile profile = [Select name from Profile where Id =: user.ProfileId ];
                System.debug(profile.name);
                if(profile !=null && profile.name != null && profile.name == 'TriLink Sales & Service') {
                    quote.ownerId = opp.CreatedById;
                }            
            }
        }
    }

TRIGGER

trigger ChangeOwner on Quote (before insert) {
    
        for(Quote q: Trigger.new)
        {
            ChangeOwnerClass.changeOwner(q);
        }        
    }

TEST CLASS

@isTest
public class ChangeOwnerTest {

    @isTest static void changeOwnerTest(){
        Opportunity opp = new Opportunity();
        opp.Name = 'Test';
        opp.LeadSource = 'Test';
        opp.StageName = 'Identification';
        opp.CloseDate = Date.newInstance(2020, 12, 14);
        opp.OwnerId = '0056A00000264YcQAI';
        insert opp;
        if(opp.OwnerId != null) {}
        User user = [Select Id,ProfileId from User where Id=: opp.OwnerId];
        if(user!= null && user.ProfileId != null){
            Profile profile = [Select name from Profile where Id =: user.ProfileId ];
            if(profile !=null && profile.name != null && profile.name == 'TriLink Sales & Service'){
                Quote q = new Quote();
                q.Id = opp.Id;
                q.ownerId = opp.CreatedById;
                insert q;
            }
        }
    }
}

Please help me to modify it to get the code coverage.
Hello guys,

i wonder if there is a way to avoid the for loop and make the query more efficient. I still want to query the line items and get a set of account ids.
Is it possible to get a map like <parent.id, opportunityLineItem>  ?
public static Set<Id> runningMemberships (Date dateField){
        Set<Id> idSet = new Set<Id> ();

        List<OpportunityLineItem> oliList = [
            SELECT Id, Opportunity.AccountId
            FROM OpportunityLineItem
            WHERE Opportunity.IsWon = true
            AND Opportunity.start__c <= :dateField
            AND Opportunity.end__c >= :dateField
            AND family__c = 'membership''
            LIMIT 50000
        ];

        for (OpportunityLineItem oli : oliList) {
            idSet.add(oli.Opportunity.AccountId);
        }
        return idSet;
    }
Greetings,

Klaus
 
I am publishing the events in such a way that first the controller executes the publish method for Parent and then the child . In System.debug , the statements are printed accordingly such as 
- Successfully Published Parent Event
- Successfully Published Child Event

But the Child replayId is less and Parent is greater indicating that child event was published first . Please , can someone explain the reason why this might happen?
Hi evryone,

how to write test class for below code if any one know please help me  and if possible please explain me.

public String getFieldByName(ProspectUser__c prospectUser, String fieldName ){
        if(fieldName.equals('FirstName')){
                return prospectUser.FirstName__c;
            }else if(fieldName.equals('LastName')){
                return prospectUser.LastName__c;
            }else if(fieldName.equals('Gender')){
                return prospectUser.Gender__c;
            }else if(fieldName.equals('Email')){
                return prospectUser.Email__c;
            }else if(fieldName.equals('DateOfBirth')){
                return String.valueOf(prospectUser.DOB__c.format());
            }else if(fieldName.trim().equalsIgnoreCase('MemberId')){
                return prospectUser.Member_ID__c;
            }else{
                return '';
            }
    }

thanks inadvance,
raju