You need to sign in to do that
Don't have an account?
Kai Cheung
Trigger CreateContactRole on Opportunity
Hi Newbie here,
In my Opportunities Tab, I have a lookup field called "relationship manager". It looks up Contact for the relationship manager.
My goal is to write a trigger that when create or update an Opportunity and input the Relationship manager fields, it automatically create a Contact Role for the same person.
This is what i have so far, I dont know how to extract Opp ID and Contact ID as I think I will need it. Please help.
Trigger CreateContactRole on Opportunity (after insert, after update)
{
For (Opportunity N:Trigger.now)
Opportunitycontactrole A = New Opportunitycontactrole ();
A.oppid = n.oppid;
a.RMID = n.rmid;
a.Role = ‘Relationship Manager’;
a.Isprimary = ‘True’;
}
In my Opportunities Tab, I have a lookup field called "relationship manager". It looks up Contact for the relationship manager.
My goal is to write a trigger that when create or update an Opportunity and input the Relationship manager fields, it automatically create a Contact Role for the same person.
This is what i have so far, I dont know how to extract Opp ID and Contact ID as I think I will need it. Please help.
Trigger CreateContactRole on Opportunity (after insert, after update)
{
For (Opportunity N:Trigger.now)
Opportunitycontactrole A = New Opportunitycontactrole ();
A.oppid = n.oppid;
a.RMID = n.rmid;
a.Role = ‘Relationship Manager’;
a.Isprimary = ‘True’;
}
Trigger CreateContactRole on Opportunity (after insert, after update)
{For (Opportunity N:Trigger.now)
Opportunitycontactrole A = New Opportunitycontactrole ();
A.OpportunityId = N.id;
A.ContactId = THE CONTACT OJBECT ID NUMBER SUCH THAT IT MAtches the field's NAme in Opportunity;
A.Role = ‘Relationship Manager’;
A.IsPrimary = ‘True’;
}
Your logic is correct but some condition we need to add on trigger logic , so that it will not execute if the entry criteria will not match.
And another thing is always we need to make trigger as bulkify to overcome salesforce governer limits.
I follow you and Im so happy you responded, the A.contactid requires a string of numbers right? instead of the N.relationship_Manager__C which is a name? Isnt what I want the ContactID number that matches the name on my opportunity field "relationship manager"???
You might get confused if it is a ID , how it displays name of the contact in UI instead of ID.
Let me clarify you confusion , whenever you will create a look up / master detail relationship with and object, while creating record on child , internally salesforce will store the parent ID of parent record on relationship filed but in UI the name of the parent record will visible.
So far your requirement ,
You might create contact as a look up on opportunity (filed : relationship manager)
so when ever you will create opportunity , by selecting contact , it will view the contact name instead of ID while creating record on opportunity, but if you quiery this filed based on record Id , it will return the contactID instead of Contact Name.
So please execute the above trigger , autometicly you will undestand what i mean to say !!!!
if you have any confusion , please let me know.
THanks!!!!!!!!!!!!!! With more practice ill get better.
Thanks so much
It works fine however, everytime I update, it creates another one of the contactrole. How do I fix that?
Sorry for the logic mistake . As i have not restrict the trigger , it was firing in both the cases(insert and update) and creating duplicate records.
I have modified my logic , Please find below.
I think my problem is i don tknow how to write whats in my mind.
For example Old_ , trigger.oldmap.get() is something i did not know or what it is commanding?
Also Object K = new object() other than 'new' is there something else i can command?
is there a place I can read to get all these commands???
thanks so much again.
I think it wont work because there is no old ID when and if we create a new opportunity. I think it will break.
Review all error messages below to correct your data.
Apex trigger CreateContactRole caused an unexpected exception, contact your administrator: CreateContactRole: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CreateContactRole: line 8, column 1
I worked around it by creating 2 trigger. One is after insert the second after update.
First one is the orginial code
Second one is the revised code
the combination works.
What do you think?
For this reason we are not getting any error while updating and as it returning null values in insert, so it is throwing exception while inserting records to database .
To overcome this exception , we need to use trigger context variables like "isinsert" and "isupdate" , which will differentiate between insert and update.
Salesforce has recommended one trigger per object, if there is complex logic which will not be fulfilled by single trigger , then only we should go for two triggers.
This scenario can be achieved by single trigger as well.