how to update opportunity stage when button clicked

Hi,

I want to update opportunity stage value when save button clicked.

opportunity stage is a dropdown field in opportunity module. In that dropdown field contains : Lead, new business opportunity, device considered, sample requested, design in and design win.

So for this I’ve created workflows like this:

1st Workflow:
Condition:
when opportunity stage Equals To Value Lead

Action:
modify record Opportunity stage Equals To Value New Business Opportunity

2nd Workflow:
Condition:
when opportunity stage Equals To Value New Business Opportunity

Action:
modify record Opportunity stage Equals To Value Device Considered

3rd Workflow:
Condition:
when opportunity stage Equals To Value Device Considred

Action:
modify record Opportunity stage Equals To Value Sample Requested

After Creating all workflows, I created new opportunity when save button clicked All Workflows execute at a time and then opportunity stage value directly goes to Device Considered and sometimes it goes to Last stage.

Please help me how do I change stage one by one when button clicked…

if I do this opportunity stage directly comes to last stage right…

that workflow executes only once after that opportunity stage value will be changed…

I think that you are better off using an after_save logic hook or else you have to introduce some kind of flag to avoid having all workflows run at once.

after_save logic hook is better idea.

For this I added one function in custom/modules/Opportunities/OpportunitiesJjwg_MapsLogicHook.php

function ChangeStages($bean, $event,$arguments){
		
		 // after_save
        $jjwg_Maps = get_module_info('jjwg_Maps');
        if ($jjwg_Maps->settings['logic_hooks_enabled']) {
		if($bean->opportunity_stage_c==="Select"){
			$bean->opportunity_stage_c="Lead";
		}
		elseif($bean->opportunity_stage_c==="Lead"){
			$bean->opportunity_stage_c="New Business Opportunity";
		}
		elseif($bean->opportunity_stage_c==="New Business Opportunity"){
			$bean->opportunity_stage_c="Device Considered";
		}
		elseif($bean->opportunity_stage_c==="Device Considered"){
			$bean->opportunity_stage_c="Sample Requested";
		}
		elseif($bean->opportunity_stage_c==="Sample Requested"){
			$bean->opportunity_stage_c="Sample Submitted";
		}
		elseif($bean->opportunity_stage_c==="Sample Submitted"){
			$bean->opportunity_stage_c="Design In";
		}
		elseif($bean->opportunity_stage_c==="Design In"){
			$bean->opportunity_stage_c="Design Win";
		}
	}
	}

and added one more line in custom\modules\Opportunities\logic_hooks.php

$hook_array['after_save'][] = Array(79, 'ChangeStages','custom/modules/Opportunities/OpportunitiesJjwg_MapsLogicHook.php','OpportunitiesJjwg_MapsLogicHook','ChangeStages');

But it is not executing when i create new opportunity
and please tell me whether the code is correct or not…

I have the following suggestions:

  1. Try saving the bean when you have modified it. Note that this is an after_save so the record has already been saved and in order to maintain your changes you also should save.
    Otherwise:

  2. Use a before_save (sorry if I suggested after_save) and you don’t need to save the bean because it is going to be saved in the next phases of the flow execution. If you use a before_save, be careful because you may end up with two copies of the same record if you save the bean

  3. While testing you could try putting some echo statements followed by exit; to test if you are getting in the portion of code and if the values are being modified. Once finished your test you should remove these statements.

Final note:
You should be aware that each time a record is modified the stage will advance to the next one (for example a user edits a record and he immediately realises that he typed a wrong telephone number (or whatever), so if he edits again the record, the stage is modified again).
I think that you should improve the logic for this reason.

how to save the bean…?
like this:

$bean->save();

That’s correct

i checked that, code is executing but opportunity_stage field is not updating

Is this correct way of assigning value to the field…


if($bean->opportunity_stage_c==="Select"){
	$bean->opportunity_stage_c="Lead";
	}

Try this:

if($bean->opportunity_stage_c==="Select"){
	$bean->opportunity_stage_c="Lead";
echo $bean->opportunity_stage_c;
exit;
	}

In this way you can see wheter a value is being assigned. Once you have checked it remove the echo and exit statements.

Then, at the end of your script you have to save the bean.

Instead of nested if statements I would use a switch statement to make sure that you control all the possible values.

One more thing:
You have to assign the key of the dropdown value. Not the Label.

If you assign the label you may get unpredictable results.

If you don’t know the keys you can check them either in Admin->Dropdown Editor and click on your dropdown or open the file custom/include/language/en_us.lang.php (or the language you are using)

oh thank you and sorry for late replay…

I’ve one doubt in my mind. can we call logic hook with other button…?

if it is possible call to logic hook with other button then we can easily separate save button and step change button…

oh thank you and sorry for late replay…

I’ve one doubt in my mind. can we call logic hook with other button…?

if it is possible call to logic hook with other button then we can easily separate save button and step change button…

one more thing i want to ask, why javascript code is not working in OpportunitiesJjwg_MapsLogicHook.php file

I added one line that is not executing…

echo "<script type='text/javascript'>alert('Error Message');</script>";

This would be better as a before save logic hook, without the bean->save in it. I also agree with amariussi that the logic should be changed.

Hi there,

without $bean->save also it’s working…
there is no problem with $bean->save

Can we call logic hooks with any other custom buttons…?
I create one button in opportunity edit view.
when i click that button logic hook particular logic hook function should call.
is it possible…

If you wish to call php code from a button, you would need to use ajax(asynchronous javascript) and call a controller.

Could you please tell me how to do that…

I tried to call php program but that’s not works

i called javascript function when button clicked.from javascript code i called php program.

<script>
function loadphp(){
window.location.href=fileurl;
}

</script>

<input type="button" name="nextstep" value="nextstep" onclick="loadphp()" />

when I click nextstep button it’s directly goes to opportunity view and it’s also not performing any operation…

You please tell me how to call logic hook.

Use Jquery:
http://api.jquery.com/jquery.ajax/

You will need to create a controller.php file in the custom/modules/Opportunities folder.

<?php
class OpportunitiesController extends SugarController {
    //Can now put actions here
    public function action_NameHere(){
//do stuff here
        echo "Hello world!";
die();
    }
}

You can then call with jquery ajax:
Something like this:

$.ajax({
  url: "index.php?module=Opportunities&action=NameHere",
})
  .done(function( result ) {
    alert(result);
  });

where i have to write ajax code…

The onclick of the button.

oh thanks let me try this code…

Hi Simeon,

I created controller.php in custom/opportunities/

added my code but stage are not changing when my nextstep button clicked.

this code in 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();
    }
}

JS code:

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

Nextstep button:


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

when i clicked nextstep button stage_c field is not changing…
please check the code and tell me whether i made any mistake in the code…