You need to sign in to do that
Don't have an account?

Simple Apex Trigger: Update a contact field when idea posted
I'm looking to have a trigger update a checkbox on a contact page whenever an idea is posted to the idea section. I've written the trigger below but I'm missing something.
I think I've been able to query the ID of the user logged in (saved as uid). I need a way to say update the field for the contact with the same ID (I'm assuming the contact ID will be the same as the user ID?)
This is my first attempt at Apex coding.
trigger UpdateContact on Idea (after insert, after update){ for (Idea newIdea : Trigger.new){ //Gets the user id who posted the idea id uid = UserInfo.getUserId(); //Need code to set the PortalUse_IdeaRaised__c field to true (this is a checkbox) //Something along the lines of Contact.PortalUse_IdeaRaised__c = true; } }
Hi Alainstic,
Please try below :-
trigger UpdateContact on Idea (after insert, after update){
//Gets the contactId related to user id who posted the idea
User uid = [select ContactId from User where id=:UserInfo.getUserID()];
//Need code to set the PortalUse_IdeaRaised__c field to true (this is a checkbox)
Contact con = [select PortalUse_IdeaRaised__c from Contact where id=:uid.ContactId];
for (Idea newIdea : Trigger.new){
con.PortalUse_IdeaRaised__c =true;
}
update con;
}
All Answers
We have a contact and we enable them as a user. Would the contact and the users have different ID's?
They are customer portal users. I have a formula lookup field on the contact page that shows their userID and their community nickname.
Hi Alainstic,
Please try below :-
trigger UpdateContact on Idea (after insert, after update){
//Gets the contactId related to user id who posted the idea
User uid = [select ContactId from User where id=:UserInfo.getUserID()];
//Need code to set the PortalUse_IdeaRaised__c field to true (this is a checkbox)
Contact con = [select PortalUse_IdeaRaised__c from Contact where id=:uid.ContactId];
for (Idea newIdea : Trigger.new){
con.PortalUse_IdeaRaised__c =true;
}
update con;
}
I've had some problems with this and so I've had a bit of a re-think. I'm going to update the User object field instead of the contact one. I've adapted the code as shown below.
During save I get the following error:
Compile Error: Illegal assignment from String to SOBJECT:User at line 4 column 1
It refers to my User uid = UserInfo.getUserID(); line. Is this the right delcaration to get the user object to update? Essentially I want the user RaisedIdea checkbox to be checked when an idea is raised.
Do you have any advice?
Chage your code from
User uid = UserInfo.getUserID();
to
User uid =[select RaisedIdea__c from User where id=:UserInfo.getUserID()];
Brilliant, thats sorted my issues. :D
Thanks for the assistance.
Happy to help :)
i've updated my code to use a number field instead, incrementing it each time an idea is raised:
I've then tested this manually by creating ideas and watching the number field increment.
I'm having a bit of trouble with writing a test class so I can then deploy this to our production environment. Can you offer any advice? Would I need to write a test that adds an Idea and provides a userID? I've got the basic structure:
Try below code :-
@isTest(SeeAllData=true)
private class UpdateUserIdeaTestClass {
static testMethod void validateIdeaUpdate() {
Community com =[select id from Community where name='<some name of Community in your org>'];
Idea id = new Idea(CommunityId=com.id,Title='Test Idea');
insert id;
}
}
Thanks for the suggestion, that gives me an error that CommunityId and Title do not exist. Should they be declared somewhere?
I think I've actually fixed that problem (I had to specify the fields as being idea.fieldname) but now I get a new error:
Expression cannot be assigned at line -1 column -1
Is that a common error message? Would be be caused if my idea has fields set as mandatory that are not being populated during this test?
My code is below:
I've managed to figure out what I was doing wrong with my test class. I've achieved an 83% pass rate and I've deployed to production.
Thanks for all the help. :)
Good to know,it worked :)