• Achtung
  • NEWBIE
  • 35 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 18
    Replies
I am trying to reduce the inefficiency of my code by avoiding nested For Loops. Any advice on you can give based from my code below?

I'm basically updating all the related Job Addresses of a Contact if the Contact's Other Address is updated.
Trigger ChangeJobAddress on Contact (before update) {
           
        Set<Id> setOfContacts = new Set<Id>();
        for(Contact c : Trigger.new){
            
            if(c.Other_Address__c != Trigger.oldMap.get(c.Id).Other_Address__c)
                setOfContacts.add(c.Id);           
        }
                
        if(setOfContacts.size() > 0){            
            Map<Id, List<sked__Job__c>> ContactJobs = new Map<Id, List<sked__Job__c>>();
            List<sked__Job__c> jobsAddress = new List <sked__Job__c>();
            
            jobsAddress = [SELECT Id, JobName, JobType__c, 
                           JobAddress__c,StartDate__c, ContactName__c,
                           JobLocation__c, JobStatus__c
                           FROM Job__c
                           WHERE JobType__c = 'Non-Recurring'
                           AND JobStart__c > TODAY
                           AND JobLocation__c = NULL
                           AND (JobStatus__c != 'Complete')
                           AND ContactName__c IN :setOfContacts
                           ORDER BY Name];
                      
            for (Job__c job : jobsAddress){
                if (ContactJobs.get(job.ContactName__c) == NULL){
                    ContactJobs.put(job.ContactName__c, new List<sked__Job__c>());                  
                }
                ContactJobs.get(job.ContactName__c).add(job);
            }               
            
            for(Contact con : Trigger.new){                
                if (ContactJobs.size() > 0){                             
                    if (ContactJobs.get(con.Id).size() > 0){   
                        //NEED TO AVOID THIS ONE
                        for (Job__c j :ContactJobs.get(con.Id)){
                            string otherStreet = Trigger.oldMap.get(con.Id).OtherStreet;
                            string otherCity = Trigger.oldMap.get(con.Id).OtherCity;
                            string otherState = Trigger.oldMap.get(con.Id).OtherState;
                            string OtherPostal = Trigger.oldMap.get(con.Id).OtherPostalCode;
                            string addressFormat1 = otherStreet + ', ' + otherCity + ', ' + otherState + ' ' + otherPostal;
                            string addressformat2 = otherStreet + ', ' + otherCity + ' ' + otherState + ' ' + otherPostal;
                            
                            if(j.JobAddress__c == addressFormat1 || j.JobAddress__c == addressFormat2){
                                j.JobAddress__c = con.Other_Address__c;  
                                update j;                               
                            }                            
                            else { 
                                System.debug('No Job Address updated.');     
                            }
                        }
                    }
                }
            }         
        }

 
So I created a List of List in my Apex Trigger code to list out all service IDs and the Job IDs associated with them. The structure looks like this:
User-added image

The Service object contains the startDate and endDate fields. The Job object also has jobStart and jobEnd date fields.

What I intend to do is, if the dates of a Service record ID are updated, it will loop through all the jobs assigned to that service ID and check if there are dates that are not within the new date duration. This won't allow the user to update the Service record because jobs currently associated with it conflict with the new dates. 

I have a Contact Object with two record types (Client & Intake).

I am enabling the "Files" related list to both page layouts, however, I only want the Intake record type to have the "Upload Files" or "Add Files" capability. The Client record will only be able to View Files.

I want to have this feature though regardless of User Profiles. Is writing a Trigger the best solution?
Hello! I'm creating a before update Trigger that won't allow changing the agreement_start_date and agreement_end_date of an agreement when the jobs associated with it are not within the duration of the new dates.

Background:
- An agreement needs an agreement_start_date and agreement_end_date as its duration. 
- An agreement can have multiple Jobs assigned to it and every Job only has one service agreement assigned.
- Every Job has a job_start_date and job_end_date and is within the duration of the agreement_start_date and agreement_end_date.

Agreement_ABC
agreement_start_date = June 1, 2018
agreement_end_date = July 31, 2018

1. Job_01: 
job_start_date = July 1, 2018
job_end_date = July 15, 2018

2. Job_02:
job_start_date = June 2, 2018
job_end_date = June 30, 2018

Agreement_ABC New Dates
agreement_start_date = June 5, 2018
agreement_end_date = July 30, 2018
---> display an error disallowing to save as the new start date conflicts with Job_02 job_start_date

Scenario:
I want to update the agreement_start_date and agreement_end_date with new date values, however, a Trigger will not allow them to save the new dates if jobs associated to it are not within the new date duration.

How can I maximise the use of Set, List, and Map to develop the Trigger? Thanks.
Hi,

I'm currently creating a process builder that will automatically copy my field values from the child object to its parent object. 

The scenario is, in my child object, I have three record types and each record store a value to their respective field. In the parent object, I have three respective fields and my goal is to populate this three fields from the three record types of the child object.

Say, child record type A has a field value 'A', child record type B has a field value 'B', and child record type C has a field value 'C'. Once the object is either created or updated, the process builder shall copy these three fields to their designated fields in the parent object.

I currently created three separate process builders that copies the fields depending on their record types. However, I want to combine this into a single process builder for easy maintenanceand troubleshooting.

Appreciate any ideas on this! :) 
I am trying to reduce the inefficiency of my code by avoiding nested For Loops. Any advice on you can give based from my code below?

I'm basically updating all the related Job Addresses of a Contact if the Contact's Other Address is updated.
Trigger ChangeJobAddress on Contact (before update) {
           
        Set<Id> setOfContacts = new Set<Id>();
        for(Contact c : Trigger.new){
            
            if(c.Other_Address__c != Trigger.oldMap.get(c.Id).Other_Address__c)
                setOfContacts.add(c.Id);           
        }
                
        if(setOfContacts.size() > 0){            
            Map<Id, List<sked__Job__c>> ContactJobs = new Map<Id, List<sked__Job__c>>();
            List<sked__Job__c> jobsAddress = new List <sked__Job__c>();
            
            jobsAddress = [SELECT Id, JobName, JobType__c, 
                           JobAddress__c,StartDate__c, ContactName__c,
                           JobLocation__c, JobStatus__c
                           FROM Job__c
                           WHERE JobType__c = 'Non-Recurring'
                           AND JobStart__c > TODAY
                           AND JobLocation__c = NULL
                           AND (JobStatus__c != 'Complete')
                           AND ContactName__c IN :setOfContacts
                           ORDER BY Name];
                      
            for (Job__c job : jobsAddress){
                if (ContactJobs.get(job.ContactName__c) == NULL){
                    ContactJobs.put(job.ContactName__c, new List<sked__Job__c>());                  
                }
                ContactJobs.get(job.ContactName__c).add(job);
            }               
            
            for(Contact con : Trigger.new){                
                if (ContactJobs.size() > 0){                             
                    if (ContactJobs.get(con.Id).size() > 0){   
                        //NEED TO AVOID THIS ONE
                        for (Job__c j :ContactJobs.get(con.Id)){
                            string otherStreet = Trigger.oldMap.get(con.Id).OtherStreet;
                            string otherCity = Trigger.oldMap.get(con.Id).OtherCity;
                            string otherState = Trigger.oldMap.get(con.Id).OtherState;
                            string OtherPostal = Trigger.oldMap.get(con.Id).OtherPostalCode;
                            string addressFormat1 = otherStreet + ', ' + otherCity + ', ' + otherState + ' ' + otherPostal;
                            string addressformat2 = otherStreet + ', ' + otherCity + ' ' + otherState + ' ' + otherPostal;
                            
                            if(j.JobAddress__c == addressFormat1 || j.JobAddress__c == addressFormat2){
                                j.JobAddress__c = con.Other_Address__c;  
                                update j;                               
                            }                            
                            else { 
                                System.debug('No Job Address updated.');     
                            }
                        }
                    }
                }
            }         
        }

 
So I created a List of List in my Apex Trigger code to list out all service IDs and the Job IDs associated with them. The structure looks like this:
User-added image

The Service object contains the startDate and endDate fields. The Job object also has jobStart and jobEnd date fields.

What I intend to do is, if the dates of a Service record ID are updated, it will loop through all the jobs assigned to that service ID and check if there are dates that are not within the new date duration. This won't allow the user to update the Service record because jobs currently associated with it conflict with the new dates. 
Hello! I'm creating a before update Trigger that won't allow changing the agreement_start_date and agreement_end_date of an agreement when the jobs associated with it are not within the duration of the new dates.

Background:
- An agreement needs an agreement_start_date and agreement_end_date as its duration. 
- An agreement can have multiple Jobs assigned to it and every Job only has one service agreement assigned.
- Every Job has a job_start_date and job_end_date and is within the duration of the agreement_start_date and agreement_end_date.

Agreement_ABC
agreement_start_date = June 1, 2018
agreement_end_date = July 31, 2018

1. Job_01: 
job_start_date = July 1, 2018
job_end_date = July 15, 2018

2. Job_02:
job_start_date = June 2, 2018
job_end_date = June 30, 2018

Agreement_ABC New Dates
agreement_start_date = June 5, 2018
agreement_end_date = July 30, 2018
---> display an error disallowing to save as the new start date conflicts with Job_02 job_start_date

Scenario:
I want to update the agreement_start_date and agreement_end_date with new date values, however, a Trigger will not allow them to save the new dates if jobs associated to it are not within the new date duration.

How can I maximise the use of Set, List, and Map to develop the Trigger? Thanks.
Hi,

I'm currently creating a process builder that will automatically copy my field values from the child object to its parent object. 

The scenario is, in my child object, I have three record types and each record store a value to their respective field. In the parent object, I have three respective fields and my goal is to populate this three fields from the three record types of the child object.

Say, child record type A has a field value 'A', child record type B has a field value 'B', and child record type C has a field value 'C'. Once the object is either created or updated, the process builder shall copy these three fields to their designated fields in the parent object.

I currently created three separate process builders that copies the fields depending on their record types. However, I want to combine this into a single process builder for easy maintenanceand troubleshooting.

Appreciate any ideas on this! :) 
My trigger:-
trigger accountDelete on Account (before Delete) {
    List<Account> accs=[select id,(select id from contacts ) from Account where id in:Trigger.old];
    for(Account a:accs){
        if(a.contacts.size()>0)
            a.addError('Account cannot be deleted where it has contacts');
    }
}
Hi everyone,

I have this trigger to delete a document when a lead is deleted :
trigger DeleteImageLead on Lead (before delete) {

    if (Trigger.isDelete) {
        for (Lead l : Trigger.old)
        {
            List<ContentDocument> myFeed = [SELECT LatestPublishedVersionId FROM ContentDocument WHERE Id In (SELECT ContentDocumentId FROM ContentVersion WHERE Id =:l.PhotoLead_Image__c)];
            delete myFeed;
        }
    }
}
When I deploy it on a production environment, I have a code coverage error because it is 0%.
Can you please help me with to make an apex test class for this code ? I tried to develop it but I'm still with 0% code coverage

Thanks a lot !

Frédéric
 
  • January 27, 2017
  • Like
  • 0
Hi,

I have a requirement to capture an image in visual force page either from computer or through Camera(Webcam/phone camera/tablet camera). What all frameworks/technologies that we have to implement in visual force page to accomplish this.

Thanks
Siva
  • July 21, 2016
  • Like
  • 0
Hi There,

It was recommended in the Success Community that I come here.

We are hoping to automatically capture the geolocation of a mobile device when a new Task/Call is created in Salesforce1.

Ideally, an on-screen field that would be read only and would auto populate.

I have managed to get an on-screen popup by incorporating this visual force page as a mobile card - https://developer.salesforce.com/forums/?id=906F0000000AwiFIAS

But can't figure out how to 'capture' this information.

Basically, by the end of a day, we would like to be able to plot/see on a map where our reps had created Activity(task/call) records following client visits.

We have found a number of applications like this on the app exchange, though I am reasonably positive my basic requirement can be handled without buying add on applications/functionality.

My logic being that if I can get the device coordinates to display, surely they can be captured.

Any assistance would be greatly appreciated!
 
Thanks,
Steve

I recall something along time ago were you could stop the deleting of a record if it has children (in a master or lookup relationship).

 

The method used validation rules instead of a trigger. 

 

Anyone know how to do this?

 

Thanks

  • October 01, 2013
  • Like
  • 0