function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AlanisticAlanistic 

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;
}

}

 

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

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

jbroquistjbroquist
How are your Users related to your Contacts? Custom lookup on the Contact?
AlanisticAlanistic

We have a contact and we enable them as a user.  Would the contact and the users have different ID's?

jbroquistjbroquist
So they are partner portal users then?
AlanisticAlanistic

They are customer portal users.  I have a formula lookup field on the contact page that shows their userID and their community nickname.

Vinit_KumarVinit_Kumar

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;

}

This was selected as the best answer
AlanisticAlanistic

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. 

 

trigger UpdateUser on Idea (after insert, after update){

//Gets the user id who posted the idea
User User uid = UserInfo.getUserID(); for (Idea newIdea:Trigger.new){ //Set raised idea to true uid.RaisedIdea__c =true; } // Update user account update uid; }

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?

Vinit_KumarVinit_Kumar

Chage your code from

 

User uid = UserInfo.getUserID();

 

to 

 

User uid =[select  RaisedIdea__c from User where id=:UserInfo.getUserID()];

AlanisticAlanistic

Brilliant, thats sorted my issues.  :D

 

Thanks for the assistance.

Vinit_KumarVinit_Kumar

Happy to help :)

AlanisticAlanistic

i've updated my code to use a number field instead, incrementing it each time an idea is raised:

 

trigger UpdateUserIdea on Idea (after insert){

//Gets the user id who posted the idea
User uid =[select Number_of_Ideas_Raised__c from User where id=:UserInfo.getUserID()];

for (Idea newIdea:Trigger.new){

// If number of replies asked field is null, set it to 1
if(uid.Number_of_Ideas_Raised__c == null)
uid.Number_of_Ideas_Raised__c = 1;

// If number of questions asked field is not null, increment by 1
else
uid.Number_of_Ideas_Raised__c ++;

}

// Update user account
update uid;
}

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:

 

@isTest
private class UpdateUserIdeaTestClass {
	static testMethod void validateIdeaUpdate() {
		

	}
}

 

 

 

 

Vinit_KumarVinit_Kumar

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;

}
}

AlanisticAlanistic

Thanks for the suggestion, that gives me an error that CommunityId and Title do not exist. Should they be declared somewhere?

AlanisticAlanistic

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:

 

//Define class as a test class
@isTest (SeeAllData=true)  //Makes all org data available to class
private class UpdateUserIdeaTestClass {
    static testMethod void validateIdeaUpdate() {
    
    //Gets community information
    Community com=[select id from Community where name = 'Community'];

    //Creates idea for testing
    Idea id1 = newIdea(idea.CommunityId=com.id,idea.Title='Test Idea',idea.Body='Test body');
    insert id1;   
    }
}

 

AlanisticAlanistic

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.  :)

Vinit_KumarVinit_Kumar

Good to know,it worked :)