• Wes Reed 27
  • NEWBIE
  • 20 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 4
    Replies
Is there a way to automatically update the Callable_Contacts__c field on the Account object when a related Contact is updated?
 
trigger CallableContacts on Account (before insert, before update) {
    for(Account acc : Trigger.new) {
        //Search for related Contacts
        List<Contact> callableContacts = [SELECT  Id, 
                                            Phone, 
                                            Account.Name 
                                            FROM Contact 
                                            WHERE Account.Id = :acc.Id];
        System.debug('Callable contacts found: ' + callableContacts.size());

        //For each contact, check whether the Phone Number field is filled in and increment the callable contacts field on Account
        Integer count = 0; 
        if(!callableContacts.isEmpty()) {
            for(Contact con : callableContacts) {
                if(con.Phone != null) {
                    count = count + 1;
                    acc.Callable_Contacts__c = count;
                }
            }
        }
    }
}

 
In the last for each loop, I'd like to display the field label rather than the field value (which is how it is currently) for the task subject. I'd also like the tasks to be related to the Lead. 
 
trigger LeadKeyFieldsPopulated on Lead (before insert) {
    for(Lead l : Trigger.new){
        
        List<String> fields = new List<String>();
        fields.add(l.FirstName);
        fields.add(l.LastName);
        fields.add(l.Phone);
        fields.add(l.Email);
        fields.add(l.Website);
        fields.add(l.Title);

        List<String> populatedFields = new List<String>();
        Integer count = 0;
        for(String field : fields){
            if(String.isNotBlank(field)){
                count = count + 1;
                l.Key_Fields_Populated__c = count;
                populatedFields.add(field);
            }
        }

        List<String> labels = new List<String>();
        labels.add(Schema.Lead.fields.FirstName.getDescribe().getLabel());
        labels.add(Schema.Lead.fields.LastName.getDescribe().getLabel());
        labels.add(Schema.Lead.fields.Phone.getDescribe().getLabel());
        labels.add(Schema.Lead.fields.Email.getDescribe().getLabel());
        labels.add(Schema.Lead.fields.Website.getDescribe().getLabel());
        labels.add(Schema.Lead.fields.Title.getDescribe().getLabel());

        List<Task> taskToCreate = new List<Task>();
        if(l.Key_Fields_Populated__c >= 3) {
            for(String field : populatedFields) {
                Task myTask    = new Task();
                myTask.WhatId  = l.Id; 
                myTask.Subject = 'Verify the ' + field + ' field';
                taskToCreate.add(myTask);
            }
            insert taskToCreate;
        }
    }
}

 

We have a custom lable with a falue of false. I'd like to have a visualforce page with a form that allows the value of the custom lable to be changed to true, without having to go into Setup to edit. Here is what I have so far and im getting this error on my vf page: Read only property 'ApexPagesELAdapterContext.$Label.BypassTriggers'; and this error on my controller: Duplicate field: myLabel.

Controller so far:
 

public class changeBypassTriggerLabel {
    String myLabel = System.Label.BypassTriggers;
    
    public String myLabel{get; set;}
    
    public changeBypassTriggerLabel(){
        
    }

}
VF page so far:
 
<apex:page controller="changeBypassTriggerLabel">
    <apex:form id="changeBypassLabel">
        <apex:pageBlock title="Quick Edit Bypass Trigger">
            <apex:pageBlockSection>
                <p>
                    Bypass Label currently set to: {!$Label.BypassTriggers}
                </p>
                <p>
                    Change label to: 
                </p>
				<apex:inputText id="newLabel" value="{!$Label.BypassTriggers}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

What is there left to accomplish goal? Thanks.
Hi, new to the world of apex and im working on my first test class and I am completely stuck on where to begin. Here is my trigger. Any help with how to get started writing the test would be greatly appreciated.
 
trigger ContactAfterDeleteTrigger on Contact(After delete) {

    String getOldAccountId;
    String getNewAccountID;
    Decimal sumPoints = 0;
    Decimal countRecords = 0;

    For(Contact objOldContact: trigger.old) {
        //Get other contacts related to same OLD account and sum their engagement points:   
        Account objOldAccount = [SELECT id FROM Account WHERE id =: objOldContact.AccountID];
        List < Contact > contactOldList = [SELECT id, EngagementPoints__c, EngagementRecords__c FROM Contact WHERE AccountId =: objOldAccount.Id];
        For(Contact i: contactOldList) {
            sumPoints = sumPoints + i.EngagementPoints__c;
            countRecords = countRecords + i.EngagementRecords__c;
        }
        //Update the OLD account's engagement score and commit change:
        objOldAccount.IndividualEngagementScore__c = sumPoints;
        objOldAccount.IndividualEngagementRecords__c = countRecords;
        Update objOldAccount;
    }
  
}

 
I have these two apex triggers that I need help writing test for. As a newbie to salesforce devlopment, it took some time to finally get these triggers working. Thanks for your help. - Wes
 
trigger IndvidualEngagementScore on EngagementContact__c(after insert, after update, after delete) {

    Contact objContact;
    String getAccountId;
    Decimal sumPoints = 0;
    Decimal countRecords = 0;

    //Set initial values based on current engagement record's contact and trigger action:
    if (Trigger.isDelete) {
        For(EngagementContact__c delEngagement: trigger.old) {
            objContact = [SELECT id, AccountID FROM Contact WHERE id =: delEngagement.Contact__c];
            getAccountId = objContact.AccountId;
            if (delEngagement.ParticipationPoints__c == NULL) {
                sumPoints = 0;
                countRecords = 0;
            }
            Else {
                sumPoints = sumPoints - delEngagement.ParticipationPoints__c;
                countRecords = countRecords - 1;
            }
        }

    }
    Else {
        For(EngagementContact__c objEngagement: trigger.new) {
            objContact = [SELECT id, AccountID FROM Contact WHERE id =: objEngagement.Contact__c];
            getAccountId = objContact.AccountId;
            sumPoints = sumPoints + objEngagement.ParticipationPoints__c;
            countRecords = countRecords + 1;
            if (Trigger.isUpdate) {
                For(EngagementContact__c oldEngagement: trigger.old) {

                    if (oldEngagement.ParticipationPoints__c == NULL) {
                        sumPoints = 0;
                        countRecords = 0;
                    }
                    Else {
                        sumPoints = sumPoints - oldEngagement.ParticipationPoints__c;
                        countRecords = countRecords - 1;
                    }


                }
            }
        }
    }

    //Get other contacts related to same account and sum their engagement points:   
    Account objAccount = [SELECT id FROM Account WHERE id =: getAccountId];
    List < Contact > contactList = [SELECT id, EngagementPoints__c, EngagementRecords__c FROM Contact WHERE AccountId =: objAccount.Id];
    For(Contact i: contactList) {
        sumPoints = sumPoints + i.EngagementPoints__c;
        countRecords = countRecords + i.EngagementRecords__c;
    }

    //Update the account's engagement score and commit change:
    objAccount.IndividualEngagementScore__c = sumPoints;
    objAccount.IndividualEngagementRecords__c = countRecords;
    Update objAccount;

}

trigger IndividualEngagmentOrgRecalc on Contact(After update, After delete) {


    String getOldAccountId;
    String getNewAccountID;
    Decimal sumPoints = 0;
    Decimal countRecords = 0;

    For(Contact objOldContact: trigger.old) {
        //Get other contacts related to same OLD account and sum their engagement points:   
        Account objOldAccount = [SELECT id FROM Account WHERE id =: objOldContact.AccountID];
        List < Contact > contactOldList = [SELECT id, EngagementPoints__c, EngagementRecords__c FROM Contact WHERE AccountId =: objOldAccount.Id];
        For(Contact i: contactOldList) {
            sumPoints = sumPoints + i.EngagementPoints__c;
            countRecords = countRecords + i.EngagementRecords__c;
        }
        //Update the OLD account's engagement score and commit change:
        objOldAccount.IndividualEngagementScore__c = sumPoints;
        objOldAccount.IndividualEngagementRecords__c = countRecords;
        Update objOldAccount;
    }


    //Reset initial values
    sumPoints = 0;
    countRecords = 0;


    if (Trigger.isDelete) {}
    ELSE {
        For(Contact objNewContact: trigger.New) {
            //Get other contacts related to same OLD account and sum their engagement points:   
            Account objNewAccount = [SELECT id FROM Account WHERE id =: objNewContact.AccountId];
            List < Contact > contactNewList = [SELECT id, EngagementPoints__c, EngagementRecords__c FROM Contact WHERE AccountId =: objNewAccount.Id];
            For(Contact i: contactNewList) {
                sumPoints = sumPoints + i.EngagementPoints__c;
                countRecords = countRecords + i.EngagementRecords__c;
            }

            //Update the OLD account's engagement score and commit change:
            objNewAccount.IndividualEngagementScore__c = sumPoints;
            objNewAccount.IndividualEngagementRecords__c = countRecords;
            Update objNewAccount;
        }
    }
}


 
I am curious how to resolve this error: Didn't understand relationship 'EngagementContact__r' in FROM part of query call. 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.

My goal is to have this trigger perform a rollup summary between to objects based on their 'Lookup Relationship'. Any guidance or advice would be greatly appreciated. 

Thanks - Wes
trigger IndvidualEngagementScore on EngagementContact__c (after delete, after insert, after update) {
    Set<id> acctIds = new Set<id>();
    List<account> accountsToUpdate = new List<account>();
    
    for (EngagementContact__c item : Trigger.new)
        acctIds.add(item.Account__c);
        
        if (Trigger.isUpdate || Trigger.isDelete) {
            for (EngagementContact__c item : Trigger.old)
                acctIds.add(item.Account__c);
        }

    
    Map<id,Account> accoutMap = new Map<id,Account>([select id, Individual_Engagement_Score__c from Account where id IN :acctIds]);
    
    
    for(Account acct : [select Id, Name, Individual_Engagement_Score__c, (select id from EngagementContact__r) from Account where Id IN :acctIds]) {
        accountMap.get(acct.Id).Individual_Engagement_Score__c = acct.EngagementContacs__r.size();
        
        accountsToUpdate.add(accountMap.get(acct.Id));
    }
    
    update accountsToUpdate;
}

 
I am writing an Apex trigger to perform a rollup sum of two objects related by a lookup relationship. The Account (parent) object is supposed to have a number field (Individual Engagement Score) that has a value of the roll-up sum from the EngagmentContact custom (child) object. 

I keep getting this error: Didn't understand relationship 'EngagementContact__r' in FROM part of query call. 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.

Everything I am seeing online says there needs to be a Master-Detail relationship, but that's not an option. Any help would be great. Thank you! 

Wes
 
trigger IndvidualEngagementScore on EngagementContact__c (after delete, after insert, after update) {
	Set<id> acctIds = new Set<id>();
    List<account> accountsToUpdate = new List<account>();
    
    for (EngagementContact__c item : Trigger.new)
        acctIds.add(item.Account__c);
        
        if (Trigger.isUpdate || Trigger.isDelete) {
            for (EngagementContact__c item : Trigger.old)
                acctIds.add(item.Account__c);
        }

	// get a map of the Account with the number of Individual Engagements
	Map<id,Account> accoutMap = new Map<id,Account>([select id, Individual_Engagement_Score__c from Account where id IN :acctIds]);
    
    // query the Accounts and the related Individual Engagements and add the number of Individual Engagements to the Accounts's Individual_Engagement_Score__c
    for(Account acct : [select Id, Name, Individual_Engagement_Score__c, (select id from EngagementContact__r) from Account where Id IN :acctIds]) {
        accountMap.get(acct.Id).Individual_Engagement_Score__c = acct.EngagementContact__r.size();
        // add the account in the map to a list so we can update it
        accountsToUpdate.add(accountMap.get(acct.Id));
    }
    
    update accountsToUpdate;
}
Hi, new to the world of apex and im working on my first test class and I am completely stuck on where to begin. Here is my trigger. Any help with how to get started writing the test would be greatly appreciated.
 
trigger ContactAfterDeleteTrigger on Contact(After delete) {

    String getOldAccountId;
    String getNewAccountID;
    Decimal sumPoints = 0;
    Decimal countRecords = 0;

    For(Contact objOldContact: trigger.old) {
        //Get other contacts related to same OLD account and sum their engagement points:   
        Account objOldAccount = [SELECT id FROM Account WHERE id =: objOldContact.AccountID];
        List < Contact > contactOldList = [SELECT id, EngagementPoints__c, EngagementRecords__c FROM Contact WHERE AccountId =: objOldAccount.Id];
        For(Contact i: contactOldList) {
            sumPoints = sumPoints + i.EngagementPoints__c;
            countRecords = countRecords + i.EngagementRecords__c;
        }
        //Update the OLD account's engagement score and commit change:
        objOldAccount.IndividualEngagementScore__c = sumPoints;
        objOldAccount.IndividualEngagementRecords__c = countRecords;
        Update objOldAccount;
    }
  
}

 
I am curious how to resolve this error: Didn't understand relationship 'EngagementContact__r' in FROM part of query call. 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.

My goal is to have this trigger perform a rollup summary between to objects based on their 'Lookup Relationship'. Any guidance or advice would be greatly appreciated. 

Thanks - Wes
trigger IndvidualEngagementScore on EngagementContact__c (after delete, after insert, after update) {
    Set<id> acctIds = new Set<id>();
    List<account> accountsToUpdate = new List<account>();
    
    for (EngagementContact__c item : Trigger.new)
        acctIds.add(item.Account__c);
        
        if (Trigger.isUpdate || Trigger.isDelete) {
            for (EngagementContact__c item : Trigger.old)
                acctIds.add(item.Account__c);
        }

    
    Map<id,Account> accoutMap = new Map<id,Account>([select id, Individual_Engagement_Score__c from Account where id IN :acctIds]);
    
    
    for(Account acct : [select Id, Name, Individual_Engagement_Score__c, (select id from EngagementContact__r) from Account where Id IN :acctIds]) {
        accountMap.get(acct.Id).Individual_Engagement_Score__c = acct.EngagementContacs__r.size();
        
        accountsToUpdate.add(accountMap.get(acct.Id));
    }
    
    update accountsToUpdate;
}

 
I know i can do the following to get userid,firstname,lastname by using the static UserInfo class. But there is no method to get Email address. How do i get email address in a controller??

  String userId = UserInfo.getUserId();
String firstName = UserInfo.getFirstName();
String lastName = UserInfo.getLastName();

I know i can do {!$User.Email} but that would only be in the visualforce pages.
How do I get the email address of the logged in user in controller.