+ Start a Discussion
Nuno.CarvalhoNuno.Carvalho 

Hello guys im new to apex triggers.

Hello guys im new to apex triggers.
I have 2 objects, standard (Contract) and costum (soccerplayers__c).
I want to create a trigger that updates Contracts start date in soccerplayers__c based on the value i puted in Contracts.
I am trying to compare the different contracts based on the players name, i have a lookup between the two objects.
The Name field on contract is (player_represented_by__c) and on soccerplayers__c is (Name). Ps: this is the name of the lookup fields.
If i didn´t express myself right you can ask me, i will try to answer.

 
Best Answer chosen by Nuno.Carvalho
Bharat.SharmaBharat.Sharma
Hi Carvalho,
please have a look i have tried to impliment your scenario.
Trigger name is soccerplayers_update_startdate and after insert this trigger will fire.
trigger soccerplayers_update_startdate on Contract (after insert) {    
    string ids;
    list <soccerplayers__c> SP = [Select id,name,Contract_Start_Date__c from soccerplayers__c];
    for(Contract newItem : trigger.new) {
       ids =newItem.id;
    }
    Contract Con = [Select id,player_represented_by__c,StartDate From Contract Where Id = : ids];
    if(Con.player_represented_by__c !=null)
    {
   soccerplayers__c  c = [Select name from soccerplayers__c where id =:con.player_represented_by__c  limit 1];
    for (soccerplayers__c plr : SP){
        if(plr.name==c.name ){
            plr.Contract_Start_Date__c = Con.StartDate;
            update plr;
        }
    }
    }
}

Please Mark this as a Best Answer if this resolve your problem.
Thank you !

All Answers

Bharat.SharmaBharat.Sharma
hello Carvalho,
can you please describe briefly about this scenario. i got confused in (I am trying to compare the different contracts based on the players name).
which object do you want to update Contract or soccerplayers__c??
 
Nuno.CarvalhoNuno.Carvalho
Hi Sharma,
I want to update a Date field (Start_date_contract) on soccerplayers__c object with the same value that is in Contracts object date field (start_date).
The criteria i want to put to give the right contract to the right player is comparing the name.
So if it´s equal the trigger gives the right start_date.
Did you understand? sry if my english is not the best.
Maharajan CMaharajan C
Hi Carvalho

Can you please confirm this:

The Player name in contract is text field or lookup to Soccerplayers__c?

Thanks,
Raj
Bharat.SharmaBharat.Sharma
Hi Carvalho,
please have a look i have tried to impliment your scenario.
Trigger name is soccerplayers_update_startdate and after insert this trigger will fire.
trigger soccerplayers_update_startdate on Contract (after insert) {    
    string ids;
    list <soccerplayers__c> SP = [Select id,name,Contract_Start_Date__c from soccerplayers__c];
    for(Contract newItem : trigger.new) {
       ids =newItem.id;
    }
    Contract Con = [Select id,player_represented_by__c,StartDate From Contract Where Id = : ids];
    if(Con.player_represented_by__c !=null)
    {
   soccerplayers__c  c = [Select name from soccerplayers__c where id =:con.player_represented_by__c  limit 1];
    for (soccerplayers__c plr : SP){
        if(plr.name==c.name ){
            plr.Contract_Start_Date__c = Con.StartDate;
            update plr;
        }
    }
    }
}

Please Mark this as a Best Answer if this resolve your problem.
Thank you !
This was selected as the best answer
Nuno.CarvalhoNuno.Carvalho
Hi Maharaja,
The player name in contract is a lookup.
Thank you.
Nuno.CarvalhoNuno.Carvalho
Hi Sharma,
Just wanted to say thank you, that worked perfectly.
Gopal R 9Gopal R 9
I am using "Opportunity" as standard controller & want the output as PDF & i am able to get this. But in addition to this i want to add few fields from another related custom object "Guarantor__c" & the field to be added are "Primary_Account__c" , "Personal_Gurantor__c". Please some one help on this.

<apex:page standardController="Opportunity" extensions="OpptyGenPDFExtension" renderAs="pdf">
    <table width="100%" style="font-family: verdana; border-collapse: collapse;"
        cellpadding="5">
        <tr>
            <td colspan="8">
                Opportunity
            </td>
        </tr>
        <tr>
            <td colspan="8" style="font-size: 4px; font-weight: bold">
                {! Opportunity.Name}
            </td>
        </tr>
        <tr>
            <td colspan="8">
            </td>
        </tr>
        <tr>
            <td colspan="8" style="font-weight: bold; border-bottom-style: solid; border-bottom-width: thick;
                border-bottom-color: #ddb929;">
                Applicant's Details
            </td>
        </tr>
        <tr>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Customer Name
            </td>
            <td style="font-weight: bold">
                {! Opportunity.AccountId}
            </td>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Loan Application Name
            </td>
            <td style="font-weight: bold">
                  {! Opportunity.Name}
            </td>
        </tr>
        <tr>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
               Branch Name
            </td>
            <td style="font-weight: bold">
               {! Opportunity.Branch_Name__c}
            </td>
              <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                CSP Name
            </td>
            <td style="font-weight: bold">
                {! Opportunity.OwnerId}
            </td>
            <tr>
             <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                 Loan Application Type
            </td>
            <td style="font-weight: bold">
                {! Opportunity.RecordTypeId}
            </td>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                 Registration Type
            </td>
            <td style="font-weight: bold">
               {! Opportunity.Registration__c}
            </td>
            </tr>
              <tr>
             <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                 Customer Type
            </td>
            <td style="font-weight: bold">
                {! Opportunity.Type_of_Applicant__c}
            </td>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                 Track Record
            </td>
            <td style="font-weight: bold">
               {! Opportunity.Track_Record__c}
            </td>
            </tr>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
               Lead Source
            </td>
            <td style="font-weight: bold">
                {! Opportunity.LeadSource}
            </td>
                   <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Stage
            </td>
            <td style="font-weight: bold">
              {! Opportunity.StageName}
            </td>
             </tr>
             <tr>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Sub-Status Name
            </td>
            <td style="font-weight: bold">
               {! Opportunity.Sub_Status__c}
            </td>
             </tr>
                <tr>
            <td colspan="4">
                &nbsp;
            </td>
        </tr>
        <tr>
        <apex:outputField value="{opportunity.Primary_Account__c}"/>
        </tr>
        <tr>
            <td colspan="4" style="font-weight: bold; border-bottom-style: solid; border-bottom-width: thick;
                border-bottom-color: #ddb929;">
                Proposed Asset Information
            </td>
        </tr>
        <tr>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Proposed Asset
            </td>
            <td style="font-weight: bold">
                {! Opportunity.Advance_Amount_Proposed_asset__c}
            </td>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Model
            </td>
            <td style="font-weight: bold">
                {! Opportunity.Year_of_Purchase__c}
            </td>
        </tr>
        <tr>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Registration Number
            </td>
            <td style="font-weight: bold">
                {! Opportunity.Registration_Numb__c}
            </td>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Machine Serial No        
            </td>
            <td style="font-weight: bold">
                {! Opportunity.Machine_Serial_No__c}
            </td>
            </tr>
            <tr>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Advance Amount
            </td>
            <td style="font-weight: bold">
                {! Opportunity.Total_Loan_Amount_Applied__c}
            </td>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Approved Amount
                </td>
            <td style="font-weight: bold">
                {! Opportunity.Total_Loan_Amount_Approved__c}
             </td>
            </tr>
            <tr>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Market Value
            </td>
            <td style="font-weight: bold">
                {! Opportunity.Total_Amount__c}
            </td>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                IDV Amount
                </td>
            <td style="font-weight: bold">
                {! Opportunity.IDV_Amount__c}
             </td>
            </tr>
               <tr>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Asset Deployment Segment
            </td>
            <td style="font-weight: bold">
                {! Opportunity.Asset_Deployment_Segment__c}
            </td>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                End Use of Application
                </td>
            <td style="font-weight: bold">
                {! Opportunity.End_Use_of_Application__c}
             </td>
            </tr>
            <tr>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                End Use of Fund
            </td>
            <td style="font-weight: bold">
                {! Opportunity.End_Use_of_Fund__c}
            </td>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Asset Deployment
                </td>
            <td style="font-weight: bold">
                {! Opportunity.Asset_Deployment__c}
             </td>
            </tr>
            <tr>
            <td colspan="4" style="font-weight: bold; border-bottom-style: solid; border-bottom-width: thick;
                border-bottom-color: #ddb929;">
                Loan Information
            </td>
        </tr>
        <tr>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Total Loan Amount Applied
            </td>
            <td style="font-weight: bold">
                {! Opportunity.Total_Loan_Amount_Applied__c}
            </td>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Scheme Name
            </td>
            <td style="font-weight: bold">
                {! Opportunity.Scheme_Name__c}
            </td>
        </tr>
        <tr>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Eligible LTV %
            </td>
            <td style="font-weight: bold">
                {! Opportunity.Loan_to_Value__c}
            </td>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Eligible LTV Amount
            </td>
            <td style="font-weight: bold">
                {! Opportunity.Loan_to_Value_Amount__c}
            </td>
        </tr>
        <tr>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
              Required IRR %
            </td>
            <td style="font-weight: bold">
                {! Opportunity.IRR__c}
            </td>
            <td align="right" style="color: #808080; border-right-style: solid; border-right-width: thin;
                border-right-color: #ddb929;">
                Quoted IRR %
            </td>
            <td style="font-weight: bold">
                {! Opportunity.Quoted_IRR__c}
            </td>
        </tr>
    </table>
    <br/><br/>
    <apex:outputPanel rendered="{!Tasks != NULL}">
        <table width="100%" style="font-family: Verdana; border-collapse: collapse;"
            cellpadding="5">
            <tr>
                <td colspan="6" style="font-weight: bold; border-bottom-style: solid; border-bottom-width: thick;
                    border-bottom-color: #ddb929;">
                    Tasks
                </td>
            </tr>
            <tr>
                <td style="font-weight: bold; background-color: #FFFFCC;">
                    Subject
                </td>
                <td style="font-weight: bold; background-color: #FFFFCC;">
                    Name
                </td>
                <td style="font-weight: bold; background-color: #FFFFCC;">
                    Due Date
                </td>
                <td style="font-weight: bold; background-color: #FFFFCC;">
                    Status
                </td>
                <td style="font-weight: bold; background-color: #FFFFCC;">
                    Priority
                </td>
                <td style="font-weight: bold; background-color: #FFFFCC;">
                    Assigned To
                </td>
            </tr>
            <apex:repeat value="{!Tasks}" var="task">
                <tr id="{!task.Id}">
                    <td>
                        {!task.Subject}
                    </td>
                    <td>
                        {!task.Who.Name}
                    </td>
                    <td>
                        <apex:outputText value="{0,date,MM'/'dd'/'yyyy}">
                            <apex:param value="{!task.ActivityDate}" /> 
                        </apex:outputText>
                    </td>
                    <td>
                        {!task.Status}
                    </td>
                    <td>
                        {!task.Priority}
                    </td>
                    <td>
                        {!task.Owner.Name}
                    </td>                
                </tr>            
            </apex:repeat>
        </table>    
    </apex:outputPanel>
</apex:page>