You need to sign in to do that
Don't have an account?
Rogerio Lara 2028
Trigger on Opportunity to enforce related object
Hi developers,
I was wondering if anyone could save me today! Pleassseeee! ;-)
I have the following trigger and a validation rule that helps to enforce contact roles to be created for the opportunities depending on the sales stage and amount of the opportunity. This is working well.
------------------------------------------------------------------------------------------ This is what I have - FYI ---------------------------------------------------------------------------------
trigger updatecontactrolecount on Opportunity (before insert, before update)
{
Boolean isPrimary;
Integer iCount;
Map<String, Opportunity> oppty_con = new Map<String, Opportunity>();//check if the contact role is needed and add it to the oppty_con map
for (Integer i = 0; i < Trigger.new.size(); i++)
{
oppty_con.put(Trigger.new[i].id,
Trigger.new[i]);
}
isPrimary = False;
for (List<OpportunityContactRole> oppcntctrle :[select OpportunityId from OpportunityContactRole where (OpportunityContactRole.IsPrimary = True and OpportunityContactRole.OpportunityId in :oppty_con.keySet())])
{
if (oppcntctrle .Size() >0)
{
isPrimary = True;
}
}
iCount = 0;
for (List<OpportunityContactRole> oppcntctrle2 : [select OpportunityId from OpportunityContactRole where (OpportunityContactRole.OpportunityId in :oppty_con.keySet())])//Query for Contact Roles
{
if (oppcntctrle2 .Size()>0)
{
iCount= oppcntctrle2 .Size();
}
}
for (Opportunity Oppty : system.trigger.new) //Check if roles exist in the map or contact role isn't required
{
Oppty.Number_of_Contacts_Roles_Assigned__c = iCount;
Oppty.Primary_Contact_Assigned__c =isPrimary;
}
}
Validation rule:
AND(OR(ISPICKVAL(StageName, 'Advanced Proposal'), ISPICKVAL (StageName, 'Final Negotiation'), ISPICKVAL (StageName, 'Contract Signed')), Number_of_Contacts_Roles_Assigned__c <2, Amount >= 1000000)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Now this is what I need:
Standard Object = Opportunity
Custom Object = Strategic Client Opportunity Plan
I created a read only checkbox field on the opportunity = Has_Strategic_Client_Opportunity_Plan__c and left it unchecked
I want the trigger to check the "Has_Strategic_Client_Opportunity_Plan__c" when a new Strategic Client Opportunity Plan is created.
Then I will create a validation rule on the opportunity to check whether this field is checked and the other required values too.
Does this make sense?
Your help is really appreciated. ;-)
Thank you,
Rog
I was wondering if anyone could save me today! Pleassseeee! ;-)
I have the following trigger and a validation rule that helps to enforce contact roles to be created for the opportunities depending on the sales stage and amount of the opportunity. This is working well.
------------------------------------------------------------------------------------------ This is what I have - FYI ---------------------------------------------------------------------------------
trigger updatecontactrolecount on Opportunity (before insert, before update)
{
Boolean isPrimary;
Integer iCount;
Map<String, Opportunity> oppty_con = new Map<String, Opportunity>();//check if the contact role is needed and add it to the oppty_con map
for (Integer i = 0; i < Trigger.new.size(); i++)
{
oppty_con.put(Trigger.new[i].id,
Trigger.new[i]);
}
isPrimary = False;
for (List<OpportunityContactRole> oppcntctrle :[select OpportunityId from OpportunityContactRole where (OpportunityContactRole.IsPrimary = True and OpportunityContactRole.OpportunityId in :oppty_con.keySet())])
{
if (oppcntctrle .Size() >0)
{
isPrimary = True;
}
}
iCount = 0;
for (List<OpportunityContactRole> oppcntctrle2 : [select OpportunityId from OpportunityContactRole where (OpportunityContactRole.OpportunityId in :oppty_con.keySet())])//Query for Contact Roles
{
if (oppcntctrle2 .Size()>0)
{
iCount= oppcntctrle2 .Size();
}
}
for (Opportunity Oppty : system.trigger.new) //Check if roles exist in the map or contact role isn't required
{
Oppty.Number_of_Contacts_Roles_Assigned__c = iCount;
Oppty.Primary_Contact_Assigned__c =isPrimary;
}
}
Validation rule:
AND(OR(ISPICKVAL(StageName, 'Advanced Proposal'), ISPICKVAL (StageName, 'Final Negotiation'), ISPICKVAL (StageName, 'Contract Signed')), Number_of_Contacts_Roles_Assigned__c <2, Amount >= 1000000)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Now this is what I need:
Standard Object = Opportunity
Custom Object = Strategic Client Opportunity Plan
I created a read only checkbox field on the opportunity = Has_Strategic_Client_Opportunity_Plan__c and left it unchecked
I want the trigger to check the "Has_Strategic_Client_Opportunity_Plan__c" when a new Strategic Client Opportunity Plan is created.
Then I will create a validation rule on the opportunity to check whether this field is checked and the other required values too.
Does this make sense?
Your help is really appreciated. ;-)
Thank you,
Rog
All Answers
I just want to tell you that please bulkify your trigger. What I mean is you should assume that in Bulk, Opportunities are going to be inserted/updated (say at a time 50 records inserted/update via dataloader or any apex code[say List<Opp> with 50 items added is inserted/updated]). So you should write a code to handle bulk data. And, that's the best practice. I will modify updatecontactrolecount trigger to bulkify it and post here.
Let's imagine a scenario,
Opp1 is having 2 OCR ( OCR1, OCR2) and now you are updating Opp1, so Opp1 before update trigger fires and will have count = 2.
Now you will insert, OCR3 on Opp1, will your Opp trigger fires?No, because you are inserting OCR not updating Opp1. So ideally trigger should be on OCR3 to update parent Opp1 with count 3.
When you insert OCR3 on Opp1, if you Opp1 is updating properly, then I believe there must be another trigger which is updating parent Opp or workflow/process builder is in place to update parent! By this, updatecontactrolecount trigger may be running internally and setting the count.
Please let me know if I am making sense..
Thank you so much. This is really incredible! Really appreaciate your kind help.
Rog
Sorry to bother you again. One last question. I've been trying to do this for the past week, but I'm not getting anywhere...
How do I combine the 2 visualforce pages for related objects, so to display on the same page layout, edit and save.
I'm trying to creat an edit visualforce page, so that when I create a new SCOP, I will, on the same page layout,be able to also create and save buying influence 1,2,3 etc. I know that I will have to override the 'new' button. for the SCOP for the new visualforce page.
<apex:page standardcontroller="Opportunity_Positioning__c">
<apex:messages />
<apex:sectionheader title="{!$ObjectType.Opportunity_Positioning__c.label} Edit" subtitle="{!IF(ISNULL(Opportunity_Positioning__c.Name), 'New Strategic Client Opportunity Plan',Opportunity_Positioning__c.Name)}"/>
<apex:form >
<apex:pageblock mode="edit" title="{!$ObjectType.Opportunity_Positioning__c.label} Edit">
<apex:pageblockbuttons >
<apex:commandbutton value="Save" action="{!Save}"/>
<apex:commandbutton value="Cancel" action="{!Cancel}"/>
</apex:pageblockbuttons>
<!-- ********** [Record Type : Master ] ********** -->
<apex:outputpanel >
<apex:pageblocksection title="Information" showheader="true" columns="2">
<apex:inputfield value="{!Opportunity_Positioning__c.Name}" required="true"/>
<apex:pageblocksectionitem />
<apex:inputfield value="{!Opportunity_Positioning__c.Opportunity__c}" required="true"/>
<apex:pageblocksectionitem />
</apex:pageblocksection>
<apex:pageblocksection title="Summary of my position today" showheader="true" columns="2">
<apex:inputfield value="{!Opportunity_Positioning__c.Current_Status__c}" required="false"/>
<apex:inputfield value="{!Opportunity_Positioning__c.Key_Actions_Required__c}" required="false"/>
<apex:inputfield value="{!Opportunity_Positioning__c.Strengths__c}" required="false"/>
<apex:inputfield value="{!Opportunity_Positioning__c.Red_Flags__c}" required="false"/>
<apex:pageblocksectionitem />
<apex:inputfield value="{!Opportunity_Positioning__c.Adequacy_of_Current_Position__c}" required="false"/>
</apex:pageblocksection>
<apex:pageblocksection title="Competitive Positioning" showheader="true" columns="2">
<apex:inputfield value="{!Opportunity_Positioning__c.Position_vs_c__c}" required="false"/>
<apex:inputfield value="{!Opportunity_Positioning__c.Primary_Competitor__c}" required="false"/>
</apex:pageblocksection>
</apex:outputpanel>
</apex:pageblock>
</apex:form>
</apex:page>
and
apex:page standardcontroller="Buying_Influence__c">
<apex:messages />
<apex:sectionheader title="{!$ObjectType.Buying_Influence__c.label} Edit" subtitle="{!IF(ISNULL(Buying_Influence__c.Name), 'New Buying Influence',Buying_Influence__c.Name)}"/>
<apex:form >
<apex:pageblock mode="edit" title="{!$ObjectType.Buying_Influence__c.label} Edit">
<apex:pageblockbuttons >
<apex:commandbutton value="Save" action="{!Save}"/>
<apex:commandbutton value="Cancel" action="{!Cancel}"/>
</apex:pageblockbuttons>
<!-- ********** [Record Type : Master ] ********** -->
<apex:outputpanel >
<apex:pageblocksection title="Information" showheader="true" columns="2">
<apex:inputfield value="{!Buying_Influence__c.Name}" required="true"/>
<apex:inputfield value="{!Buying_Influence__c.SCOP__c}" required="true"/>
<apex:inputfield value="{!Buying_Influence__c.Name__c}" required="false"/>
<apex:pageblocksectionitem />
<apex:inputfield value="{!Buying_Influence__c.Buying_influence_role__c}" required="false"/>
<apex:pageblocksectionitem />
<apex:inputfield value="{!Buying_Influence__c.Degree_of_Influence__c}" required="true"/>
<apex:pageblocksectionitem />
<apex:inputfield value="{!Buying_Influence__c.Mode__c}" required="true"/>
<apex:pageblocksectionitem />
</apex:pageblocksection>
<apex:pageblocksection title="How well is based covered for this contact" showheader="true" columns="1">
<apex:inputfield value="{!Buying_Influence__c.Rating_for_base_covered__c}" required="true"/>
<apex:inputfield value="{!Buying_Influence__c.Evidence_to_support_your_rating__c}" required="false"/>
</apex:pageblocksection>
</apex:outputpanel>
</apex:pageblock>
</apex:form>
</apex:page>
Sorry, due to work load I din't visit dev forum.
As per https://dfc-org-production.force.com/forums/ForumsMain?id=9060G000000XcUKQA0 , seems above problem solved.
Cheers! Happy coding.
Not Sure how effectively this will work or any error..Without trying in personal org, its bit difficult to trace if any mistakes in my code. STill you can give a try.
by using immediate try, not sure whether assignTo rowNum will work or not. On removeFromList, I have kept debug, check whether removeIndex is set properly or not.
I'm getting this message now:
Error: NewBuyingInfluence line 58, column 3: The element type "apex:pageblocksection" must be terminated by the matching end-tag "</apex:pageblocksection>"
Error: The element type "apex:pageblocksection" must be terminated by the matching end-tag "</apex:pageblocksection>".
Error: Compile Error: No such column 'Opportunity_Positioning__c' on entity 'Buying_Influence__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 16 column 21
I think I will leave this visualforce pages for now due to the little time I have to find a quick solution that will help the end users.
I need to create a child record when a new parent record is created.
I am trying to create an Apex trigger that creates or forces a child record to be created when a new parent record is created, but unfortunately isn't working :-(
I'm getting this error message: Error: Compile Error: Expression cannot be assigned at line -1 column -1
As always, thank you for your time.
Please try this code and let me know if any issues.
Error: Compile Error: No such column 'Opportunity_Positioning__c' on entity 'Buying_Influence__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 14 column 21
Error: Compile Error: duplicate field selected: SCOP__c at line 14 column 21
I can't see it. :-/
Error: Compile Error: Invalid constructor name: EditableBuyingInfluenceListExtension at line 7 column 12
Error: Compile Error: Invalid constructor name: EditableBuyingInfluenceListExtension at line 7 column 12
When I click the Button:
Id value 00626000004aBMr is not valid for the Opportunity_Positioning__c standard controller
Then, if I remove the id value from the URL then it takes me to the page above. So then If I click the Add Buying Influenc the I get another message.
Rerender is not currently supported with rich text editing enabled
Have a great weekend!
Best,
Rog