+ Start a Discussion
Derek Patrick DayaDerek Patrick Daya 

Update Resume Source field in Contact if its null and has a ETCObject record associated to it.

I am new to do and honestly do not have any Idea what I am doing. Can anyon help me on this?

This is our process: Send a resume to NewEmailToCandidate Address and it creates a ETCObject record in salesforce. Then a existing trigger checks if there is any existing contact:

If yes and the resume source field on that contact has a value then my trigger (not created yet) should ignore it and not update it. But if it does not have a value, then the trigger should fire and update it based on the Name field in the ETCObject as it contains the value that should be place on the resume source field. 

If no contact is found then an existing trigger in our org will create a new one. My trigger (not created yet) should fire and update the record with the correct resume source based on the name of the ETCObject.

Here is the code i am using and I could not save it due to 2 errors:

1. Error: Compile Error: expecting right curly bracket, found 'else' at line 20 column 13
2. Error: a.AVTRRT__Name__c does not exist

trigger UpdateSource on AVTRRT__ETCObject__c (after insert, after update) {
   
    Map<Id, AVTRRT__ETCObject__c> ETCmap = new Map<Id,AVTRRT__ETCObject__c>();
    for(AVTRRT__ETCObject__c a:trigger.new)
    {

            ETCmap.put(a.AVTRRT__Candidate__c, a);

           
    }
    List<Contact> candidate = [Select Id, AVTRRT__Source__c from Contact WHERE ID IN:ETCmap.keyset() AND RecordtypeID = '012A0000000v0La'];
    List<Contact> ContactsToUpdate = new List<Contact>();
    for(Contact c:candidate)
    {
       If(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('jobsDB'))
       {
               c.AVTRRT__Source__c = 'JobsDB';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('Clyde Marine Recruitment'))
               c.AVTRRT__Source__c = 'Clyde Marine Recruitment';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('Corporate Event'))
               c.AVTRRT__Source__c = 'Corporate Event';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('e Financial Careers'))
               c.AVTRRT__Source__c = 'e Financial Careers';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('Head Hunt'))
               c.AVTRRT__Source__c = 'Head Hunt';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('Indeed'))
               c.AVTRRT__Source__c = 'Indeed';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('Jobstreet'))
               c.AVTRRT__Source__c = 'Jobstreet';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('Linked In'))
               c.AVTRRT__Source__c = 'Linked In';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('Linkedin Advert'))
               c.AVTRRT__Source__c = 'Linkedin Advert';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('LinkedIn Recruiter'))
               c.AVTRRT__Source__c = 'LinkedIn Recruiter';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('Oil&Gas JobSearch'))
               c.AVTRRT__Source__c = 'Oil&Gas JobSearch';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('OilCareers'))
               c.AVTRRT__Source__c = 'OilCareers';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('Reed'))
               c.AVTRRT__Source__c = 'Reed';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('Referrals'))
               c.AVTRRT__Source__c = 'Referrals';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('S1 Jobs'))
               c.AVTRRT__Source__c = 'S1 Jobs';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('Straits Times'))
               c.AVTRRT__Source__c = 'Straits Times';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('Trade Winds'))
               c.AVTRRT__Source__c = 'Trade Winds';
           } else if(c.AVTRRT__Source__c == '' && a.AVTRRT__Name__c.contains('UK Job Centre'))
               c.AVTRRT__Source__c = 'UK Job Centre';
           } else {
               c.AVTRRT__Source__c = 'Other';
           }
       ContactsToUpdate.add(c);
       }
    }
    update ContactsToUpdate;
}

Please help me..
Best Answer chosen by Derek Patrick Daya
Shashikant SharmaShashikant Sharma

try this  :

Change this statement :

c.AVTRRT__Source__c = 'Other';

to 

c.AVTRRT__Source__c = ( ETCmap.containsKey(c.Id).AVTRRT__Name__c && ETCmap.get(c.Id).AVTRRT__Name__c ! =null ) ? ETCmap.get(c.Id).AVTRRT__Name__c : 'Other';


This should fix the issue.

All Answers

Shashikant SharmaShashikant Sharma
Not clear with the issue that you are having but you could see this fot developing trigger : 

http://forceschool.blogspot.in/

try it and let me know if you face issues.
Derek Patrick DayaDerek Patrick Daya
Hi Shashikant,

I was able to save the Trigger that I wanted to create. The only problem that I have right now is that it always puts in 'Other' on the Resume Source field in Contact eventhough the name of the ETCObject record contains 'JobsDB'.

What I aim to do is to Update Contact based on ETCObject record assosicated to it. So if the ETCObject record name contains JobsDB and the Contact record resume source field is null then it should be updated to JobsDB.

Current Code without Errors:

trigger DerekUpdateResumeSource on AVTRRT__ETCObject__c (after insert, after update) {
   
    Map<Id, AVTRRT__ETCObject__c> ETCmap = new Map<Id,AVTRRT__ETCObject__c>();
    for(AVTRRT__ETCObject__c a:trigger.new)
    {

            ETCmap.put(a.AVTRRT__Candidate__c, a);

           
    }
    List<Contact> candidate = [Select Id, AVTRRT__Source__c from Contact WHERE ID IN:ETCmap.keyset() AND RecordtypeID = '012A0000000v0La'];
    List<Contact> ContactsToUpdate = new List<Contact>();
    for(Contact c:candidate)
    {
       If(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('jobsDB'))
       {
               c.AVTRRT__Source__c = 'JobsDB';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('Clyde Marine Recruitment')){
               c.AVTRRT__Source__c = 'Clyde Marine Recruitment';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('Corporate Event')){
               c.AVTRRT__Source__c = 'Corporate Event';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('e Financial Careers')){
               c.AVTRRT__Source__c = 'e Financial Careers';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('Head Hunt')){
               c.AVTRRT__Source__c = 'Head Hunt';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('Indeed')){
               c.AVTRRT__Source__c = 'Indeed';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('Jobstreet')){
               c.AVTRRT__Source__c = 'Jobstreet';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('Linked In')){
               c.AVTRRT__Source__c = 'Linked In';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('Linkedin Advert')){
               c.AVTRRT__Source__c = 'Linkedin Advert';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('LinkedIn Recruiter')){
               c.AVTRRT__Source__c = 'LinkedIn Recruiter';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('Oil&Gas JobSearch')){
               c.AVTRRT__Source__c = 'Oil&Gas JobSearch';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('OilCareers')){
               c.AVTRRT__Source__c = 'OilCareers';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('Reed')){
               c.AVTRRT__Source__c = 'Reed';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('Referrals')){
               c.AVTRRT__Source__c = 'Referrals';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('S1 Jobs')){
               c.AVTRRT__Source__c = 'S1 Jobs';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('Straits Times')){
               c.AVTRRT__Source__c = 'Straits Times';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('Trade Winds')){
               c.AVTRRT__Source__c = 'Trade Winds';
               ContactsToUpdate.add(c);
           } else if(c.AVTRRT__Source__c == '' && ETCmap.get(c.Id).AVTRRT__Name__c.contains('UK Job Centre')){
               c.AVTRRT__Source__c = 'UK Job Centre';
               ContactsToUpdate.add(c);
           } else {
               c.AVTRRT__Source__c = 'Other';
               ContactsToUpdate.add(c);
           }
    }
    update ContactsToUpdate;
}

Thank you in advance.
Shashikant SharmaShashikant Sharma

try this  :

Change this statement :

c.AVTRRT__Source__c = 'Other';

to 

c.AVTRRT__Source__c = ( ETCmap.containsKey(c.Id).AVTRRT__Name__c && ETCmap.get(c.Id).AVTRRT__Name__c ! =null ) ? ETCmap.get(c.Id).AVTRRT__Name__c : 'Other';


This should fix the issue.

This was selected as the best answer
Derek Patrick DayaDerek Patrick Daya
I am receiving the error below:

Error: Compile Error: expecting a right parentheses, found '!' at line 71 column 116
Derek Patrick DayaDerek Patrick Daya
I change the statement

c.AVTRRT__Source__c = 'Other';

to

c.AVTRRT__Source__c = ( ETCmap.containsKey(c.Id).AVTRRT__Name__c && ETCmap.get(c.Id).AVTRRT__Name__c ! =null ) ? ETCmap.get(c.Id).AVTRRT__Name__c : 'Other';

as you have suggested but I received this error

Error: Compile Error: expecting a right parentheses, found '!' at line 71 column 116
Derek Patrick DayaDerek Patrick Daya
Hi Thank you for your help!

Figured it out. For some reason I do not understand why it does not recognize the '' on the if statments.

I changed the 

if(c.AVTRRT__Source__c == '' to if(c.AVTRRT__Source__c == null

it worked as expected.
Derek Patrick DayaDerek Patrick Daya
BTW Shashikant,

Can you help me with why does my Test Class receives an error that says "Error: System.NullPointerException: Attempt to de-reference a null object"

@IsTest
public class UpdateSource {

    static testmethod void updateUpdateSource(){

        AVTRRT__ETCObject__c a = new AVTRRT__ETCObject__c();
            a.AVTRRT__Name__c = 'jobsDB';
            a.AVTRRT__Candidate__r.Id = '003L000000QeGAc';
        insert a;
       
        AVTRRT__ETCObject__c b = [Select id from AVTRRT__ETCObject__c where AVTRRT__Name__c = 'JobsDB' and AVTRRT__Candidate__r.Id = '003L000000QeGAc'];
        update b;
       
}
}