+ Start a Discussion
ANITHA BEEMU 2ANITHA BEEMU 2 

need help for having existing addresses

HI all,
i have custom object called locations which is master detail relationship with opportunities,now i want a button (existing location)on the related list of locations in the opportunitypage.i tired below code:

apex code:
public with sharing class ExiLocExtController {

 

    public Opportunity opportunity  { get; set; }

     

    public ExiLocExtController() {

         

        String OpportunityId = ApexPages.currentPage().getParameters().get('id');

        Opportunity = [SELECT Id, Name, Location__c FROM Opportunity WHERE Id =: OpportunityId];

         

    }

     

    public PageReference save () {

         

        Location__c LOC = new Location__c ( Id = Opportunity.Location__c, Opportunity_Name__c = Opportunity.Id);

         

        try {

            Database.update(LOC);

        } catch (Exception error) {

            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error while associating.' + error.getMessage()));
       }

         

        PageReference page = new PageReference('/' + Opportunity.Id);

         

        return page.setRedirect(true);

    }

     

    public PageReference cancel () {

        PageReference page = new PageReference('/' + Opportunity.Id);

        return page.setRedirect(true);        

    }


}
visual page:

<apex:page controller="ExiLocExtController">
 <apex:form >

        <apex:pageblock title="ExistingLocationto test">

            <apex:pageMessages />

            <apex:pageblockbuttons location="top">

                <apex:commandbutton value="save" action="{!save}" />

                <apex:commandbutton value="Cancel" action="{!cancel}"/>

            </apex:pageblockbuttons>

            <apex:pageBlockSection >

                <apex:pageBlockSectionItem >

                    <apex:outputLabel >Opportunity Name</apex:outputLabel>

                    <apex:outputText >{!Opportunity.Name}</apex:outputText>  

                </apex:pageBlockSectionItem>

              <apex:pageBlockSectionItem >

                    <apex:outputLabel >Location__c</apex:outputLabel>

                    <apex:inputField value="{!Opportunity.Location__c}"/>

                </apex:pageBlockSectionItem>               

            </apex:pageBlockSection>

        </apex:pageblock>

    </apex:form>

</apex:page>

i am able to open the page,but that is not saving to the opportunity,kindly help me.
 
Raj VakatiRaj Vakati
Change you save method as below and can you give me debug logs what error you are getting
 
public PageReference save () {

         // Add all the required fields here 

        Location__c LOC = new Location__c ( Id = Opportunity.Location__c, Opportunity_Name__c = Opportunity.Id);

         

        try {

            Database.update(LOC);

        } catch (Exception error) {

            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error while associating.' + error.getMessage()));
       }

         

        PageReference page = new PageReference('/' + Opportunity.Id);

         

        return page.setRedirect(true);

    }

 
ANITHA BEEMU 2ANITHA BEEMU 2
Hi THank you so much for reply,i used the code your gave execept this <b>  // Add all the required fields here </b>,when i am using this  i am not able to save asking } is required at starting of public PageReference save () {,

without this i saved and tried saving the record same not able to save,pLEASE FIND THE DEBUG LOGS
43.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;NBA,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
11:45:43.0 (334780)|USER_INFO|[EXTERNAL]|00515000006lupt|tbelk@conterra.com.corlpoc|Eastern Standard Time|GMT-04:00
11:45:43.0 (364938)|EXECUTION_STARTED
11:45:43.0 (370324)|CODE_UNIT_STARTED|[EXTERNAL]|0661D000000B4fJ|VF: /apex/ExiLoc
11:45:43.4 (4047090)|SYSTEM_MODE_ENTER|true
11:45:43.4 (4530579)|SYSTEM_MODE_ENTER|true
11:45:43.4 (4594135)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:45:43.4 (4603635)|VARIABLE_SCOPE_BEGIN|[88]|this|ApexPage.PageMessagesComponentController|true|false
11:45:43.4 (4640777)|VARIABLE_ASSIGNMENT|[88]|this|{}|0x797e0974
11:45:43.4 (4647166)|VARIABLE_SCOPE_BEGIN|[88]|escape|Boolean|false|false
11:45:43.4 (4655533)|VARIABLE_ASSIGNMENT|[88]|escape|null
11:45:43.4 (4828129)|SYSTEM_MODE_ENTER|true
11:45:43.4 (4865879)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:45:43.4 (4872921)|VARIABLE_SCOPE_BEGIN|[2]|this|ApexPage.PageMessagesComponentController|true|false
11:45:43.4 (4891872)|VARIABLE_ASSIGNMENT|[2]|this|{}|0x797e0974
11:45:43.0 (7763275)|SYSTEM_MODE_ENTER|true
11:45:43.0 (7958797)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:92
11:45:43.0 (7967304)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:9
11:45:43.0 (7977136)|METHOD_ENTRY|[1]|01p1D000000oswq|ExiLocExtController.ExiLocExtController()
11:45:43.0 (7983706)|STATEMENT_EXECUTE|[1]
11:45:43.0 (7987618)|STATEMENT_EXECUTE|[1]
11:45:43.0 (7995140)|SYSTEM_MODE_ENTER|false
11:45:43.0 (8001736)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5
11:45:43.0 (8010631)|SYSTEM_MODE_EXIT|false
11:45:43.0 (8018475)|METHOD_EXIT|[1]|ExiLocExtController
11:45:43.0 (8031368)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:45:43.0 (8037364)|VARIABLE_SCOPE_BEGIN|[9]|this|ExiLocExtController|true|false
11:45:43.0 (8060392)|VARIABLE_ASSIGNMENT|[9]|this|{}|0x49b951da
11:45:43.0 (8071003)|SYSTEM_MODE_ENTER|false
11:45:43.0 (8075555)|HEAP_ALLOCATE|[13]|Bytes:5
11:45:43.0 (8079486)|STATEMENT_EXECUTE|[1]
11:45:43.0 (8082190)|HEAP_ALLOCATE|[13]|Bytes:5
11:45:43.0 (8084922)|STATEMENT_EXECUTE|[5]
11:45:43.0 (8100446)|STATEMENT_EXECUTE|[9]
11:45:43.0 (8102300)|STATEMENT_EXECUTE|[13]
11:45:43.0 (8164184)|HEAP_ALLOCATE|[13]|Bytes:33
11:45:43.0 (8211698)|HEAP_ALLOCATE|[13]|Bytes:17
11:45:43.0 (8233250)|HEAP_ALLOCATE|[13]|Bytes:2
11:45:43.0 (8251625)|VARIABLE_SCOPE_BEGIN|[13]|OpportunityId|String|false|false
11:45:43.0 (8266213)|VARIABLE_ASSIGNMENT|[13]|OpportunityId|"0061D000003wNyA"
11:45:43.0 (8270846)|STATEMENT_EXECUTE|[15]
11:45:43.0 (8273694)|HEAP_ALLOCATE|[15]|Bytes:65
11:45:43.0 (8284584)|HEAP_ALLOCATE|[15]|Bytes:4
11:45:43.0 (8294498)|HEAP_ALLOCATE|[15]|Bytes:7
11:45:43.0 (10066034)|SOQL_EXECUTE_BEGIN|[15]|Aggregations:0|SELECT Id, Name, Location__c FROM Opportunity WHERE Id = :tmpVar1
11:45:43.0 (14009631)|SOQL_EXECUTE_END|[15]|Rows:1
11:45:43.0 (14033587)|HEAP_ALLOCATE|[15]|Bytes:8
11:45:43.0 (14049113)|HEAP_ALLOCATE|[15]|Bytes:82
11:45:43.0 (14089828)|HEAP_ALLOCATE|[15]|Bytes:8
11:45:43.0 (14100586)|HEAP_ALLOCATE|[15]|Bytes:37
11:45:43.0 (14134123)|HEAP_ALLOCATE|[15]|Bytes:16
11:45:43.0 (14169366)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{}|0x49b951da
11:45:43.0 (14195807)|VARIABLE_ASSIGNMENT|[EXTERNAL]|value|{"Id":"0061D000003wNyAQAU","Name":"nc solution","RecordTypeId":"0121C000001JSd5QAG"}|0x242dee2b
11:45:43.0 (14214128)|VARIABLE_ASSIGNMENT|[5]|this.opportunity|{"Id":"0061D000003wNyAQAU","Name":"nc solution","RecordTypeId":"0121C000001JSd5QAG"}|0x49b951da
11:45:43.0 (14227112)|SYSTEM_MODE_EXIT|false
11:45:43.0 (14265966)|SYSTEM_MODE_ENTER|true
11:45:43.33 (33327882)|SYSTEM_MODE_ENTER|true
11:45:43.33 (33399757)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:45:43.33 (33409643)|VARIABLE_SCOPE_BEGIN|[88]|this|ApexPage.PageMessagesComponentController|true|false
11:45:43.33 (33436087)|VARIABLE_ASSIGNMENT|[88]|this|{}|0x797e0974
11:45:43.33 (33442826)|VARIABLE_SCOPE_BEGIN|[88]|escape|Boolean|false|false
11:45:43.33 (33451023)|VARIABLE_ASSIGNMENT|[88]|escape|null
11:45:43.33 (33560836)|SYSTEM_MODE_ENTER|true
11:45:43.33 (33596050)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:45:43.33 (33602316)|VARIABLE_SCOPE_BEGIN|[2]|this|ApexPage.PageMessagesComponentController|true|false
11:45:43.33 (33620035)|VARIABLE_ASSIGNMENT|[2]|this|{}|0x797e0974
11:45:43.0 (35636134)|VF_SERIALIZE_VIEWSTATE_BEGIN|0661D000000B4fJ
11:45:43.0 (37754504)|VF_SERIALIZE_VIEWSTATE_END
11:45:43.39 (39525146)|CUMULATIVE_LIMIT_USAGE
11:45:43.39 (39525146)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

11:45:43.39 (39525146)|CUMULATIVE_LIMIT_USAGE_END

11:45:43.0 (39556072)|CODE_UNIT_FINISHED|VF: /apex/ExiLoc
11:45:43.0 (40312692)|EXECUTION_FINISHED
 
43.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;NBA,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
11:45:50.0 (315295)|USER_INFO|[EXTERNAL]|00515000006lupt|tbelk@conterra.com.corlpoc|Eastern Standard Time|GMT-04:00
11:45:50.0 (344484)|EXECUTION_STARTED
11:45:50.0 (348172)|CODE_UNIT_STARTED|[EXTERNAL]|0661D000000B4fJ|VF: /apex/ExiLoc
11:45:50.0 (749797)|VF_DESERIALIZE_VIEWSTATE_BEGIN|0661D000000B4fJ
11:45:50.0 (6821423)|VF_DESERIALIZE_VIEWSTATE_END
11:45:50.7 (7337917)|SYSTEM_MODE_ENTER|true
11:45:50.8 (8423555)|SYSTEM_MODE_ENTER|true
11:45:50.8 (8480751)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:45:50.8 (8491813)|VARIABLE_SCOPE_BEGIN|[88]|this|ApexPage.PageMessagesComponentController|true|false
11:45:50.8 (8528762)|VARIABLE_ASSIGNMENT|[88]|this|{}|0x2cf597f6
11:45:50.8 (8536267)|VARIABLE_SCOPE_BEGIN|[88]|escape|Boolean|false|false
11:45:50.8 (8545561)|VARIABLE_ASSIGNMENT|[88]|escape|null
11:45:50.0 (8735728)|SYSTEM_MODE_ENTER|true
11:45:50.0 (8755616)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:92
11:45:50.0 (8762520)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:9
11:45:50.0 (8770648)|METHOD_ENTRY|[1]|01p1D000000oswq|ExiLocExtController.ExiLocExtController()
11:45:50.0 (8776435)|STATEMENT_EXECUTE|[1]
11:45:50.0 (8780262)|STATEMENT_EXECUTE|[1]
11:45:50.0 (8786909)|SYSTEM_MODE_ENTER|false
11:45:50.0 (8793676)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5
11:45:50.0 (8801939)|SYSTEM_MODE_EXIT|false
11:45:50.0 (8809580)|METHOD_EXIT|[1]|ExiLocExtController
11:45:50.18 (18636812)|SYSTEM_MODE_ENTER|true
11:45:50.18 (18694873)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:45:50.18 (18704469)|VARIABLE_SCOPE_BEGIN|[88]|this|ApexPage.PageMessagesComponentController|true|false
11:45:50.18 (18728077)|VARIABLE_ASSIGNMENT|[88]|this|{}|0x2cf597f6
11:45:50.18 (18735961)|VARIABLE_SCOPE_BEGIN|[88]|escape|Boolean|false|false
11:45:50.18 (18744242)|VARIABLE_ASSIGNMENT|[88]|escape|null
11:45:50.18 (18873016)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:-4
11:45:50.0 (19238247)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:45:50.0 (19249081)|VARIABLE_SCOPE_BEGIN|[21]|this|ExiLocExtController|true|false
11:45:50.0 (19279770)|VARIABLE_ASSIGNMENT|[21]|this|{"opportunity":"0x6b7fdf00"}|0x5e8eef4e
11:45:50.0 (19294327)|SYSTEM_MODE_ENTER|false
11:45:50.0 (19301915)|HEAP_ALLOCATE|[26]|Bytes:5
11:45:50.0 (19306946)|STATEMENT_EXECUTE|[22]
11:45:50.0 (19308955)|STATEMENT_EXECUTE|[26]
11:45:50.0 (19347014)|HEAP_ALLOCATE|[26]|Bytes:4
11:45:50.0 (19457346)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"opportunity":"0x6b7fdf00"}|0x5e8eef4e
11:45:50.0 (20153605)|VARIABLE_ASSIGNMENT|[26]|this.Id|"a0h1D000001AXVNQA4"|0x3b4b7032
11:45:50.0 (20187558)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"opportunity":"0x6b7fdf00"}|0x5e8eef4e
11:45:50.0 (20240571)|VARIABLE_ASSIGNMENT|[26]|this.Opportunity_Name__c|"0061D000003wNyAQAU"|0x3b4b7032
11:45:50.0 (20269033)|VARIABLE_SCOPE_BEGIN|[26]|LOC|Location__c|true|false
11:45:50.0 (20289253)|VARIABLE_ASSIGNMENT|[26]|LOC|{"Id":"a0h1D000001AXVNQA4","Opportunity_Name__c":"0061D000003wNyAQAU"}|0x3b4b7032
11:45:50.0 (20295887)|STATEMENT_EXECUTE|[28]
11:45:50.0 (20297612)|STATEMENT_EXECUTE|[28]
11:45:50.0 (20299148)|STATEMENT_EXECUTE|[30]
11:45:50.0 (20328587)|HEAP_ALLOCATE|[30]|Bytes:8
11:45:50.0 (20337044)|DML_BEGIN|[30]|Op:Update|Type:Location__c|Rows:1
11:45:50.0 (20369166)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:45:50.0 (52810390)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:01I1D0000002K0R
11:45:50.0 (65345396)|WF_RULE_EVAL_BEGIN|Workflow
11:45:50.0 (65365340)|WF_CRITERIA_BEGIN|[Location: A-0066 a0h1D000001AXVN]|existing_Locations_DEV3011D0000004pI8|01Q1D0000008s7h|ON_CREATE_ONLY|0
11:45:50.0 (65376241)|WF_RULE_NOT_EVALUATED
11:45:50.0 (65382770)|WF_SPOOL_ACTION_BEGIN|Workflow
11:45:50.0 (65389480)|WF_ACTION| None
11:45:50.0 (65393808)|WF_RULE_EVAL_END
11:45:50.0 (65451520)|WF_ACTIONS_END| None
11:45:50.0 (65459323)|CODE_UNIT_FINISHED|Workflow:01I1D0000002K0R
11:45:50.0 (69489338)|DML_END|[30]
11:45:50.0 (69523101)|STATEMENT_EXECUTE|[38]
11:45:50.0 (69541428)|HEAP_ALLOCATE|[38]|Bytes:1
11:45:50.0 (69631778)|VARIABLE_ASSIGNMENT|[EXTERNAL]|this|{"opportunity":"0x6b7fdf00"}|0x5e8eef4e
11:45:50.0 (69705889)|HEAP_ALLOCATE|[38]|Bytes:18
11:45:50.0 (69726738)|HEAP_ALLOCATE|[38]|Bytes:19
11:45:50.0 (69769353)|VARIABLE_SCOPE_BEGIN|[38]|page|System.PageReference|true|false
11:45:50.0 (69986157)|VARIABLE_ASSIGNMENT|[38]|page|"/0061D000003wNyAQAU"|0x7dd68dc0
11:45:50.0 (69995268)|STATEMENT_EXECUTE|[40]
11:45:50.0 (70057928)|HEAP_ALLOCATE|[40]|Bytes:23
11:45:50.0 (70074658)|SYSTEM_MODE_EXIT|false
11:45:50.74 (74606306)|CUMULATIVE_LIMIT_USAGE
11:45:50.74 (74606306)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

11:45:50.74 (74606306)|CUMULATIVE_LIMIT_USAGE_END

11:45:50.0 (74637771)|CODE_UNIT_FINISHED|VF: /apex/ExiLoc
11:45:50.0 (75751524)|EXECUTION_FINISHED

please do help me in this..thank you