how to update existing opportunity record using getBeans in Suitecrm.

Hi,

I want to update my Opportunity stage when my custom button clicked.
For that I created one custom button in Opportunity editview.
and created one controller.php file in /custom/modules/opportunity/
In that file controller.php i added some code.
controller.php

<?php
class OpportunitiesController extends SugarController {
    //Can now put actions here
    public function action_stagechange(){
                     $bean = BeanFactory::getBean('Opportunities');
                      if($bean->stage_c==""){
		            	$bean->stage_c="Lead";
	             	}
	                	elseif($bean->stage_c=="Lead"){
		 	     $bean->stage_c="New Business Opportunity";
		     }
		          elseif($bean->stage_c=="New Business Opportunity"){
			       $bean->stage_c="Device Considered";
		      }
		   elseif($bean->stage_c=="Device Considered"){
			if($bean->sample_requested_date_c==""){
			echo "Sample Request Date";
			
			}
			else{
				$bean->stage_c="Sample Requested";
			}
		}
		elseif($bean->stage_c=="Sample Requested"){
			$bean->stage_c="Sample Submitted";
		}
		elseif($bean->stage_c=="Sample Submitted"){
			$bean->stage_c="Design In";
		}
		elseif($bean->stage_c=="Design In"){
			$bean->stage_c="Design Win";
		}
		
	   $bean->save();
	die();
	
    }
}

ajax code:

<script>
	function phpload(){
$.ajax({
  url: "index.php?module=Opportunities&action=stagechange",
})
  .done(function( result ) {
    alert(result);
  });
	}
	
</script>

My Button code:

<input id="stagechange" class="button primary" type="submit" name="button" value="NextStep" onclick="phpload();" title="Save">

When I click my button controller creating new opportunity it is not updating existing record.
then I gave second parameter in the getBeans.

$bean = BeanFactory::getBean('Opportunities',$_POST['record']);

Then also it’s not working it’s creating new opportunity .

please tell me how to update my existing record when button clicked…?

The problem is that your stage change action isn’t being passed an id so it doesn’t know which record to use. You can grab the current record id in your phpload call:

var id = $('#formDetailView #record').val();

This can then be used in the url:

 url: "index.php?module=Opportunities&action=stagechange&record"=id,

Finally your controller action can get this value with $_REQUEST[ā€˜record’]

Hope this helps,
Jim

Hi there,

still record id is not passing…

<script>
	function phpload(){
		
		var id=$('#formDetailView #record').val();
$.ajax({
	
  url: "index.php?module=Opportunities&action=stagechange&record="id,
})
  .done(function( result ) {
    alert(result);
  });
	}
	
</script>

controller.php

$bean = BeanFactory::getBean('Opportunities',$_REQUEST['id']);

for testing I checked id of record like this

$ids = $_REQUEST['id'];
            echo $ids;

here I’m not getting any id alert. result of controller is displaying as alert.
please check the code and tell me if any mistakes in the code.

Apologies, there was a typo in my code. The URL should be:

url: "index.php?module=Opportunities&action=stagechange&record=" + id,

Bah, two typos actually. The js to get the id should be:

var id = $('#formDetailView input[name=record]').val();

still not working…
i’m getting id value is undefined…

var id=$('#formDetailView input[name=record]').val();
		alert(id);

Sorry, I misread your original post. That id code will work on Detail View, for the edit view you will need:

var id=$('#EditView input[name=record]').val();

now i’m getting id but that id is not passing to controller.php file

$_REQUEST['id'] 

is empty in controller.php


$ids = $_REQUEST['id'];
echo $ids;

the variable $ids is empty and when execute my code in controller.php file it’s creating new opportunity record it’s not updating existing record.

The URL you are using is calling the variable ā€œrecordā€ so this is the key you will need to use in the code:

$_REQUEST['record']
1 Like

thank you…
thank you very much Jim…